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)
.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;
}

View File

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