Skip to content

Commit

Permalink
Fix: use a simple eval to execute page scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
eight04 committed Oct 12, 2018
1 parent 405b7f8 commit e0b0641
Showing 1 changed file with 3 additions and 8 deletions.
11 changes: 3 additions & 8 deletions content/apply.js
Expand Up @@ -15,6 +15,7 @@ const APPLY = (() => {
var disabledElements = new Map();
var docRewriteObserver;
var docRootObserver;
const setStyleContent = createSetStyleContent();
const initializing = init();

msg.onTab(applyOnMessage);
Expand All @@ -34,8 +35,6 @@ const APPLY = (() => {
prefs.subscribe(['exposeIframes'], updateExposeIframes);
}

const setStyleContent = createSetStyleContent();

function init() {
// FIXME: styleViaAPI
// FIXME: getStylesFallback?
Expand Down Expand Up @@ -67,7 +66,6 @@ const APPLY = (() => {
// See https://github.com/openstyles/stylus/issues/461
// 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
// const FF_BUG461 = !CHROME && !isOwnPage && !Event.prototype.getPreventDefault;
const EVENT_NAME = chrome.runtime.id;
if (CHROME || isOwnPage || Event.prototype.getPreventDefault || !injectPageScript()) {
return (el, content) => {
Expand All @@ -86,9 +84,7 @@ const APPLY = (() => {

function injectPageScript() {
// FIXME: does it work with XML?
const script = document.createElement('script');
const scriptContent = EVENT_NAME => {
document.currentScript.remove();
window.dispatchEvent(new CustomEvent(EVENT_NAME, {
detail: {method: 'pageScriptOK'}
}));
Expand All @@ -107,17 +103,16 @@ const APPLY = (() => {
}
}, true);
};
script.text = `(${scriptContent})(${JSON.stringify(EVENT_NAME)})`;
let ok = false;
const check = e => {
if (e.detail.method === 'pageScriptOK') {
ok = 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);
script.remove();
return ok;
}
}
Expand Down

0 comments on commit e0b0641

Please sign in to comment.