get rid of switch-fallthrough; reuse requestStyles

This commit is contained in:
tophf 2017-04-12 15:31:05 +03:00
parent 80538a17f5
commit dad1d1fe5f

View File

@ -20,15 +20,17 @@ if (!isOwnPage) {
window.addEventListener(chrome.runtime.id, orphanCheck, true); window.addEventListener(chrome.runtime.id, orphanCheck, true);
} }
function requestStyles(options) { function requestStyles(options, callback = applyStyles) {
var matchUrl = location.href; var matchUrl = location.href;
try { if (!matchUrl.match(/^(http|file|chrome|ftp)/)) {
// dynamic about: and javascript: iframes don't have an URL yet // dynamic about: and javascript: iframes don't have an URL yet
// so we'll try the parent frame which is guaranteed to have a real URL // so we'll try the parent frame which is guaranteed to have a real URL
if (!matchUrl.match(/^(http|file|chrome|ftp)/) && window != parent) { try {
matchUrl = parent.location.href; if (window != parent) {
} matchUrl = parent.location.href;
} catch (e) {} }
} catch (e) {}
}
const request = Object.assign({ const request = Object.assign({
method: 'getStyles', method: 'getStyles',
matchUrl, matchUrl,
@ -40,23 +42,21 @@ function requestStyles(options) {
// unless Chrome is still starting up and the background page isn't fully loaded. // unless Chrome is still starting up and the background page isn't fully loaded.
// (Note: in this case the function may be invoked again from applyStyles.) // (Note: in this case the function may be invoked again from applyStyles.)
if (typeof getStylesSafe !== 'undefined') { if (typeof getStylesSafe !== 'undefined') {
getStylesSafe(request).then(applyStyles); getStylesSafe(request).then(callback);
} else { } else {
chrome.runtime.sendMessage(request, applyStyles); chrome.runtime.sendMessage(request, callback);
} }
} }
function applyOnMessage(request, sender, sendResponse) { function applyOnMessage(request, sender, sendResponse) {
// Do-It-Yourself tells our built-in pages to fetch the styles directly
// which is faster because IPC messaging JSON-ifies everything internally
if (request.styles == 'DIY') { if (request.styles == 'DIY') {
getStylesSafe({ // Do-It-Yourself tells our built-in pages to fetch the styles directly
matchUrl: location.href, // which is faster because IPC messaging JSON-ifies everything internally
enabled: true, requestStyles({}, styles => {
asHash: true, request.styles = styles;
}).then(styles => applyOnMessage(request);
applyOnMessage(Object.assign(request, {styles}))); });
return; return;
} }
switch (request.method) { switch (request.method) {
@ -70,12 +70,13 @@ function applyOnMessage(request, sender, sendResponse) {
applyStyleState(request.style); applyStyleState(request.style);
break; break;
} }
if (!request.style.enabled) { if (request.style.enabled) {
removeStyle({id: request.style.id, retire: true});
requestStyles({id: request.style.id});
} else {
removeStyle(request.style); removeStyle(request.style);
break;
} }
removeStyle({id: request.style.id, retire: true}); break;
// fallthrough to 'styleAdded'
case 'styleAdded': case 'styleAdded':
if (request.style.enabled) { if (request.style.enabled) {