From 095998f07c0e6ffa6c4552e88a3becf2450051c7 Mon Sep 17 00:00:00 2001 From: eight Date: Sat, 6 Oct 2018 13:27:58 +0800 Subject: [PATCH] Change: switch to msg.js --- background/background.js | 3 +- background/style-manager.js | 148 +++++++++++------------------ content/apply.js | 1 + content/install-hook-userstyles.js | 2 +- edit/applies-to-line-widget.js | 2 +- edit/edit.js | 4 +- js/msg.js | 2 + manage/manage.js | 2 +- options/options.js | 2 +- popup/popup.js | 2 +- 10 files changed, 69 insertions(+), 99 deletions(-) diff --git a/background/background.js b/background/background.js index 1909b136..33009abf 100644 --- a/background/background.js +++ b/background/background.js @@ -4,7 +4,7 @@ global handleCssTransitionBug detectSloppyRegexps global openEditor global styleViaAPI global loadScript -global usercss styleManager db +global usercss styleManager db msg */ 'use strict'; @@ -280,7 +280,6 @@ window.addEventListener('storageReady', function _() { }; const pingCS = (cs, {id, url}) => { - const maybeInject = ; cs.matches.some(match => { if ((match === ALL_URLS || url.match(match)) && (!url.startsWith('chrome') || url === NTP)) { diff --git a/background/style-manager.js b/background/style-manager.js index c90b9286..a7a06c7c 100644 --- a/background/style-manager.js +++ b/background/style-manager.js @@ -54,40 +54,6 @@ const styleManager = (() => { .then(() => id); } - function emitChangesToTabs(getMessage, appliesTo, ignoreExtension = false) { - // FIXME: does `discarded` work in old browsers? - // TODO: send to activated tabs first? - return tabQuery({discarded: false}) - .then(tabs => { - const pending = []; - for (const tab of tabs) { - if ( - !URLS.supported(tab.url) || - ignoreExtension && isExtensionUrl(tab.url) || - appliesTo && !appliesTo.has(tab.url) - ) { - continue; - } - const message = typeof getMessage === 'function' ? getMessage(tab) : getMessage; - if (message) { - pending.push(tabSendMessage(tab.id, message)); - } - } - return Promise.all(pending); - }); - } - - function emitChanges(message, appliesTo) { - const pending = runtimeSendMessage(message); - if (appliesTo && [...appliesTo].every(isExtensionUrl)) { - return pending; - } - return Promise.all([ - pending, - emitChangesToTabs(message, appliesTo, true), - ]); - } - function isExtensionUrl(url) { return /^\w+?-extension:\/\//.test(url); } @@ -166,7 +132,7 @@ const styleManager = (() => { } function installStyle(data) { - let style = styles.get(style.id); + const style = styles.get(data.id); if (!style) { data = Object.assign(createNewStyle(), data); } else { @@ -178,62 +144,64 @@ const styleManager = (() => { data.originalDigest = digest; return saveStyle(data); }) - .then(newData => { - if (!style) { - // new style - const appliesTo = new Set(); - styles.set(newData.id, { - appliesTo, - data: newData - }); - return Promis.all([ - msg.broadcastExtension({method: 'styleAdded', style: getStyleWithNoCode(newData)}), - msg.broadcastTab(tab => emitStyleAdded(tab, newData, appliesTo)) - ]); - } else { - const excluded = new Set(); - const updated = new Map(); - for (const url of style.appliesTo) { - const code = getAppliedCode(url, newData); - const cache = cachedStyleForUrl.get(url); - if (!code) { - excluded.add(url); - if (cache) { - delete cache[newData.id]; - } - } else { - updated.set(url, code); - cache[newData.id] = code; - } - } - style.appliesTo = new Set(updated.keys()); - return Promise.all([ - msg.broadcastExtension({method: 'styleUpdated', style: getStyleWithNoCode(newData)}) - msg.broadcastTab(tab => { - if (excluded.has(tab.url)) { - return { - method: 'styleDeleted', - style: {id: newData.id} - }; - } - if (updated.has(tab.url)) { - return { - method: 'styleUpdated', - style: { - id: newData.id, - sections: updated.get(tab.url) - }; - }; - } - return emitStyleAdded(tab, newData, style.appliesTo); - }) - ]) - } - return style; - }); + .then(newData => + broadcastStyleUpdated(newData) + .then(() => newData) + ); } - function emitStyleAdded(tab, data, appliesTo) { + function broadcastStyleUpdated(newData) { + const style = styles.get(newData.id); + if (!style) { + // new style + const appliesTo = new Set(); + styles.set(newData.id, { + appliesTo, + data: newData + }); + return Promise.all([ + msg.broadcastExtension({method: 'styleAdded', style: getStyleWithNoCode(newData)}), + msg.broadcastTab(tab => getStyleAddedMessage(tab, newData, appliesTo)) + ]); + } else { + const excluded = new Set(); + const updated = new Map(); + for (const url of style.appliesTo) { + const code = getAppliedCode(url, newData); + const cache = cachedStyleForUrl.get(url); + if (!code) { + excluded.add(url); + if (cache) { + delete cache[newData.id]; + } + } else { + updated.set(url, code); + cache[newData.id] = code; + } + } + style.appliesTo = new Set(updated.keys()); + return Promise.all([ + msg.broadcastExtension({method: 'styleUpdated', style: getStyleWithNoCode(newData)}), + msg.broadcastTab(tab => { + if (excluded.has(tab.url)) { + return { + method: 'styleDeleted', + style: {id: newData.id} + }; + } + if (updated.has(tab.url)) { + return { + method: 'styleUpdated', + style: {id: newData.id, sections: updated.get(tab.url)} + }; + } + return getStyleAddedMessage(tab, newData, style.appliesTo); + }) + ]); + } + } + + function getStyleAddedMessage(tab, data, appliesTo) { const code = getAppliedCode(tab.url, data); if (!code) { return; @@ -266,7 +234,7 @@ const styleManager = (() => { if (!style.name) { throw new Error('style name is empty'); } - return db.exec('put', style); + return db.exec('put', style) .then(event => { if (style.id == null) { style.id = event.target.result; diff --git a/content/apply.js b/content/apply.js index bf7a0834..1986e147 100644 --- a/content/apply.js +++ b/content/apply.js @@ -1,4 +1,5 @@ /* eslint no-var: 0 */ +/* global msg */ 'use strict'; (() => { diff --git a/content/install-hook-userstyles.js b/content/install-hook-userstyles.js index 0ae2a4f6..94a23cf4 100644 --- a/content/install-hook-userstyles.js +++ b/content/install-hook-userstyles.js @@ -1,4 +1,4 @@ -/* global cloneInto */ +/* global cloneInto msg */ 'use strict'; (() => { diff --git a/edit/applies-to-line-widget.js b/edit/applies-to-line-widget.js index 62c7b506..21d18c30 100644 --- a/edit/applies-to-line-widget.js +++ b/edit/applies-to-line-widget.js @@ -1,4 +1,4 @@ -/* global regExpTester debounce messageBox CodeMirror template colorMimicry */ +/* global regExpTester debounce messageBox CodeMirror template colorMimicry msg */ 'use strict'; function createAppliesToLineWidget(cm) { diff --git a/edit/edit.js b/edit/edit.js index 31aa9e4b..d1ef3bed 100644 --- a/edit/edit.js +++ b/edit/edit.js @@ -7,7 +7,7 @@ global beautify global initWithSectionStyle addSections removeSection getSectionsHashes global sectionsToMozFormat global exclusions -global moveFocus editorWorker +global moveFocus editorWorker msg */ 'use strict'; @@ -27,7 +27,7 @@ let editor; document.addEventListener('visibilitychange', beforeUnload); -msg.on(onRuntimeMessage); +msg.onExtension(onRuntimeMessage); preinit(); diff --git a/js/msg.js b/js/msg.js index 20a8dc9a..4ca588c3 100644 --- a/js/msg.js +++ b/js/msg.js @@ -85,12 +85,14 @@ const msg = (() => { function broadcastTab(data, filter, options, ignoreExtension = false, target = 'tab') { return tabQuery() + // TODO: send to activated tabs first? .then(tabs => { const requests = []; for (const tab of tabs) { const isExtension = tab.url.startsWith(EXTENSION_URL); if ( tab.discarded || + // FIXME: use `URLS.supported`? !/^(http|ftp|file)/.test(tab.url) && !tab.url.startsWith('chrome://newtab/') && !isExtension || diff --git a/manage/manage.js b/manage/manage.js index aa55af4b..91e73917 100644 --- a/manage/manage.js +++ b/manage/manage.js @@ -4,7 +4,7 @@ global filtersSelector filterAndAppend urlFilterParam showFiltersStats global checkUpdate handleUpdateInstalled global objectDiff global configDialog -global sorter +global sorter msg */ 'use strict'; diff --git a/options/options.js b/options/options.js index ee150239..36ac543e 100644 --- a/options/options.js +++ b/options/options.js @@ -1,4 +1,4 @@ -/* global messageBox */ +/* global messageBox msg */ 'use strict'; setupLivePrefs(); diff --git a/popup/popup.js b/popup/popup.js index 81516e02..3cfa9917 100644 --- a/popup/popup.js +++ b/popup/popup.js @@ -1,6 +1,6 @@ /* global configDialog hotkeys -global popupExclusions promisify onTabReady +global popupExclusions promisify onTabReady msg */ 'use strict';