SendSyncMessage script execution safety net more compatible with other extensions (e.g. BlockTube).
This commit is contained in:
parent
c61d11944b
commit
aaf5a3d0a6
|
@ -238,29 +238,44 @@
|
||||||
}
|
}
|
||||||
suspended = false;
|
suspended = false;
|
||||||
};
|
};
|
||||||
|
let deferred = [];
|
||||||
|
let runDeferred = () => {
|
||||||
|
let current = deferred;
|
||||||
|
deferred = []; // prevent reentrant calls
|
||||||
|
for (let ds of current) {
|
||||||
|
console.debug("sendSyncMessage running deferred", ds)
|
||||||
|
let replacement = document.createElement("script");
|
||||||
|
for (let a of ds.attributes) {
|
||||||
|
replacement.setAttribute(a.name, a.value);
|
||||||
|
}
|
||||||
|
replacement.textContent = ds.textContent;
|
||||||
|
ds.replaceWith(replacement);
|
||||||
|
}
|
||||||
|
};
|
||||||
let onBeforeScript = e => {
|
let onBeforeScript = e => {
|
||||||
if(typeof canScript() === "undefined") {
|
if(typeof canScript() === "undefined") {
|
||||||
suspend();
|
suspend();
|
||||||
}
|
}
|
||||||
let allowed = canScript();
|
let allowed = canScript();
|
||||||
|
let script = e.target;
|
||||||
if (typeof allowed === "undefined") {
|
if (typeof allowed === "undefined") {
|
||||||
let script = e.target.cloneNode(true);
|
|
||||||
e.target.replaceWith(script);
|
|
||||||
console.debug("sendSyncMessage deferring", script);
|
console.debug("sendSyncMessage deferring", script);
|
||||||
|
deferred.push(script);
|
||||||
|
}
|
||||||
|
if (!allowed) {
|
||||||
|
console.debug("sendSyncMessage blocked", script);
|
||||||
e.preventDefault();
|
e.preventDefault();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (!allowed) {
|
runDeferred();
|
||||||
console.debug("sendSyncMessage blocked a script element", e.target);
|
console.debug("sendSyncMessage allowed", script);
|
||||||
e.preventDefault();
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
addEventListener("beforescriptexecute", onBeforeScript, true);
|
addEventListener("beforescriptexecute", onBeforeScript, true);
|
||||||
|
|
||||||
let finalize = () => {
|
let finalize = () => {
|
||||||
removeEventListener("beforescriptexecute", onBeforeScript, true);
|
removeEventListener("beforescriptexecute", onBeforeScript, true);
|
||||||
|
runDeferred();
|
||||||
};
|
};
|
||||||
|
|
||||||
// on Firefox we first need to send an async message telling the
|
// on Firefox we first need to send an async message telling the
|
||||||
|
|
Loading…
Reference in New Issue