Drop requestStyles

This commit is contained in:
eight 2018-10-06 17:47:43 +08:00
parent 75f2561154
commit 1c635b5bc1
2 changed files with 47 additions and 30 deletions

View File

@ -41,13 +41,19 @@ const styleManager = (() => {
return saveStyle(newData) return saveStyle(newData)
.then(newData => { .then(newData => {
style.data = 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 = { const message = {
method: 'styleUpdated', method: 'styleUpdated',
codeIsUpdated: false, codeIsUpdated: false,
style: {id, enabled} style: {id, enabled}
}; };
if ([...style.appliesTo].every(isExtensionUrl)) { if ([...style.appliesTo].every(isExtensionUrl)) {
return msg.broadcastExtension(message); return msg.broadcastExtension(message, 'both');
} }
return msg.broadcast(message, tab => style.appliesTo.has(tab.url)); return msg.broadcast(message, tab => style.appliesTo.has(tab.url));
}) })
@ -271,7 +277,7 @@ const styleManager = (() => {
.map(k => getStyleWithNoCode(styles.get(Number(k)).data)); .map(k => getStyleWithNoCode(styles.get(Number(k)).data));
} }
function getSectionsByUrl(url, filterId) { function getSectionsByUrl(url, filter) {
let cache = cachedStyleForUrl.get(url); let cache = cachedStyleForUrl.get(url);
if (!cache) { if (!cache) {
cache = {}; cache = {};
@ -288,8 +294,16 @@ const styleManager = (() => {
} }
cachedStyleForUrl.set(url, cache); cachedStyleForUrl.set(url, cache);
} }
if (filterId) { if (filter && filter.id) {
return {[filterId]: cache[filterId]}; 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; return cache;
} }

View File

@ -26,13 +26,19 @@
const FF_BUG461 = !CHROME && !isOwnPage && !Event.prototype.getPreventDefault; const FF_BUG461 = !CHROME && !isOwnPage && !Event.prototype.getPreventDefault;
const pageContextQueue = []; const pageContextQueue = [];
requestStyles({}, styles => { // FIXME: styleViaAPI
// FIXME: need transition patch? if (!chrome.app && document instanceof XMLDocument) {
// if (needTransitionPatch(styles)) { API.styleViaAPI({action: 'styleApply'});
// applyTransitionPatch(); } else {
// } API.getSectionsByUrl(getMatchUrl(), {enabled: true})
applyStyles(styles.filter(s => s.enabled)); .then(result => {
}); const styles = Object.values(result);
if (styles.some(s => s.code.includes('transition'))) {
applyTransitionPatch();
}
applyStyles(styles);
});
}
msg.onTab(applyOnMessage); msg.onTab(applyOnMessage);
window.applyOnMessage = applyOnMessage; window.applyOnMessage = applyOnMessage;
@ -41,17 +47,14 @@
window.addEventListener(chrome.runtime.id, orphanCheck, true); window.addEventListener(chrome.runtime.id, orphanCheck, true);
} }
function requestStyles(options, callback = applyStyles) { // function requestStyles(options, callback = applyStyles) {
if (!chrome.app && document instanceof XMLDocument) {
API.styleViaAPI({action: 'styleApply'});
return;
}
// FIXME: options? // FIXME: options?
// FIXME: getStylesFallback? // FIXME: getStylesFallback?
API.getSectionsByUrl(getMatchUrl()) // API.getSectionsByUrl(getMatchUrl(), {enabled: true})
.then(buildSections) // .then(Object.values);
.then(callback); // .then(buildSections)
} // .then(callback);
// }
function getMatchUrl() { function getMatchUrl() {
var matchUrl = location.href; var matchUrl = location.href;
@ -94,15 +97,15 @@
} }
function applyOnMessage(request) { function applyOnMessage(request) {
if (request.styles === 'DIY') { // if (request.styles === 'DIY') {
// Do-It-Yourself tells our built-in pages to fetch the styles directly // Do-It-Yourself tells our built-in pages to fetch the styles directly
// which is faster because IPC messaging JSON-ifies everything internally // which is faster because IPC messaging JSON-ifies everything internally
requestStyles({}, styles => { // requestStyles({}, styles => {
request.styles = styles; // request.styles = styles;
applyOnMessage(request); // applyOnMessage(request);
}); // });
return; // return;
} // }
if (!chrome.app && document instanceof XMLDocument && request.method !== 'ping') { if (!chrome.app && document instanceof XMLDocument && request.method !== 'ping') {
request.action = request.method; request.action = request.method;
@ -127,7 +130,7 @@
} }
if (request.style.enabled) { if (request.style.enabled) {
removeStyle({id: request.style.id, retire: true}); removeStyle({id: request.style.id, retire: true});
API.getSectionsByUrl(getMatchUrl(), request.style.id) API.getSectionsByUrl(getMatchUrl(), {id: request.style.id})
.then(buildSections) .then(buildSections)
.then(applyStyles); .then(applyStyles);
} else { } else {
@ -137,7 +140,7 @@
case 'styleAdded': case 'styleAdded':
if (request.style.enabled) { if (request.style.enabled) {
API.getSectionsByUrl(getMatchUrl(), request.style.id) API.getSectionsByUrl(getMatchUrl(), {id: request.style.id})
.then(buildSections) .then(buildSections)
.then(applyStyles); .then(applyStyles);
} }
@ -203,7 +206,7 @@
addStyleElement(inCache); addStyleElement(inCache);
disabledElements.delete(id); disabledElements.delete(id);
} else { } else {
API.getSectionsByUrl(getMatchUrl(), id) API.getSectionsByUrl(getMatchUrl(), {id})
.then(buildSections) .then(buildSections)
.then(applyStyles); .then(applyStyles);
} }