2018-01-04 13:43:54 +00:00
|
|
|
/* global getStyleWithNoCode styleSectionsEqual */
|
2017-03-26 02:30:59 +00:00
|
|
|
'use strict';
|
|
|
|
|
2018-01-30 15:44:51 +00:00
|
|
|
const RX_NAMESPACE = /\s*(@namespace\s+(?:\S+\s+)?url\(http:\/\/.*?\);)\s*/g;
|
|
|
|
const RX_CHARSET = /\s*@charset\s+(['"]).*?\1\s*;\s*/g;
|
2018-01-30 15:45:29 +00:00
|
|
|
const RX_CSS_COMMENTS = /\/\*[\s\S]*?(?:\*\/|$)/g;
|
2017-06-25 10:16:23 +00:00
|
|
|
// eslint-disable-next-line no-var
|
|
|
|
var SLOPPY_REGEXP_PREFIX = '\0';
|
2017-04-26 21:49:03 +00:00
|
|
|
|
2017-09-03 08:12:18 +00:00
|
|
|
// CSS transition bug workaround: since we insert styles asynchronously,
|
|
|
|
// the browsers, especially Firefox, may apply all transitions on page load
|
|
|
|
const CSS_TRANSITION_SUPPRESSOR = '* { transition: none !important; }';
|
|
|
|
const RX_CSS_TRANSITION_DETECTOR = /([\s\n;/{]|-webkit-|-moz-)transition[\s\n]*:[\s\n]*(?!none)/;
|
|
|
|
|
2017-04-11 10:51:40 +00:00
|
|
|
// Note, only 'var'-declared variables are visible from another extension page
|
|
|
|
// eslint-disable-next-line no-var
|
|
|
|
var cachedStyles = {
|
2017-08-21 18:58:24 +00:00
|
|
|
list: null, // array of all styles
|
|
|
|
byId: new Map(), // all styles indexed by id
|
|
|
|
filters: new Map(), // filterStyles() parameters mapped to the returned results, 10k max
|
|
|
|
regexps: new Map(), // compiled style regexps
|
|
|
|
urlDomains: new Map(), // getDomain() results for 100 last checked urls
|
2017-09-03 08:12:18 +00:00
|
|
|
needTransitionPatch: new Map(), // FF bug workaround
|
2017-04-11 10:51:40 +00:00
|
|
|
mutex: {
|
2017-09-12 09:10:43 +00:00
|
|
|
inProgress: true, // while getStyles() is reading IndexedDB all subsequent calls
|
|
|
|
// (initially 'true' to prevent rogue getStyles before dbExec.initialized)
|
2017-08-21 18:58:24 +00:00
|
|
|
onDone: [], // to getStyles() are queued and resolved when the first one finishes
|
2017-04-11 10:51:40 +00:00
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2017-09-02 15:36:32 +00:00
|
|
|
// eslint-disable-next-line no-var
|
|
|
|
var dbExec = dbExecIndexedDB;
|
2017-09-03 18:25:19 +00:00
|
|
|
dbExec.initialized = false;
|
2017-09-02 15:36:32 +00:00
|
|
|
|
|
|
|
// we use chrome.storage.local fallback if IndexedDB doesn't save data,
|
|
|
|
// which, once detected on the first run, is remembered in chrome.storage.local
|
|
|
|
// for reliablility and in localStorage for fast synchronous access
|
|
|
|
// (FF may block localStorage depending on its privacy options)
|
|
|
|
do {
|
2017-09-03 18:25:19 +00:00
|
|
|
const done = () => {
|
2017-09-12 09:10:43 +00:00
|
|
|
cachedStyles.mutex.inProgress = false;
|
2017-09-03 18:25:19 +00:00
|
|
|
getStyles().then(() => {
|
|
|
|
dbExec.initialized = true;
|
|
|
|
window.dispatchEvent(new Event('storageReady'));
|
|
|
|
});
|
|
|
|
};
|
2017-09-02 15:36:32 +00:00
|
|
|
const fallback = () => {
|
|
|
|
dbExec = dbExecChromeStorage;
|
|
|
|
chromeLocal.set({dbInChromeStorage: true});
|
|
|
|
localStorage.dbInChromeStorage = 'true';
|
|
|
|
ignoreChromeError();
|
2017-09-03 18:25:19 +00:00
|
|
|
done();
|
2017-09-02 15:36:32 +00:00
|
|
|
};
|
|
|
|
const fallbackSet = localStorage.dbInChromeStorage;
|
|
|
|
if (fallbackSet === 'true' || !tryCatch(() => indexedDB)) {
|
|
|
|
fallback();
|
|
|
|
break;
|
|
|
|
} else if (fallbackSet === 'false') {
|
2017-09-03 18:25:19 +00:00
|
|
|
done();
|
2017-09-02 15:36:32 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
chromeLocal.get('dbInChromeStorage')
|
|
|
|
.then(data =>
|
|
|
|
data && data.dbInChromeStorage && Promise.reject())
|
2017-11-05 03:13:51 +00:00
|
|
|
.then(() =>
|
|
|
|
tryCatch(dbExecIndexedDB, 'getAllKeys', IDBKeyRange.lowerBound(1), 1) ||
|
|
|
|
Promise.reject())
|
2017-09-02 15:36:32 +00:00
|
|
|
.then(({target}) => (
|
|
|
|
(target.result || [])[0] ?
|
|
|
|
Promise.reject('ok') :
|
2017-09-03 18:56:46 +00:00
|
|
|
dbExecIndexedDB('put', {id: -1})))
|
|
|
|
.then(() =>
|
|
|
|
dbExecIndexedDB('get', -1))
|
2017-09-02 15:36:32 +00:00
|
|
|
.then(({target}) => (
|
|
|
|
(target.result || {}).id === -1 ?
|
2017-09-03 18:56:46 +00:00
|
|
|
dbExecIndexedDB('delete', -1) :
|
2017-09-02 15:36:32 +00:00
|
|
|
Promise.reject()))
|
2017-09-03 18:56:46 +00:00
|
|
|
.then(() =>
|
|
|
|
Promise.reject('ok'))
|
2017-09-02 15:36:32 +00:00
|
|
|
.catch(result => {
|
|
|
|
if (result === 'ok') {
|
|
|
|
chromeLocal.set({dbInChromeStorage: false});
|
|
|
|
localStorage.dbInChromeStorage = 'false';
|
2017-09-03 18:25:19 +00:00
|
|
|
done();
|
2017-09-02 15:36:32 +00:00
|
|
|
} else {
|
|
|
|
fallback();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
} while (0);
|
|
|
|
|
2017-04-11 10:51:40 +00:00
|
|
|
|
2017-09-02 15:36:32 +00:00
|
|
|
function dbExecIndexedDB(method, ...args) {
|
2017-04-25 21:48:27 +00:00
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
Object.assign(indexedDB.open('stylish', 2), {
|
|
|
|
onsuccess(event) {
|
|
|
|
const database = event.target.result;
|
|
|
|
if (!method) {
|
|
|
|
resolve(database);
|
|
|
|
} else {
|
|
|
|
const transaction = database.transaction(['styles'], 'readwrite');
|
|
|
|
const store = transaction.objectStore('styles');
|
2017-09-02 15:36:32 +00:00
|
|
|
Object.assign(store[method](...args), {
|
2017-04-25 21:48:27 +00:00
|
|
|
onsuccess: event => resolve(event, store, transaction, database),
|
|
|
|
onerror: reject,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
|
|
|
onerror(event) {
|
2017-08-24 13:30:13 +00:00
|
|
|
console.warn(event.target.error || event.target.errorCode);
|
2017-04-25 21:48:27 +00:00
|
|
|
reject(event);
|
|
|
|
},
|
|
|
|
onupgradeneeded(event) {
|
2017-07-16 18:02:00 +00:00
|
|
|
if (event.oldVersion === 0) {
|
2017-04-25 21:48:27 +00:00
|
|
|
event.target.result.createObjectStore('styles', {
|
|
|
|
keyPath: 'id',
|
|
|
|
autoIncrement: true,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
|
|
|
});
|
|
|
|
});
|
2017-03-26 02:30:59 +00:00
|
|
|
}
|
2015-02-09 04:02:08 +00:00
|
|
|
|
Improve style caching, cache requests too, add code:false mode
Previously, when a cache was invalidated and every tab/iframe issued a getStyles request, we previous needlessly accessed IndexedDB for each of these requests. It happened because 1) the global cachedStyles was created only at the end of the async DB-reading, 2) and each style record is retrieved asynchronously so the single threaded JS engine interleaved all these operations. It could easily span a few seconds when many tabs are open and you have like 100 styles.
Now, in getStyles: all requests issued while cachedStyles is being populated are queued and invoked at the end.
Now, in filterStyles: all requests are cached using the request's options combined in a string as a key. It also helps on each navigation because we monitor page loading process at different stages: before, when committed, history traversal, requesting applicable styles by a content script. Icon badge update also may issue a copy of the just issued request by one of the navigation listeners.
Now, the caches are invalidated smartly: style add/update/delete/toggle only purges filtering cache, and modifies style cache in-place without re-reading the entire IndexedDB.
Now, code:false mode for manage page that only needs style meta. It reduces the transferred message size 10-100 times thus reducing the overhead caused by to internal JSON-fication in the extensions API.
Also fast&direct getStylesSafe for own pages; code cosmetics
2017-03-17 22:50:35 +00:00
|
|
|
|
2017-09-02 15:36:32 +00:00
|
|
|
function dbExecChromeStorage(method, data) {
|
|
|
|
const STYLE_KEY_PREFIX = 'style-';
|
|
|
|
switch (method) {
|
|
|
|
case 'get':
|
|
|
|
return chromeLocal.getValue(STYLE_KEY_PREFIX + data)
|
|
|
|
.then(result => ({target: {result}}));
|
|
|
|
|
|
|
|
case 'put':
|
|
|
|
if (!data.id) {
|
|
|
|
return getStyles().then(() => {
|
|
|
|
data.id = 1;
|
|
|
|
for (const style of cachedStyles.list) {
|
|
|
|
data.id = Math.max(data.id, style.id + 1);
|
|
|
|
}
|
|
|
|
return dbExecChromeStorage('put', data);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return chromeLocal.setValue(STYLE_KEY_PREFIX + data.id, data)
|
|
|
|
.then(() => (chrome.runtime.lastError ? Promise.reject() : data.id));
|
|
|
|
|
|
|
|
case 'delete':
|
|
|
|
return chromeLocal.remove(STYLE_KEY_PREFIX + data);
|
|
|
|
|
|
|
|
case 'getAll':
|
|
|
|
return chromeLocal.get(null).then(storage => {
|
|
|
|
const styles = [];
|
|
|
|
for (const key in storage) {
|
|
|
|
if (key.startsWith(STYLE_KEY_PREFIX) &&
|
|
|
|
Number(key.substr(STYLE_KEY_PREFIX.length))) {
|
|
|
|
styles.push(storage[key]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return {target: {result: styles}};
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return Promise.reject();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-04-25 21:48:27 +00:00
|
|
|
function getStyles(options) {
|
2017-03-26 02:30:59 +00:00
|
|
|
if (cachedStyles.list) {
|
2017-04-25 21:48:27 +00:00
|
|
|
return Promise.resolve(filterStyles(options));
|
2017-03-26 02:30:59 +00:00
|
|
|
}
|
|
|
|
if (cachedStyles.mutex.inProgress) {
|
2017-04-25 21:48:27 +00:00
|
|
|
return new Promise(resolve => {
|
|
|
|
cachedStyles.mutex.onDone.push({options, resolve});
|
|
|
|
});
|
2017-03-26 02:30:59 +00:00
|
|
|
}
|
|
|
|
cachedStyles.mutex.inProgress = true;
|
|
|
|
|
2017-04-25 21:48:27 +00:00
|
|
|
return dbExec('getAll').then(event => {
|
|
|
|
cachedStyles.list = event.target.result || [];
|
2018-10-26 13:48:58 +00:00
|
|
|
cachedStyles.list.forEach(fixUsoMd5Issue);
|
2017-04-25 21:48:27 +00:00
|
|
|
cachedStyles.byId.clear();
|
|
|
|
for (const style of cachedStyles.list) {
|
|
|
|
cachedStyles.byId.set(style.id, style);
|
2017-12-03 17:32:50 +00:00
|
|
|
if (!style.name) {
|
|
|
|
style.name = 'ID: ' + style.id;
|
|
|
|
}
|
2017-04-25 21:48:27 +00:00
|
|
|
}
|
2017-03-26 02:30:59 +00:00
|
|
|
|
2017-04-25 21:48:27 +00:00
|
|
|
cachedStyles.mutex.inProgress = false;
|
|
|
|
for (const {options, resolve} of cachedStyles.mutex.onDone) {
|
|
|
|
resolve(filterStyles(options));
|
|
|
|
}
|
|
|
|
cachedStyles.mutex.onDone = [];
|
|
|
|
return filterStyles(options);
|
|
|
|
});
|
2015-01-30 17:07:24 +00:00
|
|
|
}
|
|
|
|
|
Improve style caching, cache requests too, add code:false mode
Previously, when a cache was invalidated and every tab/iframe issued a getStyles request, we previous needlessly accessed IndexedDB for each of these requests. It happened because 1) the global cachedStyles was created only at the end of the async DB-reading, 2) and each style record is retrieved asynchronously so the single threaded JS engine interleaved all these operations. It could easily span a few seconds when many tabs are open and you have like 100 styles.
Now, in getStyles: all requests issued while cachedStyles is being populated are queued and invoked at the end.
Now, in filterStyles: all requests are cached using the request's options combined in a string as a key. It also helps on each navigation because we monitor page loading process at different stages: before, when committed, history traversal, requesting applicable styles by a content script. Icon badge update also may issue a copy of the just issued request by one of the navigation listeners.
Now, the caches are invalidated smartly: style add/update/delete/toggle only purges filtering cache, and modifies style cache in-place without re-reading the entire IndexedDB.
Now, code:false mode for manage page that only needs style meta. It reduces the transferred message size 10-100 times thus reducing the overhead caused by to internal JSON-fication in the extensions API.
Also fast&direct getStylesSafe for own pages; code cosmetics
2017-03-17 22:50:35 +00:00
|
|
|
|
2017-03-30 23:18:41 +00:00
|
|
|
function filterStyles({
|
2017-04-19 16:13:11 +00:00
|
|
|
enabled = null,
|
2017-03-30 23:18:41 +00:00
|
|
|
id = null,
|
|
|
|
matchUrl = null,
|
2017-12-09 01:43:02 +00:00
|
|
|
md5Url = null,
|
2017-03-30 23:18:41 +00:00
|
|
|
asHash = null,
|
2018-01-01 17:02:49 +00:00
|
|
|
omitCode,
|
2017-03-30 23:18:41 +00:00
|
|
|
strictRegexp = true, // used by the popup to detect bad regexps
|
|
|
|
} = {}) {
|
2018-01-10 16:51:05 +00:00
|
|
|
if (id) id = Number(id);
|
|
|
|
if (asHash) enabled = true;
|
2017-03-26 02:30:59 +00:00
|
|
|
|
2017-07-16 18:02:00 +00:00
|
|
|
if (
|
|
|
|
enabled === null &&
|
|
|
|
id === null &&
|
|
|
|
matchUrl === null &&
|
2017-12-09 01:43:02 +00:00
|
|
|
md5Url === null &&
|
2017-07-16 18:02:00 +00:00
|
|
|
asHash !== true
|
|
|
|
) {
|
2017-03-30 23:18:41 +00:00
|
|
|
return cachedStyles.list;
|
2017-03-26 02:30:59 +00:00
|
|
|
}
|
|
|
|
|
2017-08-16 17:40:24 +00:00
|
|
|
if (matchUrl && !URLS.supported(matchUrl)) {
|
2018-01-10 16:51:05 +00:00
|
|
|
return asHash ? {length: 0} : [];
|
2017-04-12 13:56:41 +00:00
|
|
|
}
|
|
|
|
|
2017-09-03 15:16:00 +00:00
|
|
|
const blankHash = asHash && {
|
2018-01-10 16:51:05 +00:00
|
|
|
length: 0,
|
2017-09-03 15:16:00 +00:00
|
|
|
disableAll: prefs.get('disableAll'),
|
|
|
|
exposeIframes: prefs.get('exposeIframes'),
|
|
|
|
};
|
|
|
|
|
2018-01-11 14:10:14 +00:00
|
|
|
// make sure to use the same order in updateFiltersCache()
|
|
|
|
const cacheKey =
|
|
|
|
enabled + '\t' +
|
|
|
|
id + '\t' +
|
|
|
|
matchUrl + '\t' +
|
|
|
|
md5Url + '\t' +
|
|
|
|
asHash + '\t' +
|
|
|
|
strictRegexp;
|
2017-03-26 02:30:59 +00:00
|
|
|
const cached = cachedStyles.filters.get(cacheKey);
|
2018-01-01 17:02:49 +00:00
|
|
|
let styles;
|
2017-03-26 02:30:59 +00:00
|
|
|
if (cached) {
|
|
|
|
cached.hits++;
|
|
|
|
cached.lastHit = Date.now();
|
2018-01-01 17:02:49 +00:00
|
|
|
styles = asHash
|
2017-04-28 23:36:10 +00:00
|
|
|
? Object.assign(blankHash, cached.styles)
|
2018-01-01 17:02:49 +00:00
|
|
|
: cached.styles.slice();
|
|
|
|
} else {
|
|
|
|
styles = filterStylesInternal({
|
|
|
|
enabled,
|
|
|
|
id,
|
|
|
|
matchUrl,
|
|
|
|
md5Url,
|
|
|
|
asHash,
|
|
|
|
strictRegexp,
|
|
|
|
blankHash,
|
|
|
|
cacheKey,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
if (!omitCode) return styles;
|
|
|
|
if (!asHash) return styles.map(getStyleWithNoCode);
|
|
|
|
for (const id in styles) {
|
2018-01-10 18:54:59 +00:00
|
|
|
const sections = styles[id];
|
|
|
|
if (Array.isArray(sections)) {
|
|
|
|
styles[id] = getStyleWithNoCode({sections}).sections;
|
2018-01-01 17:02:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return styles;
|
2017-04-13 18:03:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-10-26 13:48:58 +00:00
|
|
|
// The md5Url provided by USO includes a duplicate "update" subdomain (see #523),
|
|
|
|
// This fixes any already installed styles containing this error
|
|
|
|
function fixUsoMd5Issue(style) {
|
|
|
|
if (style && style.md5Url && style.md5Url.includes('update.update.userstyles')) {
|
|
|
|
style.md5Url = style.md5Url.replace('update.update.userstyles', 'update.userstyles');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-13 18:03:25 +00:00
|
|
|
function filterStylesInternal({
|
|
|
|
// js engines don't like big functions (V8 often deoptimized the original filterStyles)
|
|
|
|
// it also makes sense to extract the less frequently executed code
|
|
|
|
enabled,
|
|
|
|
id,
|
|
|
|
matchUrl,
|
2017-12-09 01:43:02 +00:00
|
|
|
md5Url,
|
2017-04-13 18:03:25 +00:00
|
|
|
asHash,
|
|
|
|
strictRegexp,
|
2017-04-28 23:36:10 +00:00
|
|
|
blankHash,
|
2017-04-13 18:03:25 +00:00
|
|
|
cacheKey,
|
|
|
|
}) {
|
2017-03-26 07:42:13 +00:00
|
|
|
if (matchUrl && !cachedStyles.urlDomains.has(matchUrl)) {
|
|
|
|
cachedStyles.urlDomains.set(matchUrl, getDomains(matchUrl));
|
|
|
|
for (let i = cachedStyles.urlDomains.size - 100; i > 0; i--) {
|
|
|
|
const firstKey = cachedStyles.urlDomains.keys().next().value;
|
|
|
|
cachedStyles.urlDomains.delete(firstKey);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-26 02:30:59 +00:00
|
|
|
const styles = id === null
|
2017-03-30 23:18:41 +00:00
|
|
|
? cachedStyles.list
|
|
|
|
: [cachedStyles.byId.get(id)];
|
2017-09-03 15:16:00 +00:00
|
|
|
if (!styles[0]) {
|
2017-03-26 02:30:59 +00:00
|
|
|
// may happen when users [accidentally] reopen an old URL
|
|
|
|
// of edit.html with a non-existent style id parameter
|
2017-09-03 19:34:42 +00:00
|
|
|
return asHash ? blankHash : [];
|
2017-03-26 02:30:59 +00:00
|
|
|
}
|
2018-01-10 16:51:05 +00:00
|
|
|
const filtered = asHash ? {length: 0} : [];
|
2017-03-30 23:18:41 +00:00
|
|
|
const needSections = asHash || matchUrl !== null;
|
2017-08-16 17:40:24 +00:00
|
|
|
const matchUrlBase = matchUrl && matchUrl.includes('#') && matchUrl.split('#', 1)[0];
|
2017-03-30 23:18:41 +00:00
|
|
|
|
2017-07-14 08:46:10 +00:00
|
|
|
let style;
|
|
|
|
for (let i = 0; (style = styles[i]); i++) {
|
2017-07-16 18:02:00 +00:00
|
|
|
if ((enabled === null || style.enabled === enabled)
|
2017-12-09 01:43:02 +00:00
|
|
|
&& (md5Url === null || style.md5Url === md5Url)
|
2017-07-16 18:02:00 +00:00
|
|
|
&& (id === null || style.id === id)) {
|
2017-03-30 23:18:41 +00:00
|
|
|
const sections = needSections &&
|
2017-08-16 17:40:24 +00:00
|
|
|
getApplicableSections({
|
|
|
|
style,
|
|
|
|
matchUrl,
|
|
|
|
strictRegexp,
|
|
|
|
stopOnFirst: !asHash,
|
|
|
|
skipUrlCheck: true,
|
|
|
|
matchUrlBase,
|
|
|
|
});
|
2017-03-26 02:30:59 +00:00
|
|
|
if (asHash) {
|
|
|
|
if (sections.length) {
|
|
|
|
filtered[style.id] = sections;
|
2018-01-10 16:51:05 +00:00
|
|
|
filtered.length++;
|
2017-03-26 02:30:59 +00:00
|
|
|
}
|
|
|
|
} else if (matchUrl === null || sections.length) {
|
|
|
|
filtered.push(style);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-04-13 18:03:25 +00:00
|
|
|
|
2017-03-26 02:30:59 +00:00
|
|
|
cachedStyles.filters.set(cacheKey, {
|
|
|
|
styles: filtered,
|
|
|
|
lastHit: Date.now(),
|
|
|
|
hits: 1,
|
|
|
|
});
|
|
|
|
if (cachedStyles.filters.size > 10000) {
|
|
|
|
cleanupCachedFilters();
|
|
|
|
}
|
2017-04-13 18:03:25 +00:00
|
|
|
|
2017-09-03 19:34:42 +00:00
|
|
|
// a shallow copy is needed because the cache doesn't store options like disableAll
|
|
|
|
return asHash
|
|
|
|
? Object.assign(blankHash, filtered)
|
|
|
|
: filtered;
|
2015-01-30 17:07:24 +00:00
|
|
|
}
|
|
|
|
|
Improve style caching, cache requests too, add code:false mode
Previously, when a cache was invalidated and every tab/iframe issued a getStyles request, we previous needlessly accessed IndexedDB for each of these requests. It happened because 1) the global cachedStyles was created only at the end of the async DB-reading, 2) and each style record is retrieved asynchronously so the single threaded JS engine interleaved all these operations. It could easily span a few seconds when many tabs are open and you have like 100 styles.
Now, in getStyles: all requests issued while cachedStyles is being populated are queued and invoked at the end.
Now, in filterStyles: all requests are cached using the request's options combined in a string as a key. It also helps on each navigation because we monitor page loading process at different stages: before, when committed, history traversal, requesting applicable styles by a content script. Icon badge update also may issue a copy of the just issued request by one of the navigation listeners.
Now, the caches are invalidated smartly: style add/update/delete/toggle only purges filtering cache, and modifies style cache in-place without re-reading the entire IndexedDB.
Now, code:false mode for manage page that only needs style meta. It reduces the transferred message size 10-100 times thus reducing the overhead caused by to internal JSON-fication in the extensions API.
Also fast&direct getStylesSafe for own pages; code cosmetics
2017-03-17 22:50:35 +00:00
|
|
|
|
2017-03-25 02:35:54 +00:00
|
|
|
function saveStyle(style) {
|
2017-04-28 11:05:25 +00:00
|
|
|
const id = Number(style.id) || null;
|
2017-04-25 21:48:27 +00:00
|
|
|
const reason = style.reason;
|
|
|
|
const notify = style.notify !== false;
|
|
|
|
delete style.method;
|
|
|
|
delete style.reason;
|
|
|
|
delete style.notify;
|
|
|
|
if (!style.name) {
|
|
|
|
delete style.name;
|
|
|
|
}
|
2017-07-14 08:46:10 +00:00
|
|
|
let existed;
|
|
|
|
let codeIsUpdated;
|
2017-08-05 16:49:25 +00:00
|
|
|
|
2018-10-26 13:48:58 +00:00
|
|
|
fixUsoMd5Issue(style);
|
|
|
|
|
2017-09-18 03:34:12 +00:00
|
|
|
return maybeCalcDigest()
|
2017-09-16 01:24:50 +00:00
|
|
|
.then(maybeImportFix)
|
|
|
|
.then(decide);
|
|
|
|
|
2017-09-18 03:34:12 +00:00
|
|
|
function maybeCalcDigest() {
|
2017-12-02 04:07:12 +00:00
|
|
|
if (['install', 'update', 'update-digest'].includes(reason)) {
|
2017-09-16 01:24:50 +00:00
|
|
|
return calcStyleDigest(style).then(digest => {
|
|
|
|
style.originalDigest = digest;
|
|
|
|
});
|
|
|
|
}
|
2017-09-18 03:34:12 +00:00
|
|
|
return Promise.resolve();
|
2017-05-03 15:37:47 +00:00
|
|
|
}
|
2017-09-16 01:24:50 +00:00
|
|
|
|
|
|
|
function maybeImportFix() {
|
|
|
|
if (reason === 'import') {
|
|
|
|
style.originalDigest = style.originalDigest || style.styleDigest; // TODO: remove in the future
|
|
|
|
delete style.styleDigest; // TODO: remove in the future
|
|
|
|
if (typeof style.originalDigest !== 'string' || style.originalDigest.length !== 40) {
|
|
|
|
delete style.originalDigest;
|
|
|
|
}
|
2017-05-03 15:37:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function decide() {
|
|
|
|
if (id !== null) {
|
|
|
|
// Update or create
|
|
|
|
style.id = id;
|
|
|
|
return dbExec('get', id).then((event, store) => {
|
|
|
|
const oldStyle = event.target.result;
|
|
|
|
existed = Boolean(oldStyle);
|
2017-07-16 18:02:00 +00:00
|
|
|
if (reason === 'update-digest' && oldStyle.originalDigest === style.originalDigest) {
|
2017-05-03 15:37:47 +00:00
|
|
|
return style;
|
|
|
|
}
|
2017-06-06 01:40:08 +00:00
|
|
|
codeIsUpdated = !existed || 'sections' in style && !styleSectionsEqual(style, oldStyle);
|
2017-11-26 17:47:23 +00:00
|
|
|
style = Object.assign({installDate: Date.now()}, oldStyle, style);
|
2017-05-03 15:37:47 +00:00
|
|
|
return write(style, store);
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
// Create
|
|
|
|
delete style.id;
|
|
|
|
style = Object.assign({
|
|
|
|
// Set optional things if they're undefined
|
|
|
|
enabled: true,
|
|
|
|
updateUrl: null,
|
|
|
|
md5Url: null,
|
|
|
|
url: null,
|
|
|
|
originalMd5: null,
|
2018-01-01 17:02:49 +00:00
|
|
|
installDate: Date.now(),
|
2017-05-03 15:37:47 +00:00
|
|
|
}, style);
|
|
|
|
return write(style);
|
|
|
|
}
|
2017-04-25 21:48:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function write(style, store) {
|
|
|
|
style.sections = normalizeStyleSections(style);
|
|
|
|
if (store) {
|
|
|
|
return new Promise(resolve => {
|
|
|
|
store.put(style).onsuccess = event => resolve(done(event));
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
return dbExec('put', style).then(done);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function done(event) {
|
2017-07-16 18:02:00 +00:00
|
|
|
if (reason === 'update-digest') {
|
2017-05-03 15:37:47 +00:00
|
|
|
return style;
|
|
|
|
}
|
2017-04-25 21:48:27 +00:00
|
|
|
style.id = style.id || event.target.result;
|
|
|
|
invalidateCache(existed ? {updated: style} : {added: style});
|
|
|
|
if (notify) {
|
|
|
|
notifyAllTabs({
|
|
|
|
method: existed ? 'styleUpdated' : 'styleAdded',
|
|
|
|
style, codeIsUpdated, reason,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return style;
|
|
|
|
}
|
2015-01-30 17:07:24 +00:00
|
|
|
}
|
|
|
|
|
Improve style caching, cache requests too, add code:false mode
Previously, when a cache was invalidated and every tab/iframe issued a getStyles request, we previous needlessly accessed IndexedDB for each of these requests. It happened because 1) the global cachedStyles was created only at the end of the async DB-reading, 2) and each style record is retrieved asynchronously so the single threaded JS engine interleaved all these operations. It could easily span a few seconds when many tabs are open and you have like 100 styles.
Now, in getStyles: all requests issued while cachedStyles is being populated are queued and invoked at the end.
Now, in filterStyles: all requests are cached using the request's options combined in a string as a key. It also helps on each navigation because we monitor page loading process at different stages: before, when committed, history traversal, requesting applicable styles by a content script. Icon badge update also may issue a copy of the just issued request by one of the navigation listeners.
Now, the caches are invalidated smartly: style add/update/delete/toggle only purges filtering cache, and modifies style cache in-place without re-reading the entire IndexedDB.
Now, code:false mode for manage page that only needs style meta. It reduces the transferred message size 10-100 times thus reducing the overhead caused by to internal JSON-fication in the extensions API.
Also fast&direct getStylesSafe for own pages; code cosmetics
2017-03-17 22:50:35 +00:00
|
|
|
|
2017-04-11 10:51:40 +00:00
|
|
|
function deleteStyle({id, notify = true}) {
|
2017-04-25 21:48:27 +00:00
|
|
|
id = Number(id);
|
|
|
|
return dbExec('delete', id).then(() => {
|
|
|
|
invalidateCache({deletedId: id});
|
|
|
|
if (notify) {
|
|
|
|
notifyAllTabs({method: 'styleDeleted', id});
|
|
|
|
}
|
|
|
|
return id;
|
|
|
|
});
|
2015-01-30 17:07:24 +00:00
|
|
|
}
|
|
|
|
|
Improve style caching, cache requests too, add code:false mode
Previously, when a cache was invalidated and every tab/iframe issued a getStyles request, we previous needlessly accessed IndexedDB for each of these requests. It happened because 1) the global cachedStyles was created only at the end of the async DB-reading, 2) and each style record is retrieved asynchronously so the single threaded JS engine interleaved all these operations. It could easily span a few seconds when many tabs are open and you have like 100 styles.
Now, in getStyles: all requests issued while cachedStyles is being populated are queued and invoked at the end.
Now, in filterStyles: all requests are cached using the request's options combined in a string as a key. It also helps on each navigation because we monitor page loading process at different stages: before, when committed, history traversal, requesting applicable styles by a content script. Icon badge update also may issue a copy of the just issued request by one of the navigation listeners.
Now, the caches are invalidated smartly: style add/update/delete/toggle only purges filtering cache, and modifies style cache in-place without re-reading the entire IndexedDB.
Now, code:false mode for manage page that only needs style meta. It reduces the transferred message size 10-100 times thus reducing the overhead caused by to internal JSON-fication in the extensions API.
Also fast&direct getStylesSafe for own pages; code cosmetics
2017-03-17 22:50:35 +00:00
|
|
|
|
2017-08-16 17:40:24 +00:00
|
|
|
function getApplicableSections({
|
|
|
|
style,
|
|
|
|
matchUrl,
|
|
|
|
strictRegexp = true,
|
|
|
|
// filterStylesInternal() sets the following to avoid recalc on each style:
|
|
|
|
stopOnFirst,
|
|
|
|
skipUrlCheck,
|
|
|
|
matchUrlBase = matchUrl.includes('#') && matchUrl.split('#', 1)[0],
|
|
|
|
// as per spec the fragment portion is ignored in @-moz-document:
|
|
|
|
// https://www.w3.org/TR/2012/WD-css3-conditional-20120911/#url-of-doc
|
|
|
|
// but the spec is outdated and doesn't account for SPA sites
|
|
|
|
// so we only respect it in case of url("http://exact.url/without/hash")
|
|
|
|
}) {
|
|
|
|
if (!skipUrlCheck && !URLS.supported(matchUrl)) {
|
2017-04-13 21:44:23 +00:00
|
|
|
return [];
|
|
|
|
}
|
2017-03-26 07:19:47 +00:00
|
|
|
const sections = [];
|
|
|
|
for (const section of style.sections) {
|
2017-04-13 21:44:23 +00:00
|
|
|
const {urls, domains, urlPrefixes, regexps, code} = section;
|
2017-05-05 14:21:17 +00:00
|
|
|
const isGlobal = !urls.length && !urlPrefixes.length && !domains.length && !regexps.length;
|
|
|
|
const isMatching = !isGlobal && (
|
|
|
|
urls.length
|
2017-08-16 17:40:24 +00:00
|
|
|
&& (urls.includes(matchUrl) || matchUrlBase && urls.includes(matchUrlBase))
|
2017-04-24 13:29:48 +00:00
|
|
|
|| urlPrefixes.length
|
|
|
|
&& arraySomeIsPrefix(urlPrefixes, matchUrl)
|
|
|
|
|| domains.length
|
|
|
|
&& arraySomeIn(cachedStyles.urlDomains.get(matchUrl) || getDomains(matchUrl), domains)
|
|
|
|
|| regexps.length
|
2017-05-05 14:21:17 +00:00
|
|
|
&& arraySomeMatches(regexps, matchUrl, strictRegexp));
|
|
|
|
if (isGlobal && !styleCodeEmpty(code) || isMatching) {
|
2017-04-13 21:44:23 +00:00
|
|
|
sections.push(section);
|
|
|
|
if (stopOnFirst) {
|
|
|
|
break;
|
2017-03-28 08:24:31 +00:00
|
|
|
}
|
2017-04-13 21:44:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return sections;
|
|
|
|
|
|
|
|
function arraySomeIsPrefix(array, string) {
|
|
|
|
for (const prefix of array) {
|
|
|
|
if (string.startsWith(prefix)) {
|
|
|
|
return true;
|
2017-03-26 07:19:47 +00:00
|
|
|
}
|
2017-04-13 21:44:23 +00:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
function arraySomeIn(array, haystack) {
|
|
|
|
for (const el of array) {
|
|
|
|
if (haystack.indexOf(el) >= 0) {
|
|
|
|
return true;
|
2017-03-28 08:24:31 +00:00
|
|
|
}
|
2017-04-13 21:44:23 +00:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
function arraySomeMatches(array, matchUrl, strictRegexp) {
|
|
|
|
for (const regexp of array) {
|
|
|
|
for (let pass = 1; pass <= (strictRegexp ? 1 : 2); pass++) {
|
2017-07-16 18:02:00 +00:00
|
|
|
const cacheKey = pass === 1 ? regexp : SLOPPY_REGEXP_PREFIX + regexp;
|
2017-04-13 21:44:23 +00:00
|
|
|
let rx = cachedStyles.regexps.get(cacheKey);
|
2017-07-16 18:02:00 +00:00
|
|
|
if (rx === false) {
|
2017-04-13 21:44:23 +00:00
|
|
|
// invalid regexp
|
|
|
|
break;
|
2017-03-30 23:18:41 +00:00
|
|
|
}
|
2017-04-13 21:44:23 +00:00
|
|
|
if (!rx) {
|
2017-07-16 18:02:00 +00:00
|
|
|
const anchored = pass === 1 ? '^(?:' + regexp + ')$' : '^' + regexp + '$';
|
2017-04-13 21:44:23 +00:00
|
|
|
rx = tryRegExp(anchored);
|
|
|
|
cachedStyles.regexps.set(cacheKey, rx || false);
|
|
|
|
if (!rx) {
|
2017-03-30 23:18:41 +00:00
|
|
|
// invalid regexp
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2017-04-13 21:44:23 +00:00
|
|
|
if (rx.test(matchUrl)) {
|
|
|
|
return true;
|
|
|
|
}
|
2017-03-28 08:24:31 +00:00
|
|
|
}
|
2017-03-26 10:05:05 +00:00
|
|
|
}
|
2017-04-13 21:44:23 +00:00
|
|
|
return false;
|
2017-03-26 10:05:05 +00:00
|
|
|
}
|
2017-04-13 21:44:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function styleCodeEmpty(code) {
|
2017-05-05 14:21:17 +00:00
|
|
|
// Collect the global section if it's not empty, not comment-only, not namespace-only.
|
|
|
|
const cmtOpen = code && code.indexOf('/*');
|
|
|
|
if (cmtOpen >= 0) {
|
|
|
|
const cmtCloseLast = code.lastIndexOf('*/');
|
|
|
|
if (cmtCloseLast < 0) {
|
|
|
|
code = code.substr(0, cmtOpen);
|
|
|
|
} else {
|
|
|
|
code = code.substr(0, cmtOpen) +
|
|
|
|
code.substring(cmtOpen, cmtCloseLast + 2).replace(RX_CSS_COMMENTS, '') +
|
|
|
|
code.substr(cmtCloseLast + 2);
|
|
|
|
}
|
2017-04-13 21:44:23 +00:00
|
|
|
}
|
2018-01-30 15:44:51 +00:00
|
|
|
if (!code || !code.trim()) return true;
|
|
|
|
if (code.includes('@namespace')) code = code.replace(RX_NAMESPACE, '').trim();
|
|
|
|
if (code.includes('@charset')) code = code.replace(RX_CHARSET, '').trim();
|
|
|
|
return !code;
|
2016-03-07 02:27:17 +00:00
|
|
|
}
|
|
|
|
|
Improve style caching, cache requests too, add code:false mode
Previously, when a cache was invalidated and every tab/iframe issued a getStyles request, we previous needlessly accessed IndexedDB for each of these requests. It happened because 1) the global cachedStyles was created only at the end of the async DB-reading, 2) and each style record is retrieved asynchronously so the single threaded JS engine interleaved all these operations. It could easily span a few seconds when many tabs are open and you have like 100 styles.
Now, in getStyles: all requests issued while cachedStyles is being populated are queued and invoked at the end.
Now, in filterStyles: all requests are cached using the request's options combined in a string as a key. It also helps on each navigation because we monitor page loading process at different stages: before, when committed, history traversal, requesting applicable styles by a content script. Icon badge update also may issue a copy of the just issued request by one of the navigation listeners.
Now, the caches are invalidated smartly: style add/update/delete/toggle only purges filtering cache, and modifies style cache in-place without re-reading the entire IndexedDB.
Now, code:false mode for manage page that only needs style meta. It reduces the transferred message size 10-100 times thus reducing the overhead caused by to internal JSON-fication in the extensions API.
Also fast&direct getStylesSafe for own pages; code cosmetics
2017-03-17 22:50:35 +00:00
|
|
|
|
2017-04-13 16:44:43 +00:00
|
|
|
function invalidateCache({added, updated, deletedId} = {}) {
|
2018-01-11 14:10:14 +00:00
|
|
|
if (!cachedStyles.list) return;
|
2017-04-19 16:03:00 +00:00
|
|
|
const id = added ? added.id : updated ? updated.id : deletedId;
|
|
|
|
const cached = cachedStyles.byId.get(id);
|
2018-01-11 14:10:14 +00:00
|
|
|
|
2017-04-11 10:51:40 +00:00
|
|
|
if (updated) {
|
|
|
|
if (cached) {
|
2018-03-22 01:26:43 +00:00
|
|
|
const isSectionGlobal = section =>
|
|
|
|
!section.urls.length &&
|
|
|
|
!section.urlPrefixes.length &&
|
|
|
|
!section.domains.length &&
|
|
|
|
!section.regexps.length;
|
|
|
|
const hadOrHasGlobals = cached.sections.some(isSectionGlobal) ||
|
|
|
|
updated.sections.some(isSectionGlobal);
|
2018-01-11 14:10:14 +00:00
|
|
|
const reenabled = !cached.enabled && updated.enabled;
|
2018-03-22 01:26:43 +00:00
|
|
|
const equal = !hadOrHasGlobals &&
|
|
|
|
!reenabled &&
|
|
|
|
styleSectionsEqual(updated, cached, {ignoreCode: true});
|
2017-04-11 10:51:40 +00:00
|
|
|
Object.assign(cached, updated);
|
2018-01-11 14:10:14 +00:00
|
|
|
if (equal) {
|
|
|
|
updateFiltersCache(cached);
|
|
|
|
} else {
|
|
|
|
cachedStyles.filters.clear();
|
|
|
|
}
|
2017-09-03 08:12:18 +00:00
|
|
|
cachedStyles.needTransitionPatch.delete(id);
|
2017-04-19 16:03:00 +00:00
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
added = updated;
|
2017-04-11 10:51:40 +00:00
|
|
|
}
|
|
|
|
}
|
2018-01-11 14:10:14 +00:00
|
|
|
|
2017-04-11 10:51:40 +00:00
|
|
|
if (added) {
|
2017-04-19 16:03:00 +00:00
|
|
|
if (!cached) {
|
|
|
|
cachedStyles.list.push(added);
|
|
|
|
cachedStyles.byId.set(added.id, added);
|
|
|
|
cachedStyles.filters.clear();
|
2017-09-03 08:12:18 +00:00
|
|
|
cachedStyles.needTransitionPatch.delete(id);
|
2017-04-19 16:03:00 +00:00
|
|
|
}
|
2017-04-11 10:51:40 +00:00
|
|
|
return;
|
|
|
|
}
|
2018-01-11 14:10:14 +00:00
|
|
|
|
2017-04-19 16:03:00 +00:00
|
|
|
if (deletedId !== undefined) {
|
|
|
|
if (cached) {
|
|
|
|
const cachedIndex = cachedStyles.list.indexOf(cached);
|
2017-04-11 10:51:40 +00:00
|
|
|
cachedStyles.list.splice(cachedIndex, 1);
|
|
|
|
cachedStyles.byId.delete(deletedId);
|
2018-01-11 14:10:14 +00:00
|
|
|
for (const {styles} of cachedStyles.filters.values()) {
|
|
|
|
if (Array.isArray(styles)) {
|
|
|
|
const index = styles.findIndex(({id}) => id === deletedId);
|
|
|
|
if (index >= 0) styles.splice(index, 1);
|
|
|
|
} else if (deletedId in styles) {
|
|
|
|
delete styles[deletedId];
|
|
|
|
styles.length--;
|
|
|
|
}
|
|
|
|
}
|
2017-09-03 08:12:18 +00:00
|
|
|
cachedStyles.needTransitionPatch.delete(id);
|
2017-04-11 10:51:40 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2018-01-11 14:10:14 +00:00
|
|
|
|
2017-04-11 10:51:40 +00:00
|
|
|
cachedStyles.list = null;
|
|
|
|
cachedStyles.filters.clear();
|
2017-09-03 08:12:18 +00:00
|
|
|
cachedStyles.needTransitionPatch.clear(id);
|
2017-04-11 10:51:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-01-11 14:10:14 +00:00
|
|
|
function updateFiltersCache(style) {
|
|
|
|
const {id} = style;
|
|
|
|
for (const [key, {styles}] of cachedStyles.filters.entries()) {
|
|
|
|
if (Array.isArray(styles)) {
|
|
|
|
const index = styles.findIndex(style => style.id === id);
|
|
|
|
if (index >= 0) styles[index] = Object.assign({}, style);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (id in styles) {
|
|
|
|
const [, , matchUrl, , , strictRegexp] = key.split('\t');
|
|
|
|
if (!style.enabled) {
|
|
|
|
delete styles[id];
|
2018-01-14 12:40:36 +00:00
|
|
|
styles.length--;
|
2018-01-11 14:10:14 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
const matchUrlBase = matchUrl && matchUrl.includes('#') && matchUrl.split('#', 1)[0];
|
|
|
|
const sections = getApplicableSections({
|
|
|
|
style,
|
|
|
|
matchUrl,
|
|
|
|
matchUrlBase,
|
|
|
|
strictRegexp,
|
|
|
|
skipUrlCheck: true,
|
|
|
|
});
|
|
|
|
if (sections.length) {
|
|
|
|
styles[id] = sections;
|
|
|
|
} else {
|
|
|
|
delete styles[id];
|
|
|
|
styles.length--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-04-11 10:51:40 +00:00
|
|
|
function cleanupCachedFilters({force = false} = {}) {
|
|
|
|
if (!force) {
|
2017-04-13 16:44:43 +00:00
|
|
|
debounce(cleanupCachedFilters, 1000, {force: true});
|
2017-04-11 10:51:40 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
const size = cachedStyles.filters.size;
|
|
|
|
const oldestHit = cachedStyles.filters.values().next().value.lastHit;
|
|
|
|
const now = Date.now();
|
|
|
|
const timeSpan = now - oldestHit;
|
|
|
|
const recencyWeight = 5 / size;
|
2017-08-21 18:58:24 +00:00
|
|
|
const hitWeight = 1 / 4; // we make ~4 hits per URL
|
2017-04-11 10:51:40 +00:00
|
|
|
const lastHitWeight = 10;
|
|
|
|
// delete the oldest 10%
|
|
|
|
[...cachedStyles.filters.entries()]
|
|
|
|
.map(([id, v], index) => ({
|
|
|
|
id,
|
|
|
|
weight:
|
|
|
|
index * recencyWeight +
|
|
|
|
v.hits * hitWeight +
|
|
|
|
(v.lastHit - oldestHit) / timeSpan * lastHitWeight,
|
|
|
|
}))
|
|
|
|
.sort((a, b) => a.weight - b.weight)
|
|
|
|
.slice(0, size / 10 + 1)
|
|
|
|
.forEach(({id}) => cachedStyles.filters.delete(id));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function getDomains(url) {
|
2017-08-26 04:57:52 +00:00
|
|
|
let d = /.*?:\/*([^/:]+)|$/.exec(url)[1];
|
|
|
|
if (!d || url.startsWith('file:')) {
|
2017-04-11 10:51:40 +00:00
|
|
|
return [];
|
|
|
|
}
|
|
|
|
const domains = [d];
|
2017-07-16 18:02:00 +00:00
|
|
|
while (d.indexOf('.') !== -1) {
|
2017-04-11 10:51:40 +00:00
|
|
|
d = d.substring(d.indexOf('.') + 1);
|
|
|
|
domains.push(d);
|
|
|
|
}
|
|
|
|
return domains;
|
|
|
|
}
|
2017-04-23 12:19:18 +00:00
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
2017-09-03 20:52:06 +00:00
|
|
|
function handleCssTransitionBug({tabId, frameId, url, styles}) {
|
2017-09-03 08:12:18 +00:00
|
|
|
for (let id in styles) {
|
|
|
|
id |= 0;
|
|
|
|
if (!id) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
let need = cachedStyles.needTransitionPatch.get(id);
|
|
|
|
if (need === false) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (need !== true) {
|
|
|
|
need = styles[id].some(sectionContainsTransitions);
|
|
|
|
cachedStyles.needTransitionPatch.set(id, need);
|
|
|
|
if (!need) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
2017-09-03 20:52:06 +00:00
|
|
|
if (FIREFOX && !url.startsWith(URLS.ownOrigin)) {
|
2017-09-03 08:12:18 +00:00
|
|
|
patchFirefox();
|
|
|
|
} else {
|
|
|
|
styles.needTransitionPatch = true;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
function patchFirefox() {
|
2017-09-29 23:32:43 +00:00
|
|
|
const options = {
|
2017-09-03 08:12:18 +00:00
|
|
|
frameId,
|
|
|
|
code: CSS_TRANSITION_SUPPRESSOR,
|
|
|
|
matchAboutBlank: true,
|
2017-09-29 23:32:43 +00:00
|
|
|
};
|
|
|
|
if (FIREFOX >= 53) {
|
|
|
|
options.cssOrigin = 'user';
|
|
|
|
}
|
|
|
|
browser.tabs.insertCSS(tabId, Object.assign(options, {
|
|
|
|
runAt: 'document_start',
|
|
|
|
})).then(() => setTimeout(() => {
|
|
|
|
browser.tabs.removeCSS(tabId, options).catch(ignoreChromeError);
|
2017-09-03 08:12:18 +00:00
|
|
|
})).catch(ignoreChromeError);
|
|
|
|
}
|
|
|
|
|
|
|
|
function sectionContainsTransitions(section) {
|
|
|
|
let code = section.code;
|
|
|
|
const firstTransition = code.indexOf('transition');
|
|
|
|
if (firstTransition < 0) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
const firstCmt = code.indexOf('/*');
|
|
|
|
// check the part before the first comment
|
|
|
|
if (firstCmt < 0 || firstTransition < firstCmt) {
|
|
|
|
if (quickCheckAround(code, firstTransition)) {
|
|
|
|
return true;
|
|
|
|
} else if (firstCmt < 0) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// check the rest
|
|
|
|
const lastCmt = code.lastIndexOf('*/');
|
|
|
|
if (lastCmt < firstCmt) {
|
|
|
|
// the comment is unclosed and we already checked the preceding part
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
let mid = code.slice(firstCmt, lastCmt + 2);
|
|
|
|
mid = mid.indexOf('*/') === mid.length - 2 ? '' : mid.replace(RX_CSS_COMMENTS, '');
|
|
|
|
code = mid + code.slice(lastCmt + 2);
|
|
|
|
return quickCheckAround(code) || RX_CSS_TRANSITION_DETECTOR.test(code);
|
|
|
|
}
|
|
|
|
|
|
|
|
function quickCheckAround(code, pos = code.indexOf('transition')) {
|
|
|
|
return RX_CSS_TRANSITION_DETECTOR.test(code.substr(Math.max(0, pos - 10), 50));
|
|
|
|
}
|
|
|
|
}
|
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}) {
|
|
|
|
const results = [];
|
|
|
|
for (const id of ids) {
|
|
|
|
const style = cachedStyles.byId.get(id);
|
|
|
|
if (!style) continue;
|
|
|
|
// make sure all regexps are compiled
|
|
|
|
const rxCache = cachedStyles.regexps;
|
|
|
|
let hasRegExp = false;
|
|
|
|
for (const section of style.sections) {
|
|
|
|
for (const regexp of section.regexps) {
|
|
|
|
hasRegExp = true;
|
|
|
|
for (let pass = 1; pass <= 2; pass++) {
|
|
|
|
const cacheKey = pass === 1 ? regexp : SLOPPY_REGEXP_PREFIX + regexp;
|
|
|
|
if (!rxCache.has(cacheKey)) {
|
|
|
|
// according to CSS4 @document specification the entire URL must match
|
|
|
|
const anchored = pass === 1 ? '^(?:' + regexp + ')$' : '^' + regexp + '$';
|
|
|
|
// create in the bg context to avoid leaking of "dead objects"
|
|
|
|
const rx = tryRegExp(anchored);
|
|
|
|
rxCache.set(cacheKey, rx || false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!hasRegExp) continue;
|
|
|
|
const applied = getApplicableSections({style, matchUrl});
|
|
|
|
const wannabe = getApplicableSections({style, matchUrl, strictRegexp: false});
|
|
|
|
results.push({
|
|
|
|
id,
|
|
|
|
applied,
|
|
|
|
skipped: wannabe.length - applied.length,
|
|
|
|
hasInvalidRegexps: wannabe.some(({regexps}) => regexps.some(rx => !rxCache.has(rx))),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return results;
|
|
|
|
}
|