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

View File

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

View File

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

View File

@ -1,4 +1,4 @@
/* global cloneInto */
/* global cloneInto msg */
'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';
function createAppliesToLineWidget(cm) {

View File

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

View File

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

View File

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

View File

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

View File

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