2017-03-26 02:30:59 +00:00
|
|
|
/* global cachedStyles: true, prefs: true, contextMenus: false */
|
|
|
|
/* global handleUpdate, handleDelete */
|
|
|
|
'use strict';
|
|
|
|
|
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
|
|
|
|
2017-03-28 08:24:31 +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;
|
2017-03-30 23:18:41 +00:00
|
|
|
const SLOPPY_REGEXP_PREFIX = '\0';
|
2017-03-28 08:24:31 +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
|
|
|
// Let manage/popup/edit reuse background page variables
|
2017-03-26 02:30:59 +00:00
|
|
|
// Note, only 'var'-declared variables are visible from another extension page
|
|
|
|
// eslint-disable-next-line no-var
|
|
|
|
var cachedStyles, prefs;
|
|
|
|
(() => {
|
|
|
|
const bg = chrome.extension.getBackgroundPage();
|
|
|
|
cachedStyles = bg && bg.cachedStyles || {
|
|
|
|
bg,
|
|
|
|
list: null,
|
|
|
|
byId: new Map(),
|
|
|
|
filters: new Map(),
|
2017-03-26 07:19:47 +00:00
|
|
|
regexps: new Map(),
|
2017-03-26 07:42:13 +00:00
|
|
|
urlDomains: new Map(),
|
2017-03-30 23:18:41 +00:00
|
|
|
emptyCode: new Map(), // entire code is comments/whitespace/@namespace
|
2017-03-26 02:30:59 +00:00
|
|
|
mutex: {
|
|
|
|
inProgress: false,
|
|
|
|
onDone: [],
|
|
|
|
},
|
|
|
|
};
|
|
|
|
prefs = bg && bg.prefs;
|
|
|
|
})();
|
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
|
|
|
|
|
|
|
|
|
|
|
// in case Chrome haven't yet loaded the bg page and displays our page like edit/manage
|
|
|
|
function getStylesSafe(options) {
|
2017-03-26 02:30:59 +00:00
|
|
|
return new Promise(resolve => {
|
|
|
|
if (cachedStyles.bg) {
|
|
|
|
getStyles(options, resolve);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
chrome.runtime.sendMessage(Object.assign({method: 'getStyles'}, options), styles => {
|
|
|
|
if (!styles) {
|
|
|
|
resolve(getStylesSafe(options));
|
|
|
|
} else {
|
|
|
|
cachedStyles = chrome.extension.getBackgroundPage().cachedStyles;
|
|
|
|
resolve(styles);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
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;
|
|
|
|
|
|
|
|
//const t0 = performance.now();
|
|
|
|
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-30 23:18:41 +00:00
|
|
|
//console.debug('%s getStyles %s, invoking cached callbacks: %o', (performance.now() - t0).toFixed(1), JSON.stringify(options), cachedStyles.mutex.onDone.map(e => JSON.stringify(e.options))); // eslint-disable-line max-len
|
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
|
|
|
|
|
|
|
function getStyleWithNoCode(style) {
|
2017-03-26 02:30:59 +00:00
|
|
|
const stripped = Object.assign({}, style, {sections: []});
|
|
|
|
for (const section of style.sections) {
|
|
|
|
stripped.sections.push(Object.assign({}, section, {code: null}));
|
|
|
|
}
|
|
|
|
return stripped;
|
2016-03-19 23:27:28 +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
|
|
|
function invalidateCache(andNotify, {added, updated, deletedId} = {}) {
|
2017-03-26 02:30:59 +00:00
|
|
|
// prevent double-add on echoed invalidation
|
|
|
|
const cached = added && cachedStyles.byId.get(added.id);
|
|
|
|
if (cached) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (andNotify) {
|
|
|
|
chrome.runtime.sendMessage({method: 'invalidateCache', added, updated, deletedId});
|
|
|
|
}
|
|
|
|
if (!cachedStyles.list) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (updated) {
|
|
|
|
const cached = cachedStyles.byId.get(updated.id);
|
|
|
|
if (cached) {
|
2017-03-30 23:18:41 +00:00
|
|
|
Object.assign(cached, updated);
|
|
|
|
//console.debug('cache: updated', updated);
|
2017-03-26 02:30:59 +00:00
|
|
|
}
|
|
|
|
cachedStyles.filters.clear();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (added) {
|
|
|
|
cachedStyles.list.push(added);
|
2017-03-30 23:18:41 +00:00
|
|
|
cachedStyles.byId.set(added.id, added);
|
|
|
|
//console.debug('cache: added', added);
|
2017-03-26 02:30:59 +00:00
|
|
|
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);
|
2017-03-30 23:18:41 +00:00
|
|
|
//console.debug('cache: deleted', deletedStyle);
|
2017-03-26 02:30:59 +00:00
|
|
|
cachedStyles.filters.clear();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
cachedStyles.list = null;
|
2017-03-30 23:18:41 +00:00
|
|
|
//console.debug('cache cleared');
|
2017-03-26 02:30:59 +00:00
|
|
|
cachedStyles.filters.clear();
|
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
|
|
|
|
} = {}) {
|
2017-03-26 02:30:59 +00:00
|
|
|
//const t0 = performance.now();
|
2017-03-30 23:18:41 +00:00
|
|
|
enabled = fixBoolean(enabled);
|
|
|
|
id = id === null ? null : Number(id);
|
2017-03-26 02:30:59 +00:00
|
|
|
|
|
|
|
if (enabled === null
|
|
|
|
&& url === null
|
|
|
|
&& id === null
|
|
|
|
&& matchUrl === null
|
|
|
|
&& asHash != true) {
|
2017-03-30 23:18:41 +00:00
|
|
|
//console.debug('%c%s filterStyles SKIPPED LOOP %s', 'color:gray', (performance.now() - t0).toFixed(1), enabled, id, asHash, strictRegexp, matchUrl); // eslint-disable-line max-len
|
|
|
|
return cachedStyles.list;
|
2017-03-26 02:30:59 +00:00
|
|
|
}
|
|
|
|
// silence the inapplicable warning for async code
|
|
|
|
// eslint-disable-next-line no-use-before-define
|
|
|
|
const disableAll = asHash && prefs.get('disableAll', false);
|
|
|
|
|
|
|
|
// 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) {
|
2017-03-30 23:18:41 +00:00
|
|
|
//console.debug('%c%s filterStyles REUSED RESPONSE %s', 'color:gray', (performance.now() - t0).toFixed(1), enabled, id, asHash, strictRegexp, matchUrl); // eslint-disable-line max-len
|
2017-03-26 02:30:59 +00:00
|
|
|
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)
|
|
|
|
&& (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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-03-30 23:18:41 +00:00
|
|
|
//console.debug('%s filterStyles %s', (performance.now() - t0).toFixed(1), enabled, id, asHash, strictRegexp, matchUrl); // eslint-disable-line max-len
|
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();
|
|
|
|
}
|
|
|
|
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-22 05:12:11 +00:00
|
|
|
function cleanupCachedFilters({force = false} = {}) {
|
2017-03-26 02:30:59 +00:00
|
|
|
if (!force) {
|
|
|
|
// sliding timer for 1 second
|
|
|
|
clearTimeout(cleanupCachedFilters.timeout);
|
|
|
|
cleanupCachedFilters.timeout = setTimeout(cleanupCachedFilters, 1000, {force: true});
|
|
|
|
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));
|
|
|
|
cleanupCachedFilters.timeout = 0;
|
2017-03-22 05:12:11 +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;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update
|
|
|
|
if (id !== null) {
|
|
|
|
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);
|
|
|
|
style = Object.assign(oldStyle, style);
|
|
|
|
addMissingStyleTargets(style);
|
|
|
|
os.put(style).onsuccess = eventPut => {
|
|
|
|
style.id = style.id || eventPut.target.result;
|
|
|
|
invalidateCache(notify, existed ? {updated: style} : {added: style});
|
2017-03-30 23:18:41 +00:00
|
|
|
compileStyleRegExps({style});
|
2017-03-26 02:30:59 +00:00
|
|
|
if (notify) {
|
|
|
|
notifyAllTabs({
|
|
|
|
method: existed ? 'styleUpdated' : 'styleAdded',
|
|
|
|
style, codeIsUpdated, reason,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
if (typeof handleUpdate != 'undefined') {
|
|
|
|
handleUpdate(style, {reason});
|
|
|
|
}
|
|
|
|
resolve(style);
|
|
|
|
};
|
|
|
|
};
|
|
|
|
return;
|
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-03-26 02:30:59 +00:00
|
|
|
// Create
|
|
|
|
delete style.id;
|
|
|
|
style = Object.assign({
|
|
|
|
// Set optional things if they're undefined
|
|
|
|
enabled: true,
|
|
|
|
updateUrl: null,
|
|
|
|
md5Url: null,
|
|
|
|
url: null,
|
|
|
|
originalMd5: null,
|
|
|
|
}, style);
|
|
|
|
addMissingStyleTargets(style);
|
|
|
|
os.add(style).onsuccess = event => {
|
|
|
|
// Give it the ID that was generated
|
|
|
|
style.id = event.target.result;
|
|
|
|
invalidateCache(notify, {added: style});
|
2017-03-30 23:18:41 +00:00
|
|
|
compileStyleRegExps({style});
|
2017-03-26 02:30:59 +00:00
|
|
|
if (notify) {
|
|
|
|
notifyAllTabs({method: 'styleAdded', style, reason});
|
|
|
|
}
|
|
|
|
if (typeof handleUpdate != 'undefined') {
|
|
|
|
handleUpdate(style, {reason});
|
|
|
|
}
|
|
|
|
resolve(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
|
|
|
|
|
|
|
function addMissingStyleTargets(style) {
|
2017-03-26 02:30:59 +00:00
|
|
|
style.sections = (style.sections || []).map(section =>
|
|
|
|
Object.assign({
|
|
|
|
urls: [],
|
|
|
|
urlPrefixes: [],
|
|
|
|
domains: [],
|
|
|
|
regexps: [],
|
|
|
|
}, section)
|
|
|
|
);
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-01-30 17:07:24 +00:00
|
|
|
function enableStyle(id, enabled) {
|
2017-03-26 02:30:59 +00:00
|
|
|
return saveStyle({id, enabled});
|
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-24 02:51:44 +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 = () => {
|
|
|
|
invalidateCache(notify, {deletedId: id});
|
|
|
|
if (notify) {
|
|
|
|
notifyAllTabs({method: 'styleDeleted', id});
|
|
|
|
}
|
|
|
|
if (typeof handleDelete != 'undefined') {
|
|
|
|
handleDelete(id);
|
2017-03-25 02:35:54 +00:00
|
|
|
}
|
2017-03-26 02:30:59 +00:00
|
|
|
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-26 02:30:59 +00:00
|
|
|
function reportError(...args) {
|
|
|
|
for (const arg of args) {
|
|
|
|
if ('message' in arg) {
|
|
|
|
console.log(arg.message);
|
|
|
|
}
|
|
|
|
}
|
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
|
|
|
|
2016-03-07 02:27:17 +00:00
|
|
|
function fixBoolean(b) {
|
2017-03-26 02:30:59 +00:00
|
|
|
if (typeof b != 'undefined') {
|
|
|
|
return b != 'false';
|
|
|
|
}
|
|
|
|
return null;
|
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
|
|
|
|
2015-01-30 17:07:24 +00:00
|
|
|
function getDomains(url) {
|
2017-03-26 02:30:59 +00:00
|
|
|
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;
|
2015-01-30 17:07:24 +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
|
|
|
|
2015-02-09 04:02:08 +00:00
|
|
|
function getType(o) {
|
2017-03-26 02:30:59 +00:00
|
|
|
if (typeof o == 'undefined' || typeof o == 'string') {
|
|
|
|
return typeof o;
|
|
|
|
}
|
|
|
|
// with the persistent cachedStyles the Array reference is usually different
|
|
|
|
// so let's check for e.g. type of 'every' which is only present on arrays
|
|
|
|
// (in the context of our extension)
|
|
|
|
if (o instanceof Array || typeof o.every == 'function') {
|
|
|
|
return 'array';
|
|
|
|
}
|
|
|
|
console.warn('Unsupported type:', o);
|
|
|
|
return 'undefined';
|
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-03-30 23:18:41 +00:00
|
|
|
function getApplicableSections({style, matchUrl, strictRegexp = true, stopOnFirst}) {
|
|
|
|
//let t0 = 0;
|
2017-03-26 07:19:47 +00:00
|
|
|
const sections = [];
|
2017-03-28 08:24:31 +00:00
|
|
|
checkingSections:
|
2017-03-26 07:19:47 +00:00
|
|
|
for (const section of style.sections) {
|
2017-03-30 23:18:41 +00:00
|
|
|
andCollect:
|
|
|
|
do {
|
|
|
|
// only http, https, file, ftp, and chrome-extension://OWN_EXTENSION_ID allowed
|
|
|
|
if (!matchUrl.startsWith('http')
|
|
|
|
&& !matchUrl.startsWith('ftp')
|
|
|
|
&& !matchUrl.startsWith('file')
|
|
|
|
&& !matchUrl.startsWith(OWN_ORIGIN)) {
|
2017-03-28 08:24:31 +00:00
|
|
|
continue checkingSections;
|
|
|
|
}
|
2017-03-30 23:18:41 +00:00
|
|
|
if (section.urls.length == 0
|
|
|
|
&& section.domains.length == 0
|
|
|
|
&& section.urlPrefixes.length == 0
|
|
|
|
&& section.regexps.length == 0) {
|
|
|
|
break andCollect;
|
2017-03-28 08:24:31 +00:00
|
|
|
}
|
2017-03-30 23:18:41 +00:00
|
|
|
if (section.urls.indexOf(matchUrl) != -1) {
|
|
|
|
break andCollect;
|
2017-03-26 07:19:47 +00:00
|
|
|
}
|
2017-03-30 23:18:41 +00:00
|
|
|
for (const urlPrefix of section.urlPrefixes) {
|
|
|
|
if (matchUrl.startsWith(urlPrefix)) {
|
|
|
|
break andCollect;
|
2017-03-28 08:24:31 +00:00
|
|
|
}
|
|
|
|
}
|
2017-03-30 23:18:41 +00:00
|
|
|
if (section.domains.length) {
|
|
|
|
const urlDomains = cachedStyles.urlDomains.get(matchUrl) || getDomains(matchUrl);
|
|
|
|
for (const domain of urlDomains) {
|
|
|
|
if (section.domains.indexOf(domain) != -1) {
|
|
|
|
break andCollect;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (const regexp of section.regexps) {
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
if (!rx) {
|
|
|
|
const anchored = pass == 1 ? '^(?:' + regexp + ')$' : '^' + regexp + '$';
|
|
|
|
rx = tryRegExp(anchored);
|
|
|
|
cachedStyles.regexps.set(cacheKey, rx || false);
|
|
|
|
if (!rx) {
|
|
|
|
// invalid regexp
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (rx.test(matchUrl)) {
|
|
|
|
break andCollect;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
continue checkingSections;
|
|
|
|
} while (0);
|
|
|
|
// 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
|
|
|
|
//const t0start = performance.now();
|
|
|
|
const code = section.code;
|
2017-03-31 08:46:18 +00:00
|
|
|
let isEmpty = code !== null && code.length < 1000 && cachedStyles.emptyCode.get(code);
|
2017-03-30 23:18:41 +00:00
|
|
|
if (isEmpty === undefined) {
|
|
|
|
isEmpty = !code || !code.trim()
|
|
|
|
|| code.indexOf('@namespace') >= 0
|
|
|
|
&& code.replace(RX_CSS_COMMENTS, '').replace(RX_NAMESPACE, '').trim() == '';
|
|
|
|
cachedStyles.emptyCode.set(code, isEmpty);
|
|
|
|
}
|
|
|
|
//t0 += performance.now() - t0start;
|
|
|
|
if (!isEmpty) {
|
|
|
|
sections.push(section);
|
|
|
|
if (stopOnFirst) {
|
|
|
|
//t0 >= 0.1 && console.debug('%s emptyCode', t0.toFixed(1)); // eslint-disable-line no-unused-expressions
|
|
|
|
return sections;
|
2017-03-28 08:24:31 +00:00
|
|
|
}
|
2017-03-26 10:05:05 +00:00
|
|
|
}
|
|
|
|
}
|
2017-03-30 23:18:41 +00:00
|
|
|
//t0 >= 0.1 && console.debug('%s emptyCode', t0.toFixed(1)); // eslint-disable-line no-unused-expressions
|
2017-03-28 08:24:31 +00:00
|
|
|
return sections;
|
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
|
|
|
|
2015-02-09 04:02:08 +00:00
|
|
|
function isCheckbox(el) {
|
2017-03-26 02:30:59 +00:00
|
|
|
return el.localName == 'input' && el.type == 'checkbox';
|
2015-02-09 04:02:08 +00:00
|
|
|
}
|
|
|
|
|
2017-03-26 07:42:13 +00:00
|
|
|
|
2016-03-07 02:27:17 +00:00
|
|
|
// js engine can't optimize the entire function if it contains try-catch
|
|
|
|
// so we should keep it isolated from normal code in a minimal wrapper
|
2017-03-26 02:30:59 +00:00
|
|
|
// Update: might get fixed in V8 TurboFan in the future
|
|
|
|
function runTryCatch(func, ...args) {
|
|
|
|
try {
|
|
|
|
return func(...args);
|
|
|
|
} catch (e) {}
|
2016-03-07 02:27:17 +00:00
|
|
|
}
|
|
|
|
|
2017-03-26 07:19:47 +00:00
|
|
|
|
|
|
|
function tryRegExp(regexp) {
|
|
|
|
try {
|
|
|
|
return new RegExp(regexp);
|
|
|
|
} catch (e) {}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-03-31 12:22:38 +00:00
|
|
|
function tryJSONparse(jsonString) {
|
|
|
|
try {
|
|
|
|
return JSON.parse(jsonString);
|
|
|
|
} catch (e) {}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-04-05 13:14:59 +00:00
|
|
|
function debounce(fn, delay, ...args) {
|
2017-03-31 08:46:18 +00:00
|
|
|
const timers = debounce.timers = debounce.timers || new Map();
|
|
|
|
debounce.run = debounce.run || ((fn, ...args) => {
|
|
|
|
timers.delete(fn);
|
|
|
|
fn(...args);
|
|
|
|
});
|
|
|
|
clearTimeout(timers.get(fn));
|
2017-04-05 13:14:59 +00:00
|
|
|
timers.set(fn, setTimeout(debounce.run, delay, fn, ...args));
|
2017-03-31 08:46:18 +00:00
|
|
|
}
|
2017-03-26 02:30:59 +00:00
|
|
|
|
2017-03-31 08:46:18 +00:00
|
|
|
|
|
|
|
prefs = prefs || new function Prefs() {
|
2017-03-26 02:30:59 +00:00
|
|
|
const defaults = {
|
|
|
|
'openEditInWindow': false, // new editor opens in a own browser window
|
|
|
|
'windowPosition': {}, // detached window position
|
|
|
|
'show-badge': true, // display text on popup menu icon
|
|
|
|
'disableAll': false, // boss key
|
|
|
|
|
|
|
|
'popup.breadcrumbs': true, // display 'New style' links as URL breadcrumbs
|
|
|
|
'popup.breadcrumbs.usePath': false, // use URL path for 'this URL'
|
|
|
|
'popup.enabledFirst': true, // display enabled styles before disabled styles
|
|
|
|
'popup.stylesFirst': true, // display enabled styles before disabled styles
|
|
|
|
|
|
|
|
'manage.onlyEnabled': false, // display only enabled styles
|
|
|
|
'manage.onlyEdited': false, // display only styles created locally
|
2017-04-05 13:14:59 +00:00
|
|
|
'manage.newUI': true, // use the new compact layout
|
|
|
|
'manage.newUI.favicons': true, // show favicons for the sites in applies-to
|
|
|
|
'manage.newUI.targets': 3, // max number of applies-to targets visible: 0 = none
|
2017-03-26 02:30:59 +00:00
|
|
|
|
|
|
|
'editor.options': {}, // CodeMirror.defaults.*
|
|
|
|
'editor.lineWrapping': true, // word wrap
|
|
|
|
'editor.smartIndent': true, // 'smart' indent
|
|
|
|
'editor.indentWithTabs': false, // smart indent with tabs
|
|
|
|
'editor.tabSize': 4, // tab width, in spaces
|
|
|
|
'editor.keyMap': navigator.appVersion.indexOf('Windows') > 0 ? 'sublime' : 'default',
|
|
|
|
'editor.theme': 'default', // CSS theme
|
|
|
|
'editor.beautify': { // CSS beautifier
|
|
|
|
selector_separator_newline: true,
|
|
|
|
newline_before_open_brace: false,
|
|
|
|
newline_after_open_brace: true,
|
|
|
|
newline_between_properties: true,
|
|
|
|
newline_before_close_brace: true,
|
|
|
|
newline_between_rules: false,
|
2017-03-29 00:50:48 +00:00
|
|
|
end_with_newline: false,
|
|
|
|
space_around_selector_separator: true,
|
2017-03-26 02:30:59 +00:00
|
|
|
},
|
|
|
|
'editor.lintDelay': 500, // lint gutter marker update delay, ms
|
|
|
|
'editor.lintReportDelay': 4500, // lint report update delay, ms
|
2017-03-29 02:26:06 +00:00
|
|
|
'editor.matchHighlight': 'token', // token = token/word under cursor even if nothing is selected
|
|
|
|
// selection = only when something is selected
|
|
|
|
// '' (empty string) = disabled
|
2017-03-26 02:30:59 +00:00
|
|
|
|
|
|
|
'badgeDisabled': '#8B0000', // badge background color when disabled
|
|
|
|
'badgeNormal': '#006666', // badge background color
|
|
|
|
|
2017-03-31 12:22:38 +00:00
|
|
|
'popupWidth': 246, // popup width in pixels
|
2017-03-26 02:30:59 +00:00
|
|
|
|
|
|
|
'updateInterval': 0 // user-style automatic update interval, hour
|
|
|
|
};
|
|
|
|
const values = deepCopy(defaults);
|
|
|
|
|
2017-04-07 02:42:24 +00:00
|
|
|
const affectsIcon = [
|
|
|
|
'show-badge',
|
|
|
|
'disableAll',
|
|
|
|
'badgeDisabled',
|
|
|
|
'badgeNormal',
|
|
|
|
];
|
|
|
|
|
2017-03-31 08:46:18 +00:00
|
|
|
// coalesce multiple pref changes in broadcast
|
|
|
|
let broadcastPrefs = {};
|
|
|
|
|
|
|
|
function doBroadcast() {
|
2017-04-07 02:42:24 +00:00
|
|
|
const affects = {all: 'disableAll' in broadcastPrefs};
|
|
|
|
if (!affects.all) {
|
|
|
|
for (const key in broadcastPrefs) {
|
|
|
|
affects.icon = affects.icon || affectsIcon.includes(key);
|
|
|
|
affects.popup = affects.popup || key.startsWith('popup');
|
|
|
|
affects.editor = affects.editor || key.startsWith('editor');
|
|
|
|
affects.manager = affects.manager || key.startsWith('manage');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
notifyAllTabs({method: 'prefChanged', prefs: broadcastPrefs, affects});
|
2017-03-31 08:46:18 +00:00
|
|
|
broadcastPrefs = {};
|
|
|
|
}
|
|
|
|
|
|
|
|
function doSyncSet() {
|
|
|
|
getSync().set({'settings': values});
|
|
|
|
}
|
2017-03-26 02:30:59 +00:00
|
|
|
|
|
|
|
Object.defineProperty(this, 'readOnlyValues', {value: {}});
|
|
|
|
|
2017-03-31 08:46:18 +00:00
|
|
|
Object.assign(Prefs.prototype, {
|
2017-03-26 02:30:59 +00:00
|
|
|
|
2017-03-31 08:46:18 +00:00
|
|
|
get(key, defaultValue) {
|
|
|
|
if (key in values) {
|
|
|
|
return values[key];
|
|
|
|
}
|
|
|
|
if (defaultValue !== undefined) {
|
|
|
|
return defaultValue;
|
|
|
|
}
|
|
|
|
if (key in defaults) {
|
|
|
|
return defaults[key];
|
|
|
|
}
|
|
|
|
console.warn("No default preference for '%s'", key);
|
|
|
|
},
|
2017-03-26 02:30:59 +00:00
|
|
|
|
2017-03-31 08:46:18 +00:00
|
|
|
getAll() {
|
|
|
|
return deepCopy(values);
|
|
|
|
},
|
2017-03-26 02:30:59 +00:00
|
|
|
|
2017-03-31 08:46:18 +00:00
|
|
|
set(key, value, {noBroadcast, noSync} = {}) {
|
|
|
|
const oldValue = deepCopy(values[key]);
|
|
|
|
values[key] = value;
|
|
|
|
defineReadonlyProperty(this.readOnlyValues, key, value);
|
|
|
|
if (!noBroadcast && !equal(value, oldValue)) {
|
|
|
|
this.broadcast(key, value, {noSync});
|
|
|
|
}
|
2017-03-31 12:22:38 +00:00
|
|
|
localStorage[key] = typeof defaults[key] == 'object'
|
|
|
|
? JSON.stringify(value)
|
|
|
|
: value;
|
2017-03-31 08:46:18 +00:00
|
|
|
},
|
2017-03-26 02:30:59 +00:00
|
|
|
|
2017-03-31 08:46:18 +00:00
|
|
|
remove: key => this.set(key, undefined),
|
|
|
|
|
2017-04-04 17:21:03 +00:00
|
|
|
reset: key => this.set(key, deepCopy(defaults[key])),
|
|
|
|
|
2017-03-31 08:46:18 +00:00
|
|
|
broadcast(key, value, {noSync} = {}) {
|
|
|
|
broadcastPrefs[key] = value;
|
|
|
|
debounce(doBroadcast);
|
|
|
|
if (!noSync) {
|
|
|
|
debounce(doSyncSet);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
});
|
2017-03-26 02:30:59 +00:00
|
|
|
|
2017-03-31 12:22:38 +00:00
|
|
|
// Unlike sync, HTML5 localStorage is ready at browser startup
|
|
|
|
// so we'll mirror the prefs to avoid using the wrong defaults
|
|
|
|
// during the startup phase
|
|
|
|
for (const key in defaults) {
|
|
|
|
const defaultValue = defaults[key];
|
|
|
|
let value = localStorage[key];
|
|
|
|
if (typeof value == 'string') {
|
|
|
|
switch (typeof defaultValue) {
|
|
|
|
case 'boolean':
|
|
|
|
value = value.toLowerCase() === 'true';
|
|
|
|
break;
|
|
|
|
case 'number':
|
|
|
|
value |= 0;
|
|
|
|
break;
|
|
|
|
case 'object':
|
|
|
|
value = tryJSONparse(value) || defaultValue;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
value = defaultValue;
|
|
|
|
}
|
|
|
|
this.set(key, value, {noBroadcast: true});
|
|
|
|
}
|
2017-03-26 02:30:59 +00:00
|
|
|
|
2017-03-31 08:46:18 +00:00
|
|
|
getSync().get('settings', ({settings: synced}) => {
|
2017-03-31 12:22:38 +00:00
|
|
|
if (synced) {
|
|
|
|
for (const key in defaults) {
|
|
|
|
if (key == 'popupWidth' && synced[key] != values.popupWidth) {
|
|
|
|
// this is a fix for the period when popupWidth wasn't synced
|
|
|
|
// TODO: remove it in a couple of months before the summer 2017
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (key in synced) {
|
|
|
|
this.set(key, synced[key], {noSync: true});
|
|
|
|
}
|
2017-03-26 02:30:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (typeof contextMenus !== 'undefined') {
|
|
|
|
for (const id in contextMenus) {
|
|
|
|
if (typeof values[id] == 'boolean') {
|
2017-03-31 08:46:18 +00:00
|
|
|
this.broadcast(id, values[id], {noSync: true});
|
2017-03-26 02:30:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2017-03-31 08:46:18 +00:00
|
|
|
chrome.storage.onChanged.addListener((changes, area) => {
|
2017-03-26 02:30:59 +00:00
|
|
|
if (area == 'sync' && 'settings' in changes) {
|
|
|
|
const synced = changes.settings.newValue;
|
|
|
|
if (synced) {
|
|
|
|
for (const key in defaults) {
|
|
|
|
if (key in synced) {
|
2017-03-31 08:46:18 +00:00
|
|
|
this.set(key, synced[key], {noSync: true});
|
2017-03-26 02:30:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// user manually deleted our settings, we'll recreate them
|
|
|
|
getSync().set({'settings': values});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}();
|
|
|
|
|
|
|
|
|
2015-10-05 11:27:17 +00:00
|
|
|
// Accepts an array of pref names (values are fetched via prefs.get)
|
|
|
|
// and establishes a two-way connection between the document elements and the actual prefs
|
|
|
|
function setupLivePrefs(IDs) {
|
2017-03-26 02:30:59 +00:00
|
|
|
const localIDs = {};
|
|
|
|
IDs.forEach(function(id) {
|
|
|
|
localIDs[id] = true;
|
|
|
|
updateElement(id).addEventListener('change', function() {
|
|
|
|
prefs.set(this.id, isCheckbox(this) ? this.checked : this.value);
|
|
|
|
});
|
|
|
|
});
|
2017-03-31 08:46:18 +00:00
|
|
|
chrome.runtime.onMessage.addListener(msg => {
|
|
|
|
if (msg.prefs) {
|
|
|
|
for (const prefName in msg.prefs) {
|
|
|
|
if (prefName in localIDs) {
|
|
|
|
updateElement(prefName, msg.prefs[prefName]);
|
|
|
|
}
|
|
|
|
}
|
2017-03-26 02:30:59 +00:00
|
|
|
}
|
|
|
|
});
|
2017-03-31 08:46:18 +00:00
|
|
|
function updateElement(id, value) {
|
2017-03-26 02:30:59 +00:00
|
|
|
const el = document.getElementById(id);
|
2017-03-31 08:46:18 +00:00
|
|
|
el[isCheckbox(el) ? 'checked' : 'value'] = value || prefs.get(id);
|
2017-03-26 02:30:59 +00:00
|
|
|
el.dispatchEvent(new Event('change', {bubbles: true, cancelable: true}));
|
|
|
|
return el;
|
|
|
|
}
|
2015-02-09 04:02:08 +00:00
|
|
|
}
|
2015-03-02 22:31:23 +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-05 13:14:59 +00:00
|
|
|
function enforceInputRange(element) {
|
|
|
|
const min = Number(element.min);
|
|
|
|
const max = Number(element.max);
|
|
|
|
const onChange = () => {
|
|
|
|
const value = Number(element.value);
|
|
|
|
if (value < min || value > max) {
|
|
|
|
element.value = Math.max(min, Math.min(max, value));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
onChange();
|
|
|
|
element.addEventListener('change', onChange);
|
|
|
|
element.addEventListener('input', onChange);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-05-05 15:21:45 +00:00
|
|
|
function getCodeMirrorThemes(callback) {
|
2017-03-26 02:30:59 +00:00
|
|
|
chrome.runtime.getPackageDirectoryEntry(function(rootDir) {
|
|
|
|
rootDir.getDirectory('codemirror/theme', {create: false}, function(themeDir) {
|
|
|
|
themeDir.createReader().readEntries(function(entries) {
|
|
|
|
const themes = [chrome.i18n.getMessage('defaultTheme')];
|
|
|
|
entries
|
|
|
|
.filter(entry => entry.isFile)
|
|
|
|
.sort((a, b) => (a.name < b.name ? -1 : 1))
|
|
|
|
.forEach(function(entry) {
|
|
|
|
themes.push(entry.name.replace(/\.css$/, ''));
|
|
|
|
});
|
|
|
|
if (callback) {
|
|
|
|
callback(themes);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2015-05-05 15:21:45 +00:00
|
|
|
}
|
2015-05-27 19:02:36 +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
|
|
|
|
2015-05-27 19:02:36 +00:00
|
|
|
function sessionStorageHash(name) {
|
2017-03-26 02:30:59 +00:00
|
|
|
return {
|
|
|
|
name,
|
|
|
|
value: runTryCatch(JSON.parse, sessionStorage[name]) || {},
|
|
|
|
set(k, v) {
|
|
|
|
this.value[k] = v;
|
|
|
|
this.updateStorage();
|
|
|
|
},
|
|
|
|
unset(k) {
|
|
|
|
delete this.value[k];
|
|
|
|
this.updateStorage();
|
|
|
|
},
|
|
|
|
updateStorage() {
|
|
|
|
sessionStorage[this.name] = JSON.stringify(this.value);
|
|
|
|
}
|
|
|
|
};
|
2015-05-27 19:02:36 +00:00
|
|
|
}
|
2015-06-10 17:15:50 +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
|
|
|
|
2015-10-05 11:12:24 +00:00
|
|
|
function deepCopy(obj) {
|
2017-03-26 02:30:59 +00:00
|
|
|
if (!obj || typeof obj != 'object') {
|
|
|
|
return obj;
|
|
|
|
} else {
|
|
|
|
const emptyCopy = Object.create(Object.getPrototypeOf(obj));
|
|
|
|
return deepMerge(emptyCopy, obj);
|
|
|
|
}
|
2015-07-13 17:44:46 +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-26 02:30:59 +00:00
|
|
|
function deepMerge(target, ...args) {
|
|
|
|
for (const obj of args) {
|
|
|
|
for (const k in obj) {
|
|
|
|
const value = obj[k];
|
|
|
|
if (!value || typeof value != 'object') {
|
|
|
|
target[k] = value;
|
|
|
|
} else if (k in target) {
|
|
|
|
deepMerge(target[k], value);
|
|
|
|
} else if (typeof value.slice == 'function') {
|
|
|
|
target[k] = value.slice();
|
|
|
|
} else {
|
|
|
|
target[k] = deepCopy(value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return target;
|
2015-10-05 11:12: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
|
|
|
|
2015-06-10 17:15:50 +00:00
|
|
|
function equal(a, b) {
|
2017-03-26 02:30:59 +00:00
|
|
|
if (!a || !b || typeof a != 'object' || typeof b != 'object') {
|
|
|
|
return a === b;
|
|
|
|
}
|
|
|
|
if (Object.keys(a).length != Object.keys(b).length) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
for (const k in a) {
|
|
|
|
if (a[k] !== b[k]) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
2015-06-10 17:15:50 +00:00
|
|
|
}
|
2015-10-05 11:12: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
|
|
|
|
2015-10-05 11:12:24 +00:00
|
|
|
function defineReadonlyProperty(obj, key, value) {
|
2017-03-26 02:30:59 +00:00
|
|
|
const copy = deepCopy(value);
|
|
|
|
if (typeof copy == 'object') {
|
|
|
|
Object.freeze(copy);
|
|
|
|
}
|
|
|
|
Object.defineProperty(obj, key, {value: copy, configurable: true});
|
2015-10-05 11:12:24 +00:00
|
|
|
}
|
2016-01-30 23:59:45 +00:00
|
|
|
|
2017-03-31 08:46:18 +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
|
|
|
// Polyfill for Firefox < 53 https://bugzilla.mozilla.org/show_bug.cgi?id=1220494
|
2016-01-30 23:59:45 +00:00
|
|
|
function getSync() {
|
2017-03-26 02:30:59 +00:00
|
|
|
if ('sync' in chrome.storage) {
|
|
|
|
return chrome.storage.sync;
|
|
|
|
}
|
|
|
|
const crappyStorage = {};
|
|
|
|
return {
|
|
|
|
get(key, callback) {
|
|
|
|
callback(crappyStorage[key] || {});
|
|
|
|
},
|
|
|
|
set(source, callback) {
|
|
|
|
for (const property in source) {
|
|
|
|
if (source.hasOwnProperty(property)) {
|
|
|
|
crappyStorage[property] = source[property];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
callback();
|
|
|
|
}
|
|
|
|
};
|
2016-01-30 23:59:45 +00:00
|
|
|
}
|
2017-03-15 11:37:29 +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-15 11:37:29 +00:00
|
|
|
function styleSectionsEqual(styleA, styleB) {
|
2017-03-26 02:30:59 +00:00
|
|
|
if (!styleA.sections || !styleB.sections) {
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
if (styleA.sections.length != styleB.sections.length) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
const propNames = ['code', 'urlPrefixes', 'urls', 'domains', 'regexps'];
|
|
|
|
const typeBcaches = [];
|
|
|
|
checkingEveryInA:
|
|
|
|
for (const sectionA of styleA.sections) {
|
|
|
|
const typeAcache = new Map();
|
|
|
|
for (const name of propNames) {
|
|
|
|
typeAcache.set(name, getType(sectionA[name]));
|
|
|
|
}
|
|
|
|
lookingForDupeInB:
|
|
|
|
for (let i = 0, sectionB; (sectionB = styleB.sections[i]); i++) {
|
|
|
|
const typeBcache = typeBcaches[i] = typeBcaches[i] || new Map();
|
|
|
|
comparingProps:
|
|
|
|
for (const name of propNames) {
|
|
|
|
const propA = sectionA[name];
|
|
|
|
const typeA = typeAcache.get(name);
|
|
|
|
const propB = sectionB[name];
|
|
|
|
let typeB = typeBcache.get(name);
|
|
|
|
if (!typeB) {
|
|
|
|
typeB = getType(propB);
|
|
|
|
typeBcache.set(name, typeB);
|
|
|
|
}
|
|
|
|
if (typeA != typeB) {
|
|
|
|
const bothEmptyOrUndefined =
|
|
|
|
(typeA == 'undefined' || (typeA == 'array' && propA.length == 0)) &&
|
|
|
|
(typeB == 'undefined' || (typeB == 'array' && propB.length == 0));
|
|
|
|
if (bothEmptyOrUndefined) {
|
|
|
|
continue comparingProps;
|
|
|
|
} else {
|
|
|
|
continue lookingForDupeInB;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (typeA == 'undefined') {
|
|
|
|
continue comparingProps;
|
|
|
|
}
|
|
|
|
if (typeA == 'array') {
|
|
|
|
if (propA.length != propB.length) {
|
|
|
|
continue lookingForDupeInB;
|
|
|
|
}
|
|
|
|
for (const item of propA) {
|
|
|
|
if (propB.indexOf(item) < 0) {
|
|
|
|
continue lookingForDupeInB;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
continue comparingProps;
|
|
|
|
}
|
|
|
|
if (typeA == 'string' && propA != propB) {
|
|
|
|
continue lookingForDupeInB;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// dupe found
|
|
|
|
continue checkingEveryInA;
|
|
|
|
}
|
|
|
|
// dupe not found
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|