Change: switch to msg.js

This commit is contained in:
eight 2018-10-06 13:27:58 +08:00
parent fa3127d988
commit 095998f07c
10 changed files with 69 additions and 99 deletions

View File

@ -4,7 +4,7 @@ global handleCssTransitionBug detectSloppyRegexps
global openEditor global openEditor
global styleViaAPI global styleViaAPI
global loadScript global loadScript
global usercss styleManager db global usercss styleManager db msg
*/ */
'use strict'; 'use strict';
@ -280,7 +280,6 @@ window.addEventListener('storageReady', function _() {
}; };
const pingCS = (cs, {id, url}) => { const pingCS = (cs, {id, url}) => {
const maybeInject = ;
cs.matches.some(match => { cs.matches.some(match => {
if ((match === ALL_URLS || url.match(match)) && if ((match === ALL_URLS || url.match(match)) &&
(!url.startsWith('chrome') || url === NTP)) { (!url.startsWith('chrome') || url === NTP)) {

View File

@ -54,40 +54,6 @@ const styleManager = (() => {
.then(() => id); .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) { function isExtensionUrl(url) {
return /^\w+?-extension:\/\//.test(url); return /^\w+?-extension:\/\//.test(url);
} }
@ -166,7 +132,7 @@ const styleManager = (() => {
} }
function installStyle(data) { function installStyle(data) {
let style = styles.get(style.id); const style = styles.get(data.id);
if (!style) { if (!style) {
data = Object.assign(createNewStyle(), data); data = Object.assign(createNewStyle(), data);
} else { } else {
@ -178,7 +144,14 @@ const styleManager = (() => {
data.originalDigest = digest; data.originalDigest = digest;
return saveStyle(data); return saveStyle(data);
}) })
.then(newData => { .then(newData =>
broadcastStyleUpdated(newData)
.then(() => newData)
);
}
function broadcastStyleUpdated(newData) {
const style = styles.get(newData.id);
if (!style) { if (!style) {
// new style // new style
const appliesTo = new Set(); const appliesTo = new Set();
@ -186,9 +159,9 @@ const styleManager = (() => {
appliesTo, appliesTo,
data: newData data: newData
}); });
return Promis.all([ return Promise.all([
msg.broadcastExtension({method: 'styleAdded', style: getStyleWithNoCode(newData)}), msg.broadcastExtension({method: 'styleAdded', style: getStyleWithNoCode(newData)}),
msg.broadcastTab(tab => emitStyleAdded(tab, newData, appliesTo)) msg.broadcastTab(tab => getStyleAddedMessage(tab, newData, appliesTo))
]); ]);
} else { } else {
const excluded = new Set(); const excluded = new Set();
@ -208,7 +181,7 @@ const styleManager = (() => {
} }
style.appliesTo = new Set(updated.keys()); style.appliesTo = new Set(updated.keys());
return Promise.all([ return Promise.all([
msg.broadcastExtension({method: 'styleUpdated', style: getStyleWithNoCode(newData)}) msg.broadcastExtension({method: 'styleUpdated', style: getStyleWithNoCode(newData)}),
msg.broadcastTab(tab => { msg.broadcastTab(tab => {
if (excluded.has(tab.url)) { if (excluded.has(tab.url)) {
return { return {
@ -219,21 +192,16 @@ const styleManager = (() => {
if (updated.has(tab.url)) { if (updated.has(tab.url)) {
return { return {
method: 'styleUpdated', method: 'styleUpdated',
style: { style: {id: newData.id, sections: updated.get(tab.url)}
id: newData.id,
sections: updated.get(tab.url)
};
}; };
} }
return emitStyleAdded(tab, newData, style.appliesTo); return getStyleAddedMessage(tab, newData, style.appliesTo);
}) })
]) ]);
} }
return style;
});
} }
function emitStyleAdded(tab, data, appliesTo) { function getStyleAddedMessage(tab, data, appliesTo) {
const code = getAppliedCode(tab.url, data); const code = getAppliedCode(tab.url, data);
if (!code) { if (!code) {
return; return;
@ -266,7 +234,7 @@ const styleManager = (() => {
if (!style.name) { if (!style.name) {
throw new Error('style name is empty'); throw new Error('style name is empty');
} }
return db.exec('put', style); return db.exec('put', style)
.then(event => { .then(event => {
if (style.id == null) { if (style.id == null) {
style.id = event.target.result; style.id = event.target.result;

View File

@ -1,4 +1,5 @@
/* eslint no-var: 0 */ /* eslint no-var: 0 */
/* global msg */
'use strict'; 'use strict';
(() => { (() => {

View File

@ -1,4 +1,4 @@
/* global cloneInto */ /* global cloneInto msg */
'use strict'; 'use strict';
(() => { (() => {

View File

@ -1,4 +1,4 @@
/* global regExpTester debounce messageBox CodeMirror template colorMimicry */ /* global regExpTester debounce messageBox CodeMirror template colorMimicry msg */
'use strict'; 'use strict';
function createAppliesToLineWidget(cm) { function createAppliesToLineWidget(cm) {

View File

@ -7,7 +7,7 @@ global beautify
global initWithSectionStyle addSections removeSection getSectionsHashes global initWithSectionStyle addSections removeSection getSectionsHashes
global sectionsToMozFormat global sectionsToMozFormat
global exclusions global exclusions
global moveFocus editorWorker global moveFocus editorWorker msg
*/ */
'use strict'; 'use strict';
@ -27,7 +27,7 @@ let editor;
document.addEventListener('visibilitychange', beforeUnload); document.addEventListener('visibilitychange', beforeUnload);
msg.on(onRuntimeMessage); msg.onExtension(onRuntimeMessage);
preinit(); preinit();

View File

@ -85,12 +85,14 @@ const msg = (() => {
function broadcastTab(data, filter, options, ignoreExtension = false, target = 'tab') { function broadcastTab(data, filter, options, ignoreExtension = false, target = 'tab') {
return tabQuery() return tabQuery()
// TODO: send to activated tabs first?
.then(tabs => { .then(tabs => {
const requests = []; const requests = [];
for (const tab of tabs) { for (const tab of tabs) {
const isExtension = tab.url.startsWith(EXTENSION_URL); const isExtension = tab.url.startsWith(EXTENSION_URL);
if ( if (
tab.discarded || tab.discarded ||
// FIXME: use `URLS.supported`?
!/^(http|ftp|file)/.test(tab.url) && !/^(http|ftp|file)/.test(tab.url) &&
!tab.url.startsWith('chrome://newtab/') && !tab.url.startsWith('chrome://newtab/') &&
!isExtension || !isExtension ||

View File

@ -4,7 +4,7 @@ global filtersSelector filterAndAppend urlFilterParam showFiltersStats
global checkUpdate handleUpdateInstalled global checkUpdate handleUpdateInstalled
global objectDiff global objectDiff
global configDialog global configDialog
global sorter global sorter msg
*/ */
'use strict'; 'use strict';

View File

@ -1,4 +1,4 @@
/* global messageBox */ /* global messageBox msg */
'use strict'; 'use strict';
setupLivePrefs(); setupLivePrefs();

View File

@ -1,6 +1,6 @@
/* /*
global configDialog hotkeys global configDialog hotkeys
global popupExclusions promisify onTabReady global popupExclusions promisify onTabReady msg
*/ */
'use strict'; 'use strict';