From 405400de68d96057ed93533c78214b4c0e253047 Mon Sep 17 00:00:00 2001 From: tophf Date: Sun, 10 Dec 2017 10:04:13 +0300 Subject: [PATCH] move 'tempUsercssCode' cleanup to usercss helper --- background/storage.js | 6 ------ background/usercss-helper.js | 32 ++++++++++++++++++++++++++++++-- 2 files changed, 30 insertions(+), 8 deletions(-) diff --git a/background/storage.js b/background/storage.js index c750a45a..0db0ea64 100644 --- a/background/storage.js +++ b/background/storage.js @@ -195,18 +195,12 @@ function dbExecChromeStorage(method, data) { case 'getAll': return chromeLocal.get(null).then(storage => { const styles = []; - const leftovers = []; for (const key in storage) { if (key.startsWith(STYLE_KEY_PREFIX) && Number(key.substr(STYLE_KEY_PREFIX.length))) { styles.push(storage[key]); - } else if (key.startsWith('tempUsercssCode')) { - leftovers.push(key); } } - if (leftovers.length) { - chromeLocal.remove(leftovers); - } return {target: {result: styles}}; }); } diff --git a/background/usercss-helper.js b/background/usercss-helper.js index a114e9c5..6eb3684a 100644 --- a/background/usercss-helper.js +++ b/background/usercss-helper.js @@ -3,6 +3,33 @@ // eslint-disable-next-line no-var 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) { if (style.usercssData) { return Promise.resolve(style); @@ -93,13 +120,14 @@ var usercssHelper = (() => { } function prefetchCodeForInstallation(tabId, url) { - const key = 'tempUsercssCode' + tabId; + const key = TEMP_CODE_PREFIX + tabId; + tempCodeLastWriteDate = Date.now(); Promise.all([ download(url), chromeLocal.setValue(key, {loading: true}), ]).then(([code]) => { chromeLocal.setValue(key, code); - setTimeout(() => chromeLocal.remove(key), 60e3); + setTimeout(() => chromeLocal.remove(key), TEMP_CODE_CLEANUP_DELAY); }); }