Revert defer page script injection (#758)

Closes #756

I edited this a couple days ago, so let's double check the diff and make sure I didn't screw anything up.
This commit is contained in:
narcolepticinsomniac 2019-08-04 13:04:02 -04:00 committed by GitHub
parent 00c2455b07
commit 7d52326eb7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -88,9 +88,10 @@ const APPLY = (() => {
// 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 EVENT_NAME = chrome.runtime.id; const EVENT_NAME = chrome.runtime.id;
let ready; const usePageScript = CHROME || IS_OWN_PAGE || Event.prototype.getPreventDefault ?
Promise.resolve(false) : injectPageScript();
return (el, content, disabled) => return (el, content, disabled) =>
checkPageScript().then(ok => { usePageScript.then(ok => {
if (!ok) { if (!ok) {
el.textContent = content; el.textContent = content;
// https://github.com/openstyles/stylus/issues/693 // https://github.com/openstyles/stylus/issues/693
@ -106,14 +107,6 @@ const APPLY = (() => {
} }
}); });
function checkPageScript() {
if (!ready) {
ready = CHROME || IS_OWN_PAGE || Event.prototype.getPreventDefault ?
Promise.resolve(false) : injectPageScript();
}
return ready;
}
function injectPageScript() { function injectPageScript() {
const scriptContent = EVENT_NAME => { const scriptContent = EVENT_NAME => {
document.currentScript.remove(); document.currentScript.remove();
@ -156,7 +149,7 @@ const APPLY = (() => {
script.onerror = resolveFalse; script.onerror = resolveFalse;
window.addEventListener('error', resolveFalse); window.addEventListener('error', resolveFalse);
window.addEventListener(EVENT_NAME, handleInit); window.addEventListener(EVENT_NAME, handleInit);
(document.head || document.documentElement).appendChild(script); document.documentElement.appendChild(script);
return promise.then(result => { return promise.then(result => {
script.remove(); script.remove();
window.removeEventListener(EVENT_NAME, handleInit); window.removeEventListener(EVENT_NAME, handleInit);