move 'tempUsercssCode' cleanup to usercss helper

This commit is contained in:
tophf 2017-12-10 10:04:13 +03:00
parent 5d54ce3464
commit 405400de68
2 changed files with 30 additions and 8 deletions

View File

@ -195,18 +195,12 @@ function dbExecChromeStorage(method, data) {
case 'getAll': case 'getAll':
return chromeLocal.get(null).then(storage => { return chromeLocal.get(null).then(storage => {
const styles = []; const styles = [];
const leftovers = [];
for (const key in storage) { for (const key in storage) {
if (key.startsWith(STYLE_KEY_PREFIX) && if (key.startsWith(STYLE_KEY_PREFIX) &&
Number(key.substr(STYLE_KEY_PREFIX.length))) { Number(key.substr(STYLE_KEY_PREFIX.length))) {
styles.push(storage[key]); styles.push(storage[key]);
} else if (key.startsWith('tempUsercssCode')) {
leftovers.push(key);
} }
} }
if (leftovers.length) {
chromeLocal.remove(leftovers);
}
return {target: {result: styles}}; return {target: {result: styles}};
}); });
} }

View File

@ -3,6 +3,33 @@
// eslint-disable-next-line no-var // eslint-disable-next-line no-var
var usercssHelper = (() => { var usercssHelper = (() => {
const TEMP_CODE_PREFIX = 'tempUsercssCode';
const TEMP_CODE_CLEANUP_DELAY = 60e3;
let tempCodeLastWriteDate = 0;
if (FIREFOX) {
// the temp code is created on direct installation of usercss URLs in FF
// and can be left behind in case the install page didn't open in time before
// the extension was updated/reloaded/disabled or the browser was closed
setTimeout(function poll() {
if (Date.now() - tempCodeLastWriteDate < TEMP_CODE_CLEANUP_DELAY) {
setTimeout(poll, TEMP_CODE_CLEANUP_DELAY);
return;
}
chrome.storage.local.get(null, storage => {
const leftovers = [];
for (const key in storage) {
if (key.startsWith(TEMP_CODE_PREFIX)) {
leftovers.push(key);
}
}
if (leftovers.length) {
chrome.storage.local.remove(leftovers);
}
});
}, TEMP_CODE_CLEANUP_DELAY);
}
function buildMeta(style) { function buildMeta(style) {
if (style.usercssData) { if (style.usercssData) {
return Promise.resolve(style); return Promise.resolve(style);
@ -93,13 +120,14 @@ var usercssHelper = (() => {
} }
function prefetchCodeForInstallation(tabId, url) { function prefetchCodeForInstallation(tabId, url) {
const key = 'tempUsercssCode' + tabId; const key = TEMP_CODE_PREFIX + tabId;
tempCodeLastWriteDate = Date.now();
Promise.all([ Promise.all([
download(url), download(url),
chromeLocal.setValue(key, {loading: true}), chromeLocal.setValue(key, {loading: true}),
]).then(([code]) => { ]).then(([code]) => {
chromeLocal.setValue(key, code); chromeLocal.setValue(key, code);
setTimeout(() => chromeLocal.remove(key), 60e3); setTimeout(() => chromeLocal.remove(key), TEMP_CODE_CLEANUP_DELAY);
}); });
} }