40 lines
2.1 KiB
JavaScript
40 lines
2.1 KiB
JavaScript
|
|
// Glue between autoconsent (inlined above by build-inject.js) and the add-on.
|
||
|
|
// Runs in the isolated world of every http(s) frame. Nothing is exposed to
|
||
|
|
// the page: the library works through the DOM only, and the few rules that
|
||
|
|
// need the page's own JavaScript go through theseus.evalInPage.
|
||
|
|
//
|
||
|
|
// Message flow (see autoconsent docs/api.md): the instance asks for "init",
|
||
|
|
// the add-on answers with the user's settings and the rule bundle (or
|
||
|
|
// nothing, when the add-on is off or the site is excluded); "eval" requests
|
||
|
|
// are run in the main world and answered; the outcome messages are reported
|
||
|
|
// so the panel can count handled pop-ups per site.
|
||
|
|
let instance = null;
|
||
|
|
const isTop = (() => { try { return window.top === window; } catch { return false; } })();
|
||
|
|
const report = (msg) => theseus.invoke("consent.event", {
|
||
|
|
type: msg.type, cmp: msg.cmp || "", url: msg.url || location.href, result: msg.result === undefined ? null : !!msg.result,
|
||
|
|
isCosmetic: !!msg.isCosmetic, details: msg.type === "autoconsentError" ? String(msg.details && (msg.details.msg || msg.details.message || msg.details)).slice(0, 200) : "", top: isTop,
|
||
|
|
}).catch(() => {});
|
||
|
|
const send = async (msg) => {
|
||
|
|
try {
|
||
|
|
switch (msg.type) {
|
||
|
|
case "init": {
|
||
|
|
const r = await theseus.invoke("consent.init", { url: msg.url, top: isTop });
|
||
|
|
if (!r || !r.enabled || !instance) return;
|
||
|
|
await instance.receiveMessageCallback({ type: "initResp", rules: r.rules, config: r.config });
|
||
|
|
break;
|
||
|
|
}
|
||
|
|
case "eval": {
|
||
|
|
let result = false;
|
||
|
|
try { result = await theseus.evalInPage(msg.code); } catch { result = false; }
|
||
|
|
if (instance) await instance.receiveMessageCallback({ type: "evalResp", id: msg.id, result });
|
||
|
|
break;
|
||
|
|
}
|
||
|
|
case "cmpDetected": case "popupFound": case "optOutResult": case "optInResult":
|
||
|
|
case "autoconsentDone": case "autoconsentError": case "selfTestResult":
|
||
|
|
report(msg); break;
|
||
|
|
default: break;
|
||
|
|
}
|
||
|
|
} catch (e) { /* the page must never see a failure here */ }
|
||
|
|
};
|
||
|
|
try { instance = new AutoConsentCtor(send, null, null); } catch (e) { instance = null; }
|