Use semi-synchronous mode to fetch policies.
This commit is contained in:
parent
17ebef5a48
commit
adfb29f581
|
@ -1,5 +1,5 @@
|
||||||
'use strict';
|
|
||||||
{
|
{
|
||||||
|
'use strict';
|
||||||
let listenersMap = new Map();
|
let listenersMap = new Map();
|
||||||
let backlog = new Set();
|
let backlog = new Set();
|
||||||
|
|
||||||
|
@ -37,22 +37,37 @@
|
||||||
debug(`Fetching policy from document %s, readyState %s, content %s`,
|
debug(`Fetching policy from document %s, readyState %s, content %s`,
|
||||||
document.URL, document.readyState, document.documentElement.outerHTML);
|
document.URL, document.readyState, document.documentElement.outerHTML);
|
||||||
let originalState = document.readyState;
|
let originalState = document.readyState;
|
||||||
let scriptsBlocked = false;
|
let blockedScripts = [];
|
||||||
let url = document.URL;
|
let url = document.URL;
|
||||||
addEventListener("beforescriptexecute", e => {
|
addEventListener("beforescriptexecute", e => {
|
||||||
// safety net for syncrhonous load on Firefox
|
// safety net for syncrhonous load on Firefox
|
||||||
if (!this.canScript) {
|
if (!this.canScript) {
|
||||||
e.preventDefault();
|
e.preventDefault();
|
||||||
scriptsBlocked = true;
|
let script = e.target;
|
||||||
log("Some script managed to be inserted in the DOM while fetching policy, blocking it.\n", e.target.textContent);
|
blockedScripts.push(script)
|
||||||
|
log("Some script managed to be inserted in the DOM while fetching policy, blocking it.\n", script);
|
||||||
}
|
}
|
||||||
}, true);
|
}, true);
|
||||||
|
|
||||||
let policy = null;
|
let policy = null;
|
||||||
|
|
||||||
|
let setup = policy => {
|
||||||
|
debug("Fetched %o, readyState %s", policy, document.readyState); // DEV_ONLY
|
||||||
|
this.setup(policy);
|
||||||
|
if (this.canScript && blockedScripts.length && originalState === "loading") {
|
||||||
|
log("Blocked some scripts on %s even though they are actually permitted by policy.", url)
|
||||||
|
// something went wrong, e.g. with session restore.
|
||||||
|
for (let s of blockedScripts) {
|
||||||
|
// reinsert the script
|
||||||
|
s.replace(s.cloneNode(true));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
for (;;) {
|
for (;;) {
|
||||||
try {
|
try {
|
||||||
policy = browser.runtime.sendSyncMessage(
|
policy = browser.runtime.sendSyncMessage(
|
||||||
{id: "fetchPolicy", url, contextUrl: url});
|
{id: "fetchPolicy", url, contextUrl: url}, setup);
|
||||||
break;
|
break;
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
if (!Messages.isMissingEndpoint(e)) {
|
if (!Messages.isMissingEndpoint(e)) {
|
||||||
|
@ -62,20 +77,7 @@
|
||||||
error("Background page not ready yet, retrying to fetch policy...")
|
error("Background page not ready yet, retrying to fetch policy...")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
debug("Fetched %o, readyState %s", policy, document.readyState); // DEV_ONLY
|
|
||||||
this.setup(policy);
|
|
||||||
if (this.canScript && scriptsBlocked &&
|
|
||||||
originalState === "loading" && document.readyState !== originalState) {
|
|
||||||
log("Blocked some scripts on %s even though they are actually permitted by policy.", url)
|
|
||||||
// something went wrong, e.g. with session restore.
|
|
||||||
if (!url.startsWith("http")) {
|
|
||||||
log("Not a HTTP page, reloading %s.", url);
|
|
||||||
window.stop();
|
|
||||||
location.reload(false);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return true;
|
|
||||||
},
|
},
|
||||||
|
|
||||||
setup(policy) {
|
setup(policy) {
|
||||||
|
|
Loading…
Reference in New Issue