simplify getStyles

* 'asHash' implies 'enabled'
* 'length' is added to the style hash
This commit is contained in:
tophf 2018-01-10 19:51:05 +03:00
parent 11dbfea897
commit 9fdabe3582
4 changed files with 16 additions and 23 deletions

View File

@ -251,7 +251,7 @@ window.addEventListener('storageReady', function _() {
// ************************************************************************* // *************************************************************************
function webNavigationListener(method, {url, tabId, frameId}) { 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 && URLS.supported(url) && tabId >= 0) {
if (method === 'styleApply') { if (method === 'styleApply') {
handleCssTransitionBug({tabId, frameId, url, styles}); handleCssTransitionBug({tabId, frameId, url, styles});
@ -352,20 +352,14 @@ function updateIcon({tab, styles}) {
return; return;
} }
getTabRealURL(tab) getTabRealURL(tab)
.then(url => getStyles({matchUrl: url, enabled: true, asHash: true})) .then(url => getStyles({matchUrl: url, asHash: true}))
.then(stylesReceived); .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) { function stylesReceived(styles) {
const numStyles = countStyles(styles);
const disableAll = 'disableAll' in styles ? styles.disableAll : prefs.get('disableAll'); 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 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')] || ''; const iconset = ['', 'light/'][prefs.get('iconset')] || '';
let tabIcon = tabIcons.get(tab.id); let tabIcon = tabIcons.get(tab.id);

View File

@ -202,9 +202,8 @@ function filterStyles({
omitCode, omitCode,
strictRegexp = true, // used by the popup to detect bad regexps strictRegexp = true, // used by the popup to detect bad regexps
} = {}) { } = {}) {
enabled = enabled === null || typeof enabled === 'boolean' ? enabled : if (id) id = Number(id);
typeof enabled === 'string' ? enabled === 'true' : null; if (asHash) enabled = true;
id = id === null ? null : Number(id);
if ( if (
enabled === null && enabled === null &&
@ -217,10 +216,11 @@ function filterStyles({
} }
if (matchUrl && !URLS.supported(matchUrl)) { if (matchUrl && !URLS.supported(matchUrl)) {
return asHash ? {} : []; return asHash ? {length: 0} : [];
} }
const blankHash = asHash && { const blankHash = asHash && {
length: 0,
disableAll: prefs.get('disableAll'), disableAll: prefs.get('disableAll'),
exposeIframes: prefs.get('exposeIframes'), exposeIframes: prefs.get('exposeIframes'),
}; };
@ -286,7 +286,7 @@ function filterStylesInternal({
// of edit.html with a non-existent style id parameter // of edit.html with a non-existent style id parameter
return asHash ? blankHash : []; return asHash ? blankHash : [];
} }
const filtered = asHash ? {} : []; const filtered = asHash ? {length: 0} : [];
const needSections = asHash || matchUrl !== null; const needSections = asHash || matchUrl !== null;
const matchUrlBase = matchUrl && matchUrl.includes('#') && matchUrl.split('#', 1)[0]; const matchUrlBase = matchUrl && matchUrl.includes('#') && matchUrl.split('#', 1)[0];
@ -307,6 +307,7 @@ function filterStylesInternal({
if (asHash) { if (asHash) {
if (sections.length) { if (sections.length) {
filtered[style.id] = sections; filtered[style.id] = sections;
filtered.length++;
} }
} else if (matchUrl === null || sections.length) { } else if (matchUrl === null || sections.length) {
filtered.push(style); filtered.push(style);

View File

@ -38,7 +38,7 @@ API_METHODS.styleViaAPI = !CHROME && (() => {
if (id === null && !ignoreUrlCheck && frameStyles.url === url) { if (id === null && !ignoreUrlCheck && frameStyles.url === url) {
return NOP; return NOP;
} }
return getStyles({id, matchUrl: url, enabled: true, asHash: true}).then(styles => { return getStyles({id, matchUrl: url, asHash: true}).then(styles => {
const tasks = []; const tasks = [];
for (const styleId in styles) { for (const styleId in styles) {
if (isNaN(parseInt(styleId))) { if (isNaN(parseInt(styleId))) {

View File

@ -43,9 +43,8 @@
} }
const request = Object.assign({ const request = Object.assign({
method: 'getStyles', method: 'getStyles',
matchUrl,
enabled: true,
asHash: true, asHash: true,
matchUrl,
}, options); }, options);
// On own pages we request the styles directly to minimize delay and flicker // On own pages we request the styles directly to minimize delay and flicker
if (typeof API === 'function') { if (typeof API === 'function') {
@ -209,14 +208,12 @@
if ('disableAll' in styles) { if ('disableAll' in styles) {
doDisableAll(styles.disableAll); doDisableAll(styles.disableAll);
delete styles.disableAll;
} }
if ('exposeIframes' in styles) { if ('exposeIframes' in styles) {
doExposeIframes(styles.exposeIframes); doExposeIframes(styles.exposeIframes);
delete styles.exposeIframes;
} }
const gotNewStyles = Object.keys(styles).length || styles.needTransitionPatch; const gotNewStyles = styles.length || styles.needTransitionPatch;
if (gotNewStyles) { if (gotNewStyles) {
if (docRootObserver) { if (docRootObserver) {
docRootObserver.stop(); docRootObserver.stop();
@ -227,12 +224,13 @@
if (styles.needTransitionPatch) { if (styles.needTransitionPatch) {
applyTransitionPatch(); applyTransitionPatch();
delete styles.needTransitionPatch;
} }
if (gotNewStyles) { if (gotNewStyles) {
for (const id in styles) { 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}); docRootObserver.start({sort: true});
} }