Fix for various content script timing related issues.

This commit is contained in:
hackademix 2018-09-09 18:59:52 +02:00
parent 193e706a99
commit 305c6779a7
4 changed files with 15 additions and 29 deletions

View File

@ -6,18 +6,12 @@
let Scripts = { let Scripts = {
references: new Set(), references: new Set(),
opts: { opts: {
js: [{}], js: [{file: "/content/dynamicNS.js"}, {}],
allFrames: true, allFrames: true,
matchAboutBlank: true, matchAboutBlank: true,
runAt: "document_start" runAt: "document_start"
}, },
async init() { async init() {
let opts = Object.assign({}, this.opts);
opts.js = [{file: "/content/dynamicNS.js"}];
opts.matches = allUrls;
delete opts.excludedMatches;
this._stubScript = await browser.contentScripts.register(opts);
this.init = this.forget; this.init = this.forget;
}, },
forget() { forget() {
@ -38,7 +32,7 @@
if (!matches.length) return; if (!matches.length) return;
try { try {
let opts = Object.assign({}, this.opts); let opts = Object.assign({}, this.opts);
opts.js[0].code = this.trace(code); opts.js[1].code = this.trace(code);
opts.matches = matches; opts.matches = matches;
if (excludeMatches && excludeMatches.length) { if (excludeMatches && excludeMatches.length) {
opts.excludeMatches = excludeMatches; opts.excludeMatches = excludeMatches;
@ -49,14 +43,11 @@
} }
}, },
buildPerms(perms, finalizeSetup = false) { buildPerms(perms) {
if (typeof perms !== "string") { if (typeof perms !== "string") {
perms = JSON.stringify(perms); perms = JSON.stringify(perms);
} }
return finalizeSetup return `ns.setup(${perms}, ${marker});`
? `ns.setup(${perms}, ${marker});`
: `ns.config.CURRENT = ${perms};`
;
} }
}; };
@ -169,10 +160,15 @@
} }
// register new content scripts // register new content scripts
let registering = [];
let allMatching = [];
for (let [perms, keys] of [...permsMap]) { for (let [perms, keys] of [...permsMap]) {
await Scripts.register(Scripts.buildPerms(perms), siteKeys2MatchPatterns(keys), excludeMap.get(perms)); let match = siteKeys2MatchPatterns(keys);
allMatching.push(...match);
registering.push(Scripts.register(Scripts.buildPerms(perms), match, excludeMap.get(perms)));
} }
await Scripts.register(Scripts.buildPerms(serialized.DEFAULT, true), allUrls); registering.push(Scripts.register(Scripts.buildPerms(serialized.DEFAULT), allUrls, allMatching));
await Promise.all(registering);
if (tracing) { if (tracing) {
debug("All the child policies registered in %sms", Date.now() - t0); debug("All the child policies registered in %sms", Date.now() - t0);
} }
@ -186,14 +182,5 @@
}; };
}, },
async updateFrame(tabId, frameId, perms, defaultPreset) {
let code = Scripts.buildPerms(perms) + Scripts.buildPerms(defaultPreset, true);
await browser.tabs.executeScript(tabId, {
code,
frameId,
matchAboutBlank: true,
runAt: "document_start"
});
}
}; };
} }

View File

@ -32,7 +32,7 @@
} else { } else {
await include("/legacy/Legacy.js"); await include("/legacy/Legacy.js");
ns.policy = await Legacy.createOrMigratePolicy(); ns.policy = await Legacy.createOrMigratePolicy();
ns.savePolicy(); await ns.savePolicy();
} }
await RequestGuard.start(); await RequestGuard.start();

View File

@ -54,8 +54,7 @@ var notifyPage = async () => {
debug(`No answer to fetchChildPolicy message. This should not be happening.`); debug(`No answer to fetchChildPolicy message. This should not be happening.`);
return; return;
} }
ns.config.CURRENT = childPolicy.CURRENT; ns.setup(childPolicy.CURRENT, childPolicy.MARKER);
ns.setup(childPolicy.DEFAULT, childPolicy.MARKER);
return; return;
} }

View File

@ -7,8 +7,8 @@ if (!this.ns) {
let deferredSetup = null; let deferredSetup = null;
let nsStub = this.ns = { let nsStub = this.ns = {
config: {}, config: {},
setup(DEFAULT, MARKER) { setup(CURRENT, MARKER) {
deferredSetup = [DEFAULT, MARKER]; deferredSetup = [CURRENT, MARKER];
}, },
merge: ns => { merge: ns => {
ns.config = Object.assign(ns.config, nsStub.config); ns.config = Object.assign(ns.config, nsStub.config);