2017-04-11 10:51:40 +00:00
|
|
|
/* global cachedStyles: true */
|
2017-03-26 02:30:59 +00:00
|
|
|
'use strict';
|
|
|
|
|
2017-04-11 10:51:40 +00:00
|
|
|
const RX_NAMESPACE = new RegExp([/[\s\r\n]*/,
|
|
|
|
/(@namespace[\s\r\n]+(?:[^\s\r\n]+[\s\r\n]+)?url\(http:\/\/.*?\);)/,
|
|
|
|
/[\s\r\n]*/].map(rx => rx.source).join(''), 'g');
|
|
|
|
const RX_CSS_COMMENTS = /\/\*[\s\S]*?\*\//g;
|
|
|
|
const SLOPPY_REGEXP_PREFIX = '\0';
|
|
|
|
|
|
|
|
// Note, only 'var'-declared variables are visible from another extension page
|
|
|
|
// eslint-disable-next-line no-var
|
|
|
|
var cachedStyles = {
|
2017-04-13 16:44:43 +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
|
|
|
|
emptyCode: new Map(), // entire code is comments/whitespace/@namespace
|
2017-04-11 10:51:40 +00:00
|
|
|
mutex: {
|
2017-04-13 16:44:43 +00:00
|
|
|
inProgress: false, // while getStyles() is reading IndexedDB all subsequent calls
|
|
|
|
onDone: [], // to getStyles() are queued and resolved when the first one finishes
|
2017-04-11 10:51:40 +00:00
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2015-01-30 17:07:24 +00:00
|
|
|
function getDatabase(ready, error) {
|
2017-03-26 02:30:59 +00:00
|
|
|
const dbOpenRequest = window.indexedDB.open('stylish', 2);
|
|
|
|
dbOpenRequest.onsuccess = event => {
|
|
|
|
ready(event.target.result);
|
|
|
|
};
|
|
|
|
dbOpenRequest.onerror = event => {
|
|
|
|
console.warn(event.target.errorCode);
|
|
|
|
if (error) {
|
|
|
|
error(event);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
dbOpenRequest.onupgradeneeded = event => {
|
|
|
|
if (event.oldVersion == 0) {
|
|
|
|
event.target.result.createObjectStore('styles', {
|
|
|
|
keyPath: 'id',
|
|
|
|
autoIncrement: true,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
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
|
|
|
|
2016-03-07 02:27:17 +00:00
|
|
|
function getStyles(options, callback) {
|
2017-03-26 02:30:59 +00:00
|
|
|
if (cachedStyles.list) {
|
|
|
|
callback(filterStyles(options));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (cachedStyles.mutex.inProgress) {
|
|
|
|
cachedStyles.mutex.onDone.push({options, callback});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
cachedStyles.mutex.inProgress = true;
|
|
|
|
|
|
|
|
getDatabase(db => {
|
|
|
|
const tx = db.transaction(['styles'], 'readonly');
|
|
|
|
const os = tx.objectStore('styles');
|
|
|
|
os.getAll().onsuccess = event => {
|
|
|
|
cachedStyles.list = event.target.result || [];
|
|
|
|
cachedStyles.byId.clear();
|
|
|
|
for (const style of cachedStyles.list) {
|
2017-03-30 23:18:41 +00:00
|
|
|
cachedStyles.byId.set(style.id, style);
|
|
|
|
compileStyleRegExps({style});
|
2017-03-26 02:30:59 +00:00
|
|
|
}
|
2017-03-26 07:19:47 +00:00
|
|
|
callback(filterStyles(options));
|
2017-03-26 02:30:59 +00:00
|
|
|
|
|
|
|
cachedStyles.mutex.inProgress = false;
|
|
|
|
for (const {options, callback} of cachedStyles.mutex.onDone) {
|
|
|
|
callback(filterStyles(options));
|
|
|
|
}
|
|
|
|
cachedStyles.mutex.onDone = [];
|
|
|
|
};
|
|
|
|
}, null);
|
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({
|
|
|
|
enabled,
|
|
|
|
url = null,
|
|
|
|
id = null,
|
|
|
|
matchUrl = null,
|
|
|
|
asHash = null,
|
|
|
|
strictRegexp = true, // used by the popup to detect bad regexps
|
|
|
|
} = {}) {
|
|
|
|
enabled = fixBoolean(enabled);
|
|
|
|
id = id === null ? null : Number(id);
|
2017-03-26 02:30:59 +00:00
|
|
|
|
|
|
|
if (enabled === null
|
2017-04-11 10:51:40 +00:00
|
|
|
&& url === null
|
|
|
|
&& id === null
|
|
|
|
&& matchUrl === null
|
|
|
|
&& asHash != true) {
|
2017-03-30 23:18:41 +00:00
|
|
|
return cachedStyles.list;
|
2017-03-26 02:30:59 +00:00
|
|
|
}
|
|
|
|
const disableAll = asHash && prefs.get('disableAll', false);
|
|
|
|
|
2017-04-12 13:56:41 +00:00
|
|
|
if (matchUrl && matchUrl.startsWith(URLS.chromeWebStore)) {
|
|
|
|
// CWS cannot be scripted in chromium, see ChromeExtensionsClient::IsScriptableURL
|
|
|
|
// https://cs.chromium.org/chromium/src/chrome/common/extensions/chrome_extensions_client.cc
|
|
|
|
return asHash ? {} : [];
|
|
|
|
}
|
|
|
|
|
2017-03-26 02:30:59 +00:00
|
|
|
// add \t after url to prevent collisions (not sure it can actually happen though)
|
2017-03-30 23:18:41 +00:00
|
|
|
const cacheKey = ' ' + enabled + url + '\t' + id + matchUrl + '\t' + asHash + strictRegexp;
|
2017-03-26 02:30:59 +00:00
|
|
|
const cached = cachedStyles.filters.get(cacheKey);
|
|
|
|
if (cached) {
|
|
|
|
cached.hits++;
|
|
|
|
cached.lastHit = Date.now();
|
|
|
|
|
|
|
|
return asHash
|
|
|
|
? Object.assign({disableAll}, cached.styles)
|
|
|
|
: cached.styles;
|
|
|
|
}
|
|
|
|
|
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-03-26 02:30:59 +00:00
|
|
|
const filtered = asHash ? {} : [];
|
|
|
|
if (!styles) {
|
|
|
|
// may happen when users [accidentally] reopen an old URL
|
|
|
|
// of edit.html with a non-existent style id parameter
|
|
|
|
return filtered;
|
|
|
|
}
|
2017-03-30 23:18:41 +00:00
|
|
|
const needSections = asHash || matchUrl !== null;
|
|
|
|
|
2017-03-26 02:30:59 +00:00
|
|
|
for (let i = 0, style; (style = styles[i]); i++) {
|
|
|
|
if ((enabled === null || style.enabled == enabled)
|
2017-04-13 16:44:43 +00:00
|
|
|
&& (url === null || style.url == url)
|
|
|
|
&& (id === null || style.id == id)) {
|
2017-03-30 23:18:41 +00:00
|
|
|
const sections = needSections &&
|
|
|
|
getApplicableSections({style, matchUrl, strictRegexp, stopOnFirst: !asHash});
|
2017-03-26 02:30:59 +00:00
|
|
|
if (asHash) {
|
|
|
|
if (sections.length) {
|
|
|
|
filtered[style.id] = sections;
|
|
|
|
}
|
|
|
|
} else if (matchUrl === null || sections.length) {
|
|
|
|
filtered.push(style);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
cachedStyles.filters.set(cacheKey, {
|
|
|
|
styles: filtered,
|
|
|
|
lastHit: Date.now(),
|
|
|
|
hits: 1,
|
|
|
|
});
|
|
|
|
if (cachedStyles.filters.size > 10000) {
|
|
|
|
cleanupCachedFilters();
|
|
|
|
}
|
|
|
|
return asHash
|
|
|
|
? Object.assign({disableAll}, 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-03-26 02:30:59 +00:00
|
|
|
return new Promise(resolve => {
|
|
|
|
getDatabase(db => {
|
|
|
|
const tx = db.transaction(['styles'], 'readwrite');
|
|
|
|
const os = tx.objectStore('styles');
|
|
|
|
|
|
|
|
const id = style.id !== undefined && style.id !== null ? Number(style.id) : null;
|
|
|
|
const reason = style.reason;
|
|
|
|
const notify = style.notify !== false;
|
|
|
|
delete style.method;
|
|
|
|
delete style.reason;
|
|
|
|
delete style.notify;
|
|
|
|
if (!style.name) {
|
|
|
|
delete style.name;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (id !== null) {
|
2017-04-13 16:44:43 +00:00
|
|
|
// Update or create
|
2017-03-26 02:30:59 +00:00
|
|
|
style.id = id;
|
|
|
|
os.get(id).onsuccess = eventGet => {
|
|
|
|
const existed = Boolean(eventGet.target.result);
|
|
|
|
const oldStyle = Object.assign({}, eventGet.target.result);
|
|
|
|
const codeIsUpdated = 'sections' in style && !styleSectionsEqual(style, oldStyle);
|
2017-04-13 16:44:43 +00:00
|
|
|
write(Object.assign(oldStyle, style), {existed, codeIsUpdated});
|
2017-03-26 02:30:59 +00:00
|
|
|
};
|
2017-04-13 16:44:43 +00:00
|
|
|
} else {
|
|
|
|
// Create
|
|
|
|
delete style.id;
|
|
|
|
write(Object.assign({
|
|
|
|
// Set optional things if they're undefined
|
|
|
|
enabled: true,
|
|
|
|
updateUrl: null,
|
|
|
|
md5Url: null,
|
|
|
|
url: null,
|
|
|
|
originalMd5: null,
|
|
|
|
}, style));
|
2017-03-25 02:35:54 +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 write(style, {existed, codeIsUpdated} = {}) {
|
|
|
|
style.sections = (style.sections || []).map(section =>
|
|
|
|
Object.assign({
|
|
|
|
urls: [],
|
|
|
|
urlPrefixes: [],
|
|
|
|
domains: [],
|
|
|
|
regexps: [],
|
|
|
|
}, section)
|
|
|
|
);
|
|
|
|
os.put(style).onsuccess = event => {
|
|
|
|
style.id = style.id || event.target.result;
|
|
|
|
invalidateCache(existed ? {updated: style} : {added: style});
|
|
|
|
compileStyleRegExps({style});
|
|
|
|
if (notify) {
|
|
|
|
notifyAllTabs({
|
|
|
|
method: existed ? 'styleUpdated' : 'styleAdded',
|
|
|
|
style, codeIsUpdated, reason,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
resolve(style);
|
|
|
|
};
|
|
|
|
}
|
2017-03-26 02:30:59 +00:00
|
|
|
});
|
|
|
|
});
|
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-03-26 02:30:59 +00:00
|
|
|
return new Promise(resolve =>
|
|
|
|
getDatabase(db => {
|
|
|
|
const tx = db.transaction(['styles'], 'readwrite');
|
|
|
|
const os = tx.objectStore('styles');
|
|
|
|
os.delete(Number(id)).onsuccess = () => {
|
2017-04-13 16:44:43 +00:00
|
|
|
invalidateCache({deletedId: id});
|
2017-03-26 02:30:59 +00:00
|
|
|
if (notify) {
|
|
|
|
notifyAllTabs({method: 'styleDeleted', id});
|
|
|
|
}
|
|
|
|
resolve(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-03-30 23:18:41 +00:00
|
|
|
function getApplicableSections({style, matchUrl, strictRegexp = true, stopOnFirst}) {
|
2017-04-13 21:44:23 +00:00
|
|
|
if (!matchUrl.startsWith('http')
|
|
|
|
&& !matchUrl.startsWith('ftp')
|
|
|
|
&& !matchUrl.startsWith('file')
|
|
|
|
&& !matchUrl.startsWith(URLS.ownOrigin)) {
|
|
|
|
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;
|
|
|
|
if ((!urls.length && !urlPrefixes.length && !domains.length && !regexps.length
|
|
|
|
|| urls.length && urls.indexOf(matchUrl) >= 0
|
|
|
|
|| urlPrefixes.length && arraySomeIsPrefix(urlPrefixes, matchUrl)
|
|
|
|
|| domains.length && arraySomeIn(cachedStyles.urlDomains.get(matchUrl) || getDomains(matchUrl), domains)
|
|
|
|
|| regexps.length && arraySomeMatches(regexps, matchUrl, strictRegexp)
|
|
|
|
) && !styleCodeEmpty(code)) {
|
|
|
|
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++) {
|
|
|
|
const cacheKey = pass == 1 ? regexp : SLOPPY_REGEXP_PREFIX + regexp;
|
|
|
|
let rx = cachedStyles.regexps.get(cacheKey);
|
|
|
|
if (rx == false) {
|
|
|
|
// invalid regexp
|
|
|
|
break;
|
2017-03-30 23:18:41 +00:00
|
|
|
}
|
2017-04-13 21:44:23 +00:00
|
|
|
if (!rx) {
|
|
|
|
const anchored = pass == 1 ? '^(?:' + regexp + ')$' : '^' + regexp + '$';
|
|
|
|
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) {
|
|
|
|
// Collect the section if not empty or namespace-only.
|
|
|
|
// We don't check long code as it's slow both for emptyCode declared as Object
|
|
|
|
// and as Map in case the string is not the same reference used to add the item
|
|
|
|
let isEmpty = code !== null &&
|
|
|
|
code.length < 1000 &&
|
|
|
|
cachedStyles.emptyCode.get(code);
|
|
|
|
if (isEmpty !== undefined) {
|
|
|
|
return isEmpty;
|
|
|
|
}
|
|
|
|
isEmpty = !code || !code.trim()
|
|
|
|
|| code.indexOf('@namespace') >= 0
|
|
|
|
&& code.replace(RX_CSS_COMMENTS, '').replace(RX_NAMESPACE, '').trim() == '';
|
|
|
|
cachedStyles.emptyCode.set(code, isEmpty);
|
|
|
|
return isEmpty;
|
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 21:49:18 +00:00
|
|
|
function styleSectionsEqual({sections: a}, {sections: b}) {
|
|
|
|
if (!a || !b) {
|
2017-03-26 02:30:59 +00:00
|
|
|
return undefined;
|
|
|
|
}
|
2017-04-13 21:49:18 +00:00
|
|
|
if (a.length != b.length) {
|
2017-03-26 02:30:59 +00:00
|
|
|
return false;
|
|
|
|
}
|
2017-04-13 21:49:18 +00:00
|
|
|
const checkedInB = [];
|
|
|
|
return a.every(sectionA => b.some(sectionB => {
|
|
|
|
if (!checkedInB.includes(sectionB) && propertiesEqual(sectionA, sectionB)) {
|
|
|
|
checkedInB.push(sectionB);
|
|
|
|
return true;
|
2017-03-26 02:30:59 +00:00
|
|
|
}
|
2017-04-13 21:49:18 +00:00
|
|
|
}));
|
|
|
|
|
|
|
|
function propertiesEqual(secA, secB) {
|
|
|
|
for (const name of ['urlPrefixes', 'urls', 'domains', 'regexps']) {
|
|
|
|
if (!equalOrEmpty(secA[name], secB[name], 'every', arrayMirrors)) {
|
|
|
|
return false;
|
2017-03-26 02:30:59 +00:00
|
|
|
}
|
|
|
|
}
|
2017-04-13 21:49:18 +00:00
|
|
|
return equalOrEmpty(secA.code, secB.code, 'substr', (a, b) => a == b);
|
|
|
|
}
|
|
|
|
|
|
|
|
function equalOrEmpty(a, b, telltale, comparator) {
|
|
|
|
const typeA = a && typeof a[telltale] == 'function';
|
|
|
|
const typeB = b && typeof b[telltale] == 'function';
|
|
|
|
return (
|
|
|
|
(a === null || a === undefined || (typeA && !a.length)) &&
|
|
|
|
(b === null || b === undefined || (typeB && !b.length))
|
|
|
|
) || typeA && typeB && a.length == b.length && comparator(a, b);
|
|
|
|
}
|
|
|
|
|
|
|
|
function arrayMirrors(array1, array2) {
|
|
|
|
for (const el of array1) {
|
|
|
|
if (array2.indexOf(el) < 0) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (const el of array2) {
|
|
|
|
if (array1.indexOf(el) < 0) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
2017-03-26 02:30:59 +00:00
|
|
|
}
|
2017-03-15 11:37:29 +00:00
|
|
|
}
|
2017-03-26 07:19:47 +00:00
|
|
|
|
|
|
|
|
2017-03-30 23:18:41 +00:00
|
|
|
function compileStyleRegExps({style, compileAll}) {
|
2017-03-26 07:19:47 +00:00
|
|
|
const t0 = performance.now();
|
|
|
|
for (const section of style.sections || []) {
|
|
|
|
for (const regexp of section.regexps) {
|
2017-03-30 23:18:41 +00:00
|
|
|
for (let pass = 1; pass <= (compileAll ? 2 : 1); pass++) {
|
|
|
|
const cacheKey = pass == 1 ? regexp : SLOPPY_REGEXP_PREFIX + regexp;
|
|
|
|
if (cachedStyles.regexps.has(cacheKey)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
// according to CSS4 @document specification the entire URL must match
|
|
|
|
const anchored = pass == 1 ? '^(?:' + regexp + ')$' : '^' + regexp + '$';
|
|
|
|
const rx = tryRegExp(anchored);
|
|
|
|
cachedStyles.regexps.set(cacheKey, rx || false);
|
|
|
|
if (!compileAll && performance.now() - t0 > 100) {
|
|
|
|
return;
|
|
|
|
}
|
2017-03-26 07:19:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-04-11 10:51:40 +00:00
|
|
|
|
|
|
|
|
2017-04-13 16:44:43 +00:00
|
|
|
function invalidateCache({added, updated, deletedId} = {}) {
|
2017-04-11 10:51:40 +00:00
|
|
|
// prevent double-add on echoed invalidation
|
|
|
|
const cached = added && cachedStyles.byId.get(added.id);
|
|
|
|
if (cached) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!cachedStyles.list) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (updated) {
|
|
|
|
const cached = cachedStyles.byId.get(updated.id);
|
|
|
|
if (cached) {
|
|
|
|
Object.assign(cached, updated);
|
|
|
|
}
|
|
|
|
cachedStyles.filters.clear();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (added) {
|
|
|
|
cachedStyles.list.push(added);
|
|
|
|
cachedStyles.byId.set(added.id, added);
|
|
|
|
cachedStyles.filters.clear();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (deletedId != undefined) {
|
|
|
|
const deletedStyle = (cachedStyles.byId.get(deletedId) || {}).style;
|
|
|
|
if (deletedStyle) {
|
|
|
|
const cachedIndex = cachedStyles.list.indexOf(deletedStyle);
|
|
|
|
cachedStyles.list.splice(cachedIndex, 1);
|
|
|
|
cachedStyles.byId.delete(deletedId);
|
|
|
|
cachedStyles.filters.clear();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
cachedStyles.list = null;
|
|
|
|
cachedStyles.filters.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
const hitWeight = 1 / 4; // we make ~4 hits per URL
|
|
|
|
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 reportError(...args) {
|
|
|
|
for (const arg of args) {
|
|
|
|
if ('message' in arg) {
|
|
|
|
console.log(arg.message);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function fixBoolean(b) {
|
|
|
|
if (typeof b != 'undefined') {
|
|
|
|
return b != 'false';
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function getDomains(url) {
|
|
|
|
if (url.indexOf('file:') == 0) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
let d = /.*?:\/*([^/:]+)/.exec(url)[1];
|
|
|
|
const domains = [d];
|
|
|
|
while (d.indexOf('.') != -1) {
|
|
|
|
d = d.substring(d.indexOf('.') + 1);
|
|
|
|
domains.push(d);
|
|
|
|
}
|
|
|
|
return domains;
|
|
|
|
}
|