Fix: use a simple eval to execute page scripts

This commit is contained in:
eight 2018-10-12 15:03:00 +08:00
parent 405b7f8f06
commit e0b064115d

View File

@ -15,6 +15,7 @@ const APPLY = (() => {
var disabledElements = new Map(); var disabledElements = new Map();
var docRewriteObserver; var docRewriteObserver;
var docRootObserver; var docRootObserver;
const setStyleContent = createSetStyleContent();
const initializing = init(); const initializing = init();
msg.onTab(applyOnMessage); msg.onTab(applyOnMessage);
@ -34,8 +35,6 @@ const APPLY = (() => {
prefs.subscribe(['exposeIframes'], updateExposeIframes); prefs.subscribe(['exposeIframes'], updateExposeIframes);
} }
const setStyleContent = createSetStyleContent();
function init() { function init() {
// FIXME: styleViaAPI // FIXME: styleViaAPI
// FIXME: getStylesFallback? // FIXME: getStylesFallback?
@ -67,7 +66,6 @@ const APPLY = (() => {
// See https://github.com/openstyles/stylus/issues/461 // See https://github.com/openstyles/stylus/issues/461
// Since it's easy to spoof the browser version in pre-Quantum FF we're checking // Since it's easy to spoof the browser version in pre-Quantum FF we're checking
// for getPreventDefault which got removed in FF59 https://bugzil.la/691151 // for getPreventDefault which got removed in FF59 https://bugzil.la/691151
// const FF_BUG461 = !CHROME && !isOwnPage && !Event.prototype.getPreventDefault;
const EVENT_NAME = chrome.runtime.id; const EVENT_NAME = chrome.runtime.id;
if (CHROME || isOwnPage || Event.prototype.getPreventDefault || !injectPageScript()) { if (CHROME || isOwnPage || Event.prototype.getPreventDefault || !injectPageScript()) {
return (el, content) => { return (el, content) => {
@ -86,9 +84,7 @@ const APPLY = (() => {
function injectPageScript() { function injectPageScript() {
// FIXME: does it work with XML? // FIXME: does it work with XML?
const script = document.createElement('script');
const scriptContent = EVENT_NAME => { const scriptContent = EVENT_NAME => {
document.currentScript.remove();
window.dispatchEvent(new CustomEvent(EVENT_NAME, { window.dispatchEvent(new CustomEvent(EVENT_NAME, {
detail: {method: 'pageScriptOK'} detail: {method: 'pageScriptOK'}
})); }));
@ -107,7 +103,6 @@ const APPLY = (() => {
} }
}, true); }, true);
}; };
script.text = `(${scriptContent})(${JSON.stringify(EVENT_NAME)})`;
let ok = false; let ok = false;
const check = e => { const check = e => {
if (e.detail.method === 'pageScriptOK') { if (e.detail.method === 'pageScriptOK') {
@ -115,9 +110,9 @@ const APPLY = (() => {
} }
}; };
window.addEventListener(EVENT_NAME, check, true); window.addEventListener(EVENT_NAME, check, true);
ROOT.appendChild(script); // eslint-disable-next-line no-eval
window.eval(`(${scriptContent})(${JSON.stringify(EVENT_NAME)})`);
window.removeEventListener(EVENT_NAME, check, true); window.removeEventListener(EVENT_NAME, check, true);
script.remove();
return ok; return ok;
} }
} }