From 9fdabe3582aa066bd702031954591d0b7611a020 Mon Sep 17 00:00:00 2001 From: tophf Date: Wed, 10 Jan 2018 19:51:05 +0300 Subject: [PATCH] simplify getStyles * 'asHash' implies 'enabled' * 'length' is added to the style hash --- background/background.js | 14 ++++---------- background/storage.js | 11 ++++++----- background/style-via-api.js | 2 +- content/apply.js | 12 +++++------- 4 files changed, 16 insertions(+), 23 deletions(-) diff --git a/background/background.js b/background/background.js index 59b04b2a..70dbc8a6 100644 --- a/background/background.js +++ b/background/background.js @@ -251,7 +251,7 @@ window.addEventListener('storageReady', function _() { // ************************************************************************* function webNavigationListener(method, {url, tabId, frameId}) { - getStyles({matchUrl: url, enabled: true, asHash: true}).then(styles => { + getStyles({matchUrl: url, asHash: true}).then(styles => { if (method && URLS.supported(url) && tabId >= 0) { if (method === 'styleApply') { handleCssTransitionBug({tabId, frameId, url, styles}); @@ -352,20 +352,14 @@ function updateIcon({tab, styles}) { return; } getTabRealURL(tab) - .then(url => getStyles({matchUrl: url, enabled: true, asHash: true})) + .then(url => getStyles({matchUrl: url, asHash: true})) .then(stylesReceived); - function countStyles(styles) { - if (Array.isArray(styles)) return styles.length; - return Object.keys(styles).reduce((sum, id) => sum + !isNaN(Number(id)), 0); - } - function stylesReceived(styles) { - const numStyles = countStyles(styles); const disableAll = 'disableAll' in styles ? styles.disableAll : prefs.get('disableAll'); - const postfix = disableAll ? 'x' : numStyles === 0 ? 'w' : ''; + const postfix = disableAll ? 'x' : !styles.length ? 'w' : ''; const color = prefs.get(disableAll ? 'badgeDisabled' : 'badgeNormal'); - const text = prefs.get('show-badge') && numStyles ? String(numStyles) : ''; + const text = prefs.get('show-badge') && styles.length ? String(styles.length) : ''; const iconset = ['', 'light/'][prefs.get('iconset')] || ''; let tabIcon = tabIcons.get(tab.id); diff --git a/background/storage.js b/background/storage.js index e9378d27..c95b9652 100644 --- a/background/storage.js +++ b/background/storage.js @@ -202,9 +202,8 @@ function filterStyles({ omitCode, strictRegexp = true, // used by the popup to detect bad regexps } = {}) { - enabled = enabled === null || typeof enabled === 'boolean' ? enabled : - typeof enabled === 'string' ? enabled === 'true' : null; - id = id === null ? null : Number(id); + if (id) id = Number(id); + if (asHash) enabled = true; if ( enabled === null && @@ -217,10 +216,11 @@ function filterStyles({ } if (matchUrl && !URLS.supported(matchUrl)) { - return asHash ? {} : []; + return asHash ? {length: 0} : []; } const blankHash = asHash && { + length: 0, disableAll: prefs.get('disableAll'), exposeIframes: prefs.get('exposeIframes'), }; @@ -286,7 +286,7 @@ function filterStylesInternal({ // of edit.html with a non-existent style id parameter return asHash ? blankHash : []; } - const filtered = asHash ? {} : []; + const filtered = asHash ? {length: 0} : []; const needSections = asHash || matchUrl !== null; const matchUrlBase = matchUrl && matchUrl.includes('#') && matchUrl.split('#', 1)[0]; @@ -307,6 +307,7 @@ function filterStylesInternal({ if (asHash) { if (sections.length) { filtered[style.id] = sections; + filtered.length++; } } else if (matchUrl === null || sections.length) { filtered.push(style); diff --git a/background/style-via-api.js b/background/style-via-api.js index 10962963..5a2c2d3c 100644 --- a/background/style-via-api.js +++ b/background/style-via-api.js @@ -38,7 +38,7 @@ API_METHODS.styleViaAPI = !CHROME && (() => { if (id === null && !ignoreUrlCheck && frameStyles.url === url) { return NOP; } - return getStyles({id, matchUrl: url, enabled: true, asHash: true}).then(styles => { + return getStyles({id, matchUrl: url, asHash: true}).then(styles => { const tasks = []; for (const styleId in styles) { if (isNaN(parseInt(styleId))) { diff --git a/content/apply.js b/content/apply.js index f941cf8f..0ae7d10a 100644 --- a/content/apply.js +++ b/content/apply.js @@ -43,9 +43,8 @@ } const request = Object.assign({ method: 'getStyles', - matchUrl, - enabled: true, asHash: true, + matchUrl, }, options); // On own pages we request the styles directly to minimize delay and flicker if (typeof API === 'function') { @@ -209,14 +208,12 @@ if ('disableAll' in styles) { doDisableAll(styles.disableAll); - delete styles.disableAll; } if ('exposeIframes' in styles) { doExposeIframes(styles.exposeIframes); - delete styles.exposeIframes; } - const gotNewStyles = Object.keys(styles).length || styles.needTransitionPatch; + const gotNewStyles = styles.length || styles.needTransitionPatch; if (gotNewStyles) { if (docRootObserver) { docRootObserver.stop(); @@ -227,12 +224,13 @@ if (styles.needTransitionPatch) { applyTransitionPatch(); - delete styles.needTransitionPatch; } if (gotNewStyles) { for (const id in styles) { - applySections(id, styles[id].map(section => section.code).join('\n')); + const sections = styles[id]; + if (!Array.isArray(sections)) continue; + applySections(id, sections.map(({code}) => code).join('\n')); } docRootObserver.start({sort: true}); }