get rid of switch-fallthrough; reuse requestStyles
This commit is contained in:
parent
80538a17f5
commit
dad1d1fe5f
35
apply.js
35
apply.js
|
@ -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 {
|
||||||
|
if (window != parent) {
|
||||||
matchUrl = parent.location.href;
|
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) {
|
||||||
|
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
|
||||||
if (request.styles == 'DIY') {
|
requestStyles({}, styles => {
|
||||||
getStylesSafe({
|
request.styles = styles;
|
||||||
matchUrl: location.href,
|
applyOnMessage(request);
|
||||||
enabled: true,
|
});
|
||||||
asHash: true,
|
|
||||||
}).then(styles =>
|
|
||||||
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(request.style);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
removeStyle({id: request.style.id, retire: true});
|
removeStyle({id: request.style.id, retire: true});
|
||||||
// fallthrough to 'styleAdded'
|
requestStyles({id: request.style.id});
|
||||||
|
} else {
|
||||||
|
removeStyle(request.style);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
|
||||||
case 'styleAdded':
|
case 'styleAdded':
|
||||||
if (request.style.enabled) {
|
if (request.style.enabled) {
|
||||||
|
|
Loading…
Reference in New Issue
Block a user