2018-10-10 17:22:13 +00:00
|
|
|
/* exported calcStyleDigest detectSloppyRegexps */
|
2017-03-26 02:30:59 +00:00
|
|
|
'use strict';
|
|
|
|
|
2017-04-24 13:29:48 +00:00
|
|
|
function normalizeStyleSections({sections}) {
|
|
|
|
// retain known properties in an arbitrarily predefined order
|
|
|
|
return (sections || []).map(section => ({
|
|
|
|
code: section.code || '',
|
|
|
|
urls: section.urls || [],
|
|
|
|
urlPrefixes: section.urlPrefixes || [],
|
|
|
|
domains: section.domains || [],
|
|
|
|
regexps: section.regexps || [],
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
|
|
|
function calcStyleDigest(style) {
|
2017-09-16 01:24:50 +00:00
|
|
|
const jsonString = style.usercssData ?
|
|
|
|
style.sourceCode : JSON.stringify(normalizeStyleSections(style));
|
2017-04-24 13:29:48 +00:00
|
|
|
const text = new TextEncoder('utf-8').encode(jsonString);
|
2017-04-23 12:19:18 +00:00
|
|
|
return crypto.subtle.digest('SHA-1', text).then(hex);
|
2017-04-24 13:29:48 +00:00
|
|
|
|
2017-04-23 12:19:18 +00:00
|
|
|
function hex(buffer) {
|
|
|
|
const parts = [];
|
|
|
|
const PAD8 = '00000000';
|
|
|
|
const view = new DataView(buffer);
|
|
|
|
for (let i = 0; i < view.byteLength; i += 4) {
|
|
|
|
parts.push((PAD8 + view.getUint32(i).toString(16)).slice(-8));
|
|
|
|
}
|
|
|
|
return parts.join('');
|
|
|
|
}
|
|
|
|
}
|
2017-09-03 08:12:18 +00:00
|
|
|
|
2018-01-01 17:02:49 +00:00
|
|
|
/*
|
|
|
|
According to CSS4 @document specification the entire URL must match.
|
|
|
|
Stylish-for-Chrome implemented it incorrectly since the very beginning.
|
|
|
|
We'll detect styles that abuse the bug by finding the sections that
|
|
|
|
would have been applied by Stylish but not by us as we follow the spec.
|
|
|
|
Additionally we'll check for invalid regexps.
|
|
|
|
*/
|
|
|
|
function detectSloppyRegexps({matchUrl, ids}) {
|
2018-10-10 17:22:13 +00:00
|
|
|
// TODO
|
2018-01-01 17:02:49 +00:00
|
|
|
}
|