From 1c635b5bc1e8b5347c1171cb01a32852030c0a91 Mon Sep 17 00:00:00 2001 From: eight Date: Sat, 6 Oct 2018 17:47:43 +0800 Subject: [PATCH] Drop requestStyles --- background/style-manager.js | 22 ++++++++++++--- content/apply.js | 55 +++++++++++++++++++------------------ 2 files changed, 47 insertions(+), 30 deletions(-) diff --git a/background/style-manager.js b/background/style-manager.js index 1b794096..74730dfe 100644 --- a/background/style-manager.js +++ b/background/style-manager.js @@ -41,13 +41,19 @@ const styleManager = (() => { return saveStyle(newData) .then(newData => { style.data = newData; + for (const url of style.appliesTo) { + const cache = cachedStyleForUrl.get(url); + if (cache) { + cache[newData.id].enabled = newData.enabled; + } + } const message = { method: 'styleUpdated', codeIsUpdated: false, style: {id, enabled} }; if ([...style.appliesTo].every(isExtensionUrl)) { - return msg.broadcastExtension(message); + return msg.broadcastExtension(message, 'both'); } return msg.broadcast(message, tab => style.appliesTo.has(tab.url)); }) @@ -271,7 +277,7 @@ const styleManager = (() => { .map(k => getStyleWithNoCode(styles.get(Number(k)).data)); } - function getSectionsByUrl(url, filterId) { + function getSectionsByUrl(url, filter) { let cache = cachedStyleForUrl.get(url); if (!cache) { cache = {}; @@ -288,8 +294,16 @@ const styleManager = (() => { } cachedStyleForUrl.set(url, cache); } - if (filterId) { - return {[filterId]: cache[filterId]}; + if (filter && filter.id) { + return {[filter.id]: cache[filter.id]}; + } + if (filter) { + return Object.values(cache) + .filter(s => filterMatchStyle(filter, s)) + .reduce((o, v) => { + o[v.id] = v; + return o; + }, {}); } return cache; } diff --git a/content/apply.js b/content/apply.js index 2ae01bcf..36dde7c7 100644 --- a/content/apply.js +++ b/content/apply.js @@ -26,13 +26,19 @@ const FF_BUG461 = !CHROME && !isOwnPage && !Event.prototype.getPreventDefault; const pageContextQueue = []; - requestStyles({}, styles => { - // FIXME: need transition patch? - // if (needTransitionPatch(styles)) { - // applyTransitionPatch(); - // } - applyStyles(styles.filter(s => s.enabled)); - }); + // FIXME: styleViaAPI + if (!chrome.app && document instanceof XMLDocument) { + API.styleViaAPI({action: 'styleApply'}); + } else { + API.getSectionsByUrl(getMatchUrl(), {enabled: true}) + .then(result => { + const styles = Object.values(result); + if (styles.some(s => s.code.includes('transition'))) { + applyTransitionPatch(); + } + applyStyles(styles); + }); + } msg.onTab(applyOnMessage); window.applyOnMessage = applyOnMessage; @@ -41,17 +47,14 @@ window.addEventListener(chrome.runtime.id, orphanCheck, true); } - function requestStyles(options, callback = applyStyles) { - if (!chrome.app && document instanceof XMLDocument) { - API.styleViaAPI({action: 'styleApply'}); - return; - } + // function requestStyles(options, callback = applyStyles) { // FIXME: options? // FIXME: getStylesFallback? - API.getSectionsByUrl(getMatchUrl()) - .then(buildSections) - .then(callback); - } + // API.getSectionsByUrl(getMatchUrl(), {enabled: true}) + // .then(Object.values); + // .then(buildSections) + // .then(callback); + // } function getMatchUrl() { var matchUrl = location.href; @@ -94,15 +97,15 @@ } function applyOnMessage(request) { - if (request.styles === 'DIY') { + // if (request.styles === 'DIY') { // Do-It-Yourself tells our built-in pages to fetch the styles directly // which is faster because IPC messaging JSON-ifies everything internally - requestStyles({}, styles => { - request.styles = styles; - applyOnMessage(request); - }); - return; - } + // requestStyles({}, styles => { + // request.styles = styles; + // applyOnMessage(request); + // }); + // return; + // } if (!chrome.app && document instanceof XMLDocument && request.method !== 'ping') { request.action = request.method; @@ -127,7 +130,7 @@ } if (request.style.enabled) { removeStyle({id: request.style.id, retire: true}); - API.getSectionsByUrl(getMatchUrl(), request.style.id) + API.getSectionsByUrl(getMatchUrl(), {id: request.style.id}) .then(buildSections) .then(applyStyles); } else { @@ -137,7 +140,7 @@ case 'styleAdded': if (request.style.enabled) { - API.getSectionsByUrl(getMatchUrl(), request.style.id) + API.getSectionsByUrl(getMatchUrl(), {id: request.style.id}) .then(buildSections) .then(applyStyles); } @@ -203,7 +206,7 @@ addStyleElement(inCache); disabledElements.delete(id); } else { - API.getSectionsByUrl(getMatchUrl(), id) + API.getSectionsByUrl(getMatchUrl(), {id}) .then(buildSections) .then(applyStyles); }