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}) {
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);

View File

@ -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);

View File

@ -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))) {

View File

@ -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});
}