2015-01-30 17:07:24 +00:00
|
|
|
function getDatabase(ready, error) {
|
2016-03-07 02:27:17 +00:00
|
|
|
var dbOpenRequest = window.indexedDB.open("stylish", 2);
|
|
|
|
dbOpenRequest.onsuccess = function(e) {
|
|
|
|
ready(e.target.result);
|
|
|
|
};
|
|
|
|
dbOpenRequest.onerror = function(event) {
|
|
|
|
console.log(event.target.errorCode);
|
|
|
|
if (error) {
|
|
|
|
error(event);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
dbOpenRequest.onupgradeneeded = function(event) {
|
|
|
|
if (event.oldVersion == 0) {
|
|
|
|
var os = event.target.result.createObjectStore("styles", {keyPath: 'id', autoIncrement: true});
|
|
|
|
webSqlStorage.migrate();
|
|
|
|
}
|
2015-02-09 04:02:08 +00:00
|
|
|
}
|
2016-03-07 02:27:17 +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
|
|
|
|
|
|
|
// Let manage/popup/edit reuse background page variables
|
|
|
|
// Note, only "var"-declared variables are visible from another extension page
|
2017-03-19 19:49:43 +00:00
|
|
|
var cachedStyles = ((bg) => bg && bg.cachedStyles || {
|
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
|
|
|
bg,
|
|
|
|
list: null,
|
|
|
|
noCode: null,
|
|
|
|
byId: new Map(),
|
|
|
|
filters: new Map(),
|
|
|
|
mutex: {
|
|
|
|
inProgress: false,
|
|
|
|
onDone: [],
|
|
|
|
},
|
|
|
|
})(chrome.extension.getBackgroundPage());
|
|
|
|
|
|
|
|
|
|
|
|
// in case Chrome haven't yet loaded the bg page and displays our page like edit/manage
|
|
|
|
function getStylesSafe(options) {
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-03-07 02:27:17 +00:00
|
|
|
function getStyles(options, callback) {
|
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
|
|
|
if (cachedStyles.list) {
|
|
|
|
callback(filterStyles(options));
|
2016-03-19 23:27:28 +00:00
|
|
|
return;
|
|
|
|
}
|
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
|
|
|
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');
|
2017-03-19 19:49:43 +00:00
|
|
|
os.getAll().onsuccess = event => {
|
|
|
|
cachedStyles.list = event.target.result || [];
|
|
|
|
cachedStyles.noCode = [];
|
|
|
|
cachedStyles.byId.clear();
|
|
|
|
for (let style of cachedStyles.list) {
|
|
|
|
const noCode = getStyleWithNoCode(style);
|
|
|
|
cachedStyles.noCode.push(noCode);
|
|
|
|
cachedStyles.byId.set(style.id, {style, noCode});
|
|
|
|
}
|
|
|
|
//console.log('%s getStyles %s, invoking cached callbacks: %o', (performance.now() - t0).toFixed(1), JSON.stringify(options), cachedStyles.mutex.onDone.map(e => JSON.stringify(e.options)))
|
|
|
|
try{
|
|
|
|
callback(filterStyles(options));
|
|
|
|
} catch(e){
|
|
|
|
// no error in console, it works
|
|
|
|
}
|
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-19 19:49:43 +00:00
|
|
|
cachedStyles.mutex.inProgress = false;
|
|
|
|
for (let {options, callback} of cachedStyles.mutex.onDone) {
|
|
|
|
callback(filterStyles(options));
|
2016-03-07 02:27:17 +00:00
|
|
|
}
|
2017-03-19 19:49:43 +00:00
|
|
|
cachedStyles.mutex.onDone = [];
|
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
|
|
|
}, 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) {
|
|
|
|
const stripped = Object.assign({}, style, {sections: []});
|
|
|
|
for (let section of style.sections) {
|
|
|
|
stripped.sections.push(Object.assign({}, section, {code: null}));
|
2016-03-19 23:27:28 +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
|
|
|
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} = {}) {
|
|
|
|
// prevent double-add on echoed invalidation
|
|
|
|
const cached = added && cachedStyles.byId.get(added.id);
|
|
|
|
if (cached) {
|
|
|
|
return;
|
2016-03-19 21:58:01 +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
|
|
|
if (andNotify) {
|
|
|
|
chrome.runtime.sendMessage({method: 'invalidateCache', added, updated, deletedId});
|
2016-03-19 21:58:01 +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
|
|
|
if (!cachedStyles.list) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (updated) {
|
|
|
|
const cached = cachedStyles.byId.get(updated.id);
|
|
|
|
if (cached) {
|
|
|
|
Object.assign(cached.style, updated);
|
|
|
|
Object.assign(cached.noCode, getStyleWithNoCode(updated));
|
|
|
|
//console.log('cache: updated', updated);
|
|
|
|
}
|
|
|
|
cachedStyles.filters.clear();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (added) {
|
|
|
|
const noCode = getStyleWithNoCode(added);
|
|
|
|
cachedStyles.list.push(added);
|
|
|
|
cachedStyles.noCode.push(noCode);
|
|
|
|
cachedStyles.byId.set(added.id, {style: added, noCode});
|
|
|
|
//console.log('cache: added', 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.noCode.splice(cachedIndex, 1);
|
|
|
|
cachedStyles.byId.delete(deletedId);
|
|
|
|
//console.log('cache: deleted', deletedStyle);
|
|
|
|
cachedStyles.filters.clear();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
cachedStyles.list = null;
|
|
|
|
cachedStyles.noCode = null;
|
|
|
|
//console.log('cache cleared');
|
|
|
|
cachedStyles.filters.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function filterStyles(options = {}) {
|
|
|
|
const t0 = performance.now()
|
|
|
|
const enabled = fixBoolean(options.enabled);
|
|
|
|
const url = 'url' in options ? options.url : null;
|
|
|
|
const id = 'id' in options ? Number(options.id) : null;
|
|
|
|
const matchUrl = 'matchUrl' in options ? options.matchUrl : null;
|
|
|
|
const code = 'code' in options ? options.code : true;
|
|
|
|
const asHash = 'asHash' in options ? options.asHash : false;
|
|
|
|
|
|
|
|
if (enabled == null
|
|
|
|
&& url == null
|
|
|
|
&& id == null
|
|
|
|
&& matchUrl == null
|
|
|
|
&& asHash != true) {
|
|
|
|
//console.log('%c%s filterStyles SKIPPED LOOP %s', 'color:gray', (performance.now() - t0).toFixed(1), JSON.stringify(options))
|
|
|
|
return code ? cachedStyles.list : cachedStyles.noCode;
|
|
|
|
}
|
|
|
|
|
|
|
|
// add \t after url to prevent collisions (not sure it can actually happen though)
|
|
|
|
const cacheKey = '' + enabled + url + '\t' + id + matchUrl + '\t' + code + asHash;
|
|
|
|
const cached = cachedStyles.filters.get(cacheKey);
|
|
|
|
if (cached) {
|
|
|
|
//console.log('%c%s filterStyles REUSED RESPONSE %s', 'color:gray', (performance.now() - t0).toFixed(1), JSON.stringify(options))
|
2017-03-22 05:12:11 +00:00
|
|
|
cached.hits++;
|
|
|
|
cached.lastHit = Date.now();
|
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
|
|
|
return asHash
|
2017-03-22 05:12:11 +00:00
|
|
|
? Object.assign({disableAll: prefs.get('disableAll', false)}, cached.styles)
|
|
|
|
: cached.styles;
|
2016-03-19 21:58:01 +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
|
|
|
|
|
|
|
const styles = id == null
|
|
|
|
? (code ? cachedStyles.list : cachedStyles.noCode)
|
2017-03-20 21:35:51 +00:00
|
|
|
: [(cachedStyles.byId.get(id) || {})[code ? 'style' : 'noCode']];
|
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
|
|
|
const filtered = asHash ? {} : [];
|
2017-03-20 21:35:51 +00:00
|
|
|
if (!styles) {
|
|
|
|
// may happen when users [accidentally] reopen an old URL
|
|
|
|
// of edit.html with a non-existent style id parameter
|
|
|
|
return filtered;
|
|
|
|
}
|
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
|
|
|
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)) {
|
|
|
|
const sections = (asHash || matchUrl != null) && getApplicableSections(style, matchUrl);
|
|
|
|
if (asHash) {
|
|
|
|
if (sections.length) {
|
|
|
|
filtered[style.id] = 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
|
|
|
} else if (matchUrl == null || sections.length) {
|
|
|
|
filtered.push(style);
|
|
|
|
}
|
2016-03-07 02:27:17 +00:00
|
|
|
}
|
2016-03-19 21:58:01 +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
|
|
|
//console.log('%s filterStyles %s', (performance.now() - t0).toFixed(1), JSON.stringify(options))
|
2017-03-22 05:12:11 +00:00
|
|
|
cachedStyles.filters.set(cacheKey, {
|
|
|
|
styles: filtered,
|
|
|
|
lastHit: Date.now(),
|
|
|
|
hits: 1,
|
|
|
|
});
|
|
|
|
if (cachedStyles.filters.size > 10000) {
|
|
|
|
cleanupCachedFilters();
|
|
|
|
}
|
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
|
|
|
return asHash
|
|
|
|
? Object.assign({disableAll: prefs.get('disableAll', false)}, 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} = {}) {
|
|
|
|
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%
|
|
|
|
const sorted = [...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-25 02:35:54 +00:00
|
|
|
function saveStyle(style) {
|
2017-03-14 23:27:52 +00:00
|
|
|
return new Promise(resolve => {
|
|
|
|
getDatabase(db => {
|
|
|
|
const tx = db.transaction(['styles'], 'readwrite');
|
|
|
|
const os = tx.objectStore('styles');
|
2016-03-07 02:27:17 +00:00
|
|
|
|
2017-03-24 02:51:44 +00:00
|
|
|
const id = style.id !== undefined && style.id !== null ? Number(style.id) : null;
|
2017-03-20 01:58:55 +00:00
|
|
|
const reason = style.reason;
|
2017-03-25 02:35:54 +00:00
|
|
|
const notify = style.notify !== false;
|
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
|
|
|
delete style.method;
|
2017-03-20 01:58:55 +00:00
|
|
|
delete style.reason;
|
2017-03-25 02:35:54 +00:00
|
|
|
delete style.notify;
|
|
|
|
if (!style.name) {
|
|
|
|
delete style.name;
|
|
|
|
}
|
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-14 23:27:52 +00:00
|
|
|
// Update
|
2017-03-24 02:51:44 +00:00
|
|
|
if (id != null) {
|
|
|
|
style.id = id;
|
|
|
|
os.get(id).onsuccess = eventGet => {
|
|
|
|
const existed = !!eventGet.target.result;
|
2017-03-15 11:55:20 +00:00
|
|
|
const oldStyle = Object.assign({}, eventGet.target.result);
|
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
|
|
|
const codeIsUpdated = 'sections' in style && !styleSectionsEqual(style, oldStyle);
|
2017-03-15 11:55:20 +00:00
|
|
|
style = Object.assign(oldStyle, style);
|
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
|
|
|
addMissingStyleTargets(style);
|
2017-03-14 23:27:52 +00:00
|
|
|
os.put(style).onsuccess = eventPut => {
|
|
|
|
style.id = style.id || eventPut.target.result;
|
2017-03-24 02:51:44 +00:00
|
|
|
invalidateCache(notify, existed ? {updated: style} : {added: style});
|
2017-03-14 23:27:52 +00:00
|
|
|
if (notify) {
|
2017-03-24 02:51:44 +00:00
|
|
|
notifyAllTabs({
|
|
|
|
method: existed ? 'styleUpdated' : 'styleAdded',
|
|
|
|
style, codeIsUpdated, reason,
|
|
|
|
});
|
2017-03-14 23:27:52 +00:00
|
|
|
}
|
2017-03-25 02:35:54 +00:00
|
|
|
if (typeof handleUpdate != 'undefined') {
|
|
|
|
handleUpdate(style, {reason});
|
|
|
|
}
|
2017-03-14 23:27:52 +00:00
|
|
|
resolve(style);
|
|
|
|
};
|
2016-03-07 02:27:17 +00:00
|
|
|
};
|
2017-03-14 23:27:52 +00:00
|
|
|
return;
|
|
|
|
}
|
2015-01-30 17:07:24 +00:00
|
|
|
|
2017-03-14 23:27:52 +00:00
|
|
|
// Create
|
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
|
|
|
delete style.id;
|
2017-03-14 23:27:52 +00:00
|
|
|
style = Object.assign({
|
|
|
|
// Set optional things if they're undefined
|
|
|
|
enabled: true,
|
|
|
|
updateUrl: null,
|
|
|
|
md5Url: null,
|
|
|
|
url: null,
|
|
|
|
originalMd5: null,
|
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
|
|
|
}, style);
|
|
|
|
addMissingStyleTargets(style);
|
2017-03-14 23:27:52 +00:00
|
|
|
os.add(style).onsuccess = event => {
|
|
|
|
// Give it the ID that was generated
|
|
|
|
style.id = event.target.result;
|
2017-03-24 02:51:44 +00:00
|
|
|
invalidateCache(notify, {added: style});
|
|
|
|
if (notify) {
|
|
|
|
notifyAllTabs({method: 'styleAdded', style, reason});
|
|
|
|
}
|
2017-03-25 02:35:54 +00:00
|
|
|
if (typeof handleUpdate != 'undefined') {
|
|
|
|
handleUpdate(style, {reason});
|
|
|
|
}
|
2017-03-14 23:27:52 +00:00
|
|
|
resolve(style);
|
|
|
|
};
|
2016-03-17 02:44:17 +00:00
|
|
|
});
|
2016-03-07 02:27:17 +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 addMissingStyleTargets(style) {
|
|
|
|
style.sections = (style.sections || []).map(section =>
|
|
|
|
Object.assign({
|
|
|
|
urls: [],
|
|
|
|
urlPrefixes: [],
|
|
|
|
domains: [],
|
|
|
|
regexps: [],
|
|
|
|
}, section)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-01-30 17:07:24 +00:00
|
|
|
function enableStyle(id, enabled) {
|
2017-03-21 01:32:38 +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} = {}) {
|
|
|
|
return new Promise(resolve =>
|
|
|
|
getDatabase(db => {
|
|
|
|
const tx = db.transaction(['styles'], 'readwrite');
|
|
|
|
const os = tx.objectStore('styles');
|
|
|
|
os.delete(Number(id)).onsuccess = event => {
|
|
|
|
invalidateCache(notify, {deletedId: id});
|
|
|
|
if (notify) {
|
|
|
|
notifyAllTabs({method: 'styleDeleted', id});
|
|
|
|
}
|
2017-03-25 02:35:54 +00:00
|
|
|
if (typeof handleDelete != 'undefined') {
|
|
|
|
handleDelete(id);
|
|
|
|
}
|
2017-03-24 02:51:44 +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
|
|
|
|
2015-01-30 17:07:24 +00:00
|
|
|
function reportError() {
|
|
|
|
for (i in arguments) {
|
|
|
|
if ("message" in arguments[i]) {
|
|
|
|
//alert(arguments[i].message);
|
|
|
|
console.log(arguments[i].message);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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) {
|
|
|
|
if (typeof b != "undefined") {
|
|
|
|
return b != "false";
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
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) {
|
|
|
|
if (url.indexOf("file:") == 0) {
|
|
|
|
return [];
|
|
|
|
}
|
2015-04-07 08:56:50 +00:00
|
|
|
var d = /.*?:\/*([^\/:]+)/.exec(url)[1];
|
2015-01-30 17:07:24 +00:00
|
|
|
var domains = [d];
|
|
|
|
while (d.indexOf(".") != -1) {
|
|
|
|
d = d.substring(d.indexOf(".") + 1);
|
|
|
|
domains.push(d);
|
|
|
|
}
|
|
|
|
return domains;
|
|
|
|
}
|
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-24 01:19:56 +00:00
|
|
|
if (typeof o == 'undefined' || typeof o == 'string') {
|
2015-02-09 04:02:08 +00:00
|
|
|
return typeof o;
|
|
|
|
}
|
2017-03-24 01:19:56 +00:00
|
|
|
// with the persistent cachedStyles the Array reference is usually different
|
2017-03-24 02:51:44 +00:00
|
|
|
// so let's check for e.g. type of 'every' which is only present on arrays
|
|
|
|
// (in the context of our extension)
|
2017-03-24 01:19:56 +00:00
|
|
|
if (o instanceof Array || typeof o.every == 'function') {
|
|
|
|
return 'array';
|
2015-02-09 04:02:08 +00:00
|
|
|
}
|
2017-03-24 01:19:56 +00:00
|
|
|
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
|
|
|
const namespacePattern = /^\s*(@namespace[^;]+;\s*)+$/;
|
|
|
|
|
2016-03-07 02:27:17 +00:00
|
|
|
function getApplicableSections(style, url) {
|
|
|
|
var sections = style.sections.filter(function(section) {
|
|
|
|
return sectionAppliesToUrl(section, url);
|
|
|
|
});
|
|
|
|
// ignore if it's just namespaces
|
|
|
|
if (sections.length == 1 && namespacePattern.test(sections[0].code)) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
return sections;
|
|
|
|
}
|
|
|
|
|
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 sectionAppliesToUrl(section, url) {
|
|
|
|
// only http, https, file, and chrome-extension allowed
|
|
|
|
if (url.indexOf("http") != 0 && url.indexOf("file") != 0 && url.indexOf("chrome-extension") != 0 && url.indexOf("ftp") != 0) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// other extensions can't be styled
|
|
|
|
if (url.indexOf("chrome-extension") == 0 && url.indexOf(chrome.extension.getURL("")) != 0) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (section.urls.length == 0 && section.domains.length == 0 && section.urlPrefixes.length == 0 && section.regexps.length == 0) {
|
|
|
|
//console.log(section.id + " is global");
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (section.urls.indexOf(url) != -1) {
|
|
|
|
//console.log(section.id + " applies to " + url + " due to URL rules");
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (section.urlPrefixes.some(function(prefix) {
|
|
|
|
return url.indexOf(prefix) == 0;
|
|
|
|
})) {
|
|
|
|
//console.log(section.id + " applies to " + url + " due to URL prefix rules");
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (section.domains.length > 0 && getDomains(url).some(function(domain) {
|
|
|
|
return section.domains.indexOf(domain) != -1;
|
|
|
|
})) {
|
|
|
|
//console.log(section.id + " applies due to " + url + " due to domain rules");
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (section.regexps.some(function(regexp) {
|
|
|
|
// we want to match the full url, so add ^ and $ if not already present
|
|
|
|
if (regexp[0] != "^") {
|
|
|
|
regexp = "^" + regexp;
|
|
|
|
}
|
|
|
|
if (regexp[regexp.length - 1] != "$") {
|
|
|
|
regexp += "$";
|
|
|
|
}
|
|
|
|
var re = runTryCatch(function() { return new RegExp(regexp) });
|
|
|
|
if (re) {
|
|
|
|
return (re).test(url);
|
|
|
|
} else {
|
|
|
|
console.log(section.id + "'s regexp '" + regexp + "' is not valid");
|
|
|
|
}
|
|
|
|
})) {
|
|
|
|
//console.log(section.id + " applies to " + url + " due to regexp rules");
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
//console.log(section.id + " does not apply due to " + url);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
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) {
|
|
|
|
return el.nodeName.toLowerCase() == "input" && "checkbox" == el.type.toLowerCase();
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
function runTryCatch(func) {
|
|
|
|
try { return func() }
|
|
|
|
catch(e) {}
|
|
|
|
}
|
|
|
|
|
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) {
|
2015-10-05 11:12:24 +00:00
|
|
|
var localIDs = {};
|
|
|
|
IDs.forEach(function(id) {
|
|
|
|
localIDs[id] = true;
|
|
|
|
updateElement(id).addEventListener("change", function() {
|
2015-10-05 11:27:17 +00:00
|
|
|
prefs.set(this.id, isCheckbox(this) ? this.checked : this.value);
|
2015-10-05 11:12:24 +00:00
|
|
|
});
|
|
|
|
});
|
2016-01-31 00:06:04 +00:00
|
|
|
chrome.runtime.onMessage.addListener(function(request) {
|
2015-10-05 11:12:24 +00:00
|
|
|
if (request.prefName in localIDs) {
|
|
|
|
updateElement(request.prefName);
|
2015-02-09 04:02:08 +00:00
|
|
|
}
|
2015-10-05 11:12:24 +00:00
|
|
|
});
|
|
|
|
function updateElement(id) {
|
|
|
|
var el = document.getElementById(id);
|
2015-10-05 11:27:17 +00:00
|
|
|
el[isCheckbox(el) ? "checked" : "value"] = prefs.get(id);
|
2015-03-07 07:57:57 +00:00
|
|
|
el.dispatchEvent(new Event("change", {bubbles: true, cancelable: true}));
|
2015-10-05 11:12:24 +00:00
|
|
|
return el;
|
2015-02-17 19:50:51 +00:00
|
|
|
}
|
2015-02-09 04:02:08 +00:00
|
|
|
}
|
2015-03-02 22:31:23 +00:00
|
|
|
|
2015-10-05 11:12:24 +00:00
|
|
|
var prefs = chrome.extension.getBackgroundPage().prefs || new function Prefs() {
|
|
|
|
var me = this;
|
|
|
|
|
|
|
|
var defaults = {
|
2015-10-04 12:26:50 +00:00
|
|
|
"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
|
|
|
|
|
|
|
|
"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,
|
|
|
|
end_with_newline: false
|
|
|
|
},
|
|
|
|
"editor.lintDelay": 500, // lint gutter marker update delay, ms
|
|
|
|
"editor.lintReportDelay": 4500, // lint report update delay, ms
|
2017-02-14 15:35:53 +00:00
|
|
|
|
2017-02-14 22:42:27 +00:00
|
|
|
"badgeDisabled": "#8B0000", // badge background color when disabled
|
2017-02-14 15:35:53 +00:00
|
|
|
"badgeNormal": "#006666", // badge background color
|
|
|
|
|
2017-03-06 20:17:32 +00:00
|
|
|
"popupWidth": 240, // popup width in pixels
|
|
|
|
|
2017-02-23 06:44:52 +00:00
|
|
|
"updateInterval": 0 // user-style automatic update interval, hour
|
2015-10-05 11:12:24 +00:00
|
|
|
};
|
|
|
|
var values = deepCopy(defaults);
|
2015-03-04 07:55:35 +00:00
|
|
|
|
2015-10-05 11:12:24 +00:00
|
|
|
var syncTimeout; // see broadcast() function below
|
2015-03-02 22:31:23 +00:00
|
|
|
|
2015-10-05 11:12:24 +00:00
|
|
|
Object.defineProperty(this, "readOnlyValues", {value: {}});
|
|
|
|
|
2015-10-05 11:27:17 +00:00
|
|
|
Prefs.prototype.get = function(key, defaultValue) {
|
2015-10-05 11:12:24 +00:00
|
|
|
if (key in values) {
|
|
|
|
return values[key];
|
2015-03-02 22:31:23 +00:00
|
|
|
}
|
2015-10-05 11:12:24 +00:00
|
|
|
if (defaultValue !== undefined) {
|
|
|
|
return defaultValue;
|
2015-03-02 22:31:23 +00:00
|
|
|
}
|
2015-10-05 11:12:24 +00:00
|
|
|
if (key in defaults) {
|
|
|
|
return defaults[key];
|
2015-03-07 07:57:57 +00:00
|
|
|
}
|
2015-10-05 11:12:24 +00:00
|
|
|
console.warn("No default preference for '%s'", key);
|
|
|
|
};
|
|
|
|
|
2015-10-05 11:27:17 +00:00
|
|
|
Prefs.prototype.getAll = function(key) {
|
2015-10-05 11:12:24 +00:00
|
|
|
return deepCopy(values);
|
|
|
|
};
|
|
|
|
|
2015-10-05 11:27:17 +00:00
|
|
|
Prefs.prototype.set = function(key, value, options) {
|
2015-10-05 11:12:24 +00:00
|
|
|
var oldValue = deepCopy(values[key]);
|
|
|
|
values[key] = value;
|
|
|
|
defineReadonlyProperty(this.readOnlyValues, key, value);
|
|
|
|
if ((!options || !options.noBroadcast) && !equal(value, oldValue)) {
|
|
|
|
me.broadcast(key, value, options);
|
2015-10-04 12:26:50 +00:00
|
|
|
}
|
2015-10-05 11:12:24 +00:00
|
|
|
};
|
|
|
|
|
2015-10-05 11:27:17 +00:00
|
|
|
Prefs.prototype.remove = function(key) { me.set(key, undefined) };
|
2015-10-05 11:12:24 +00:00
|
|
|
|
|
|
|
Prefs.prototype.broadcast = function(key, value, options) {
|
2015-10-04 12:26:50 +00:00
|
|
|
var message = {method: "prefChanged", prefName: key, value: value};
|
|
|
|
notifyAllTabs(message);
|
2016-01-30 23:08:10 +00:00
|
|
|
chrome.runtime.sendMessage(message);
|
2015-10-05 11:12:24 +00:00
|
|
|
if (key == "disableAll") {
|
|
|
|
notifyAllTabs({method: "styleDisableAll", disableAll: value});
|
|
|
|
}
|
2015-10-04 12:26:50 +00:00
|
|
|
if (!options || !options.noSync) {
|
2015-10-05 11:12:24 +00:00
|
|
|
clearTimeout(syncTimeout);
|
|
|
|
syncTimeout = setTimeout(function() {
|
2016-01-30 23:59:45 +00:00
|
|
|
getSync().set({"settings": values});
|
2015-10-05 11:12:24 +00:00
|
|
|
}, 0);
|
2015-03-07 07:57:57 +00:00
|
|
|
}
|
2015-10-05 11:12:24 +00:00
|
|
|
};
|
2015-05-05 15:21:45 +00:00
|
|
|
|
2015-10-05 11:12:24 +00:00
|
|
|
Object.keys(defaults).forEach(function(key) {
|
2015-10-05 11:27:17 +00:00
|
|
|
me.set(key, defaults[key], {noBroadcast: true});
|
2015-10-05 11:12:24 +00:00
|
|
|
});
|
|
|
|
|
2016-01-30 23:59:45 +00:00
|
|
|
getSync().get("settings", function(result) {
|
2015-10-05 11:12:24 +00:00
|
|
|
var synced = result.settings;
|
|
|
|
for (var key in defaults) {
|
|
|
|
if (synced && (key in synced)) {
|
2015-10-05 11:27:17 +00:00
|
|
|
me.set(key, synced[key], {noSync: true});
|
2015-10-05 11:12:24 +00:00
|
|
|
} else {
|
|
|
|
var value = tryMigrating(key);
|
|
|
|
if (value !== undefined) {
|
2015-10-05 11:27:17 +00:00
|
|
|
me.set(key, value);
|
2015-10-05 11:12:24 +00:00
|
|
|
}
|
|
|
|
}
|
2015-10-04 12:26:50 +00:00
|
|
|
}
|
2017-03-18 22:35:27 +00:00
|
|
|
if (typeof contextMenus !== 'undefined') {
|
|
|
|
for (let id in contextMenus) {
|
|
|
|
if (typeof values[id] == 'boolean') {
|
|
|
|
me.broadcast(id, values[id], {noSync: true});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-10-04 12:26:50 +00:00
|
|
|
});
|
2015-10-05 11:12:24 +00:00
|
|
|
|
|
|
|
chrome.storage.onChanged.addListener(function(changes, area) {
|
|
|
|
if (area == "sync" && "settings" in changes) {
|
|
|
|
var synced = changes.settings.newValue;
|
|
|
|
if (synced) {
|
|
|
|
for (key in defaults) {
|
|
|
|
if (key in synced) {
|
2015-10-05 11:27:17 +00:00
|
|
|
me.set(key, synced[key], {noSync: true});
|
2015-10-05 11:12:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// user manually deleted our settings, we'll recreate them
|
2016-01-30 23:59:45 +00:00
|
|
|
getSync().set({"settings": values});
|
2015-10-04 12:26:50 +00:00
|
|
|
}
|
|
|
|
}
|
2015-10-05 11:12:24 +00:00
|
|
|
});
|
2015-10-04 12:26:50 +00:00
|
|
|
|
2015-10-05 11:12:24 +00:00
|
|
|
function tryMigrating(key) {
|
|
|
|
if (!(key in localStorage)) {
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
var value = localStorage[key];
|
|
|
|
delete localStorage[key];
|
|
|
|
localStorage["DEPRECATED: " + key] = value;
|
|
|
|
switch (typeof defaults[key]) {
|
|
|
|
case "boolean":
|
|
|
|
return value.toLowerCase() === "true";
|
|
|
|
case "number":
|
|
|
|
return Number(value);
|
|
|
|
case "object":
|
|
|
|
try {
|
|
|
|
return JSON.parse(value);
|
|
|
|
} catch(e) {
|
|
|
|
console.log("Cannot migrate from localStorage %s = '%s': %o", key, value, e);
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return value;
|
2015-10-04 12:26:50 +00:00
|
|
|
}
|
2015-10-05 11:12:24 +00:00
|
|
|
};
|
2015-10-04 12:26: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-05-05 15:21:45 +00:00
|
|
|
function getCodeMirrorThemes(callback) {
|
|
|
|
chrome.runtime.getPackageDirectoryEntry(function(rootDir) {
|
|
|
|
rootDir.getDirectory("codemirror/theme", {create: false}, function(themeDir) {
|
|
|
|
themeDir.createReader().readEntries(function(entries) {
|
2015-05-05 20:42:38 +00:00
|
|
|
var themes = [chrome.i18n.getMessage("defaultTheme")];
|
2015-05-05 15:21:45 +00:00
|
|
|
entries
|
|
|
|
.filter(function(entry) { return entry.isFile })
|
|
|
|
.sort(function(a, b) { return a.name < b.name ? -1 : 1 })
|
|
|
|
.forEach(function(entry) {
|
|
|
|
themes.push(entry.name.replace(/\.css$/, ""));
|
|
|
|
});
|
|
|
|
if (callback) {
|
|
|
|
callback(themes);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
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) {
|
|
|
|
var hash = {
|
|
|
|
value: {},
|
|
|
|
set: function(k, v) { this.value[k] = v; this.updateStorage(); },
|
|
|
|
unset: function(k) { delete this.value[k]; this.updateStorage(); },
|
|
|
|
updateStorage: function() {
|
|
|
|
sessionStorage[this.name] = JSON.stringify(this.value);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
try { hash.value = JSON.parse(sessionStorage[name]); } catch(e) {}
|
|
|
|
Object.defineProperty(hash, "name", {value: name});
|
|
|
|
return hash;
|
|
|
|
}
|
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) {
|
|
|
|
if (!obj || typeof obj != "object") {
|
|
|
|
return obj;
|
|
|
|
} else {
|
|
|
|
var 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
|
|
|
|
2015-10-05 11:12:24 +00:00
|
|
|
function deepMerge(target, obj1 /* plus any number of object arguments */) {
|
|
|
|
for (var i = 1; i < arguments.length; i++) {
|
|
|
|
var obj = arguments[i];
|
|
|
|
for (var k in obj) {
|
|
|
|
// hasOwnProperty checking is not needed for our non-OOP stuff
|
|
|
|
var value = obj[k];
|
|
|
|
if (!value || typeof value != "object") {
|
|
|
|
target[k] = value;
|
|
|
|
} else if (k in target) {
|
|
|
|
deepMerge(target[k], value);
|
|
|
|
} else {
|
|
|
|
target[k] = deepCopy(value);
|
|
|
|
}
|
2015-07-13 17:44:46 +00:00
|
|
|
}
|
2015-06-10 17:15:50 +00:00
|
|
|
}
|
2015-10-05 11:12:24 +00:00
|
|
|
return target;
|
|
|
|
}
|
|
|
|
|
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) {
|
2015-07-26 02:24:18 +00:00
|
|
|
if (!a || !b || typeof a != "object" || typeof b != "object") {
|
2015-06-10 17:15:50 +00:00
|
|
|
return a === b;
|
|
|
|
}
|
|
|
|
if (Object.keys(a).length != Object.keys(b).length) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
for (var k in a) {
|
|
|
|
if (a[k] !== b[k]) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
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) {
|
|
|
|
var copy = deepCopy(value);
|
2016-04-05 00:02:20 +00:00
|
|
|
// In ES6, freezing a literal is OK (it returns the same value), but in previous versions it's an exception.
|
|
|
|
if (typeof copy == "object") {
|
|
|
|
Object.freeze(copy);
|
|
|
|
}
|
2015-10-05 11:12:24 +00:00
|
|
|
Object.defineProperty(obj, key, {value: copy, configurable: true})
|
|
|
|
}
|
2016-01-30 23:59:45 +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() {
|
|
|
|
if ("sync" in chrome.storage) {
|
|
|
|
return chrome.storage.sync;
|
|
|
|
}
|
|
|
|
crappyStorage = {};
|
|
|
|
return {
|
|
|
|
get: function(key, callback) {
|
|
|
|
callback(crappyStorage[key] || {});
|
|
|
|
},
|
|
|
|
set: function(source, callback) {
|
|
|
|
for (var property in source) {
|
|
|
|
if (source.hasOwnProperty(property)) {
|
|
|
|
crappyStorage[property] = source[property];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
callback();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
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-15 11:55:20 +00:00
|
|
|
if (!styleA.sections || !styleB.sections) {
|
|
|
|
return undefined;
|
|
|
|
}
|
2017-03-15 11:37:29 +00:00
|
|
|
if (styleA.sections.length != styleB.sections.length) {
|
|
|
|
return false;
|
|
|
|
}
|
2017-03-24 01:19:56 +00:00
|
|
|
const propNames = ['code', 'urlPrefixes', 'urls', 'domains', 'regexps'];
|
|
|
|
const typeBcaches = [];
|
|
|
|
checkingEveryInA: for (let sectionA of styleA.sections) {
|
|
|
|
const typeAcache = new Map();
|
|
|
|
for (let name of propNames) {
|
|
|
|
typeAcache.set(name, getType(sectionA[name]));
|
2017-03-15 11:37:29 +00:00
|
|
|
}
|
2017-03-24 01:19:56 +00:00
|
|
|
lookingForDupeInB: for (let i = 0, sectionB; (sectionB = styleB.sections[i]); i++) {
|
|
|
|
const typeBcache = typeBcaches[i] = typeBcaches[i] || new Map();
|
|
|
|
comparingProps: for (let name of propNames) {
|
|
|
|
const propA = sectionA[name], 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 (let item of propA) {
|
|
|
|
if (propB.indexOf(item) < 0) {
|
|
|
|
continue lookingForDupeInB;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
continue comparingProps;
|
|
|
|
}
|
|
|
|
if (typeA == 'string' && propA != propB) {
|
|
|
|
continue lookingForDupeInB;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// dupe found
|
|
|
|
continue checkingEveryInA;
|
2017-03-15 11:37:29 +00:00
|
|
|
}
|
2017-03-24 01:19:56 +00:00
|
|
|
// dupe not found
|
|
|
|
return false;
|
2017-03-15 11:37:29 +00:00
|
|
|
}
|
2017-03-24 01:19:56 +00:00
|
|
|
return true;
|
2017-03-15 11:37:29 +00:00
|
|
|
}
|