From b026501ab33a3765638bd36e9500dcd973449352 Mon Sep 17 00:00:00 2001 From: drunkwinter <38593134+drunkwinter@users.noreply.github.com> Date: Thu, 12 Sep 2024 17:51:32 +0200 Subject: [PATCH] [extension] fix 'this document requires TrustedHTML assignment' Related issues: - #253 --- src/extension/main.js | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/src/extension/main.js b/src/extension/main.js index 34456f8..078d3c2 100644 --- a/src/extension/main.js +++ b/src/extension/main.js @@ -34,11 +34,20 @@ function injectScript() { // DANGER: DO NOT USE GLOBALS HERE WITHOUT `window` OBJECT!! FIREFOX BUG WITH GLOBALS. const injectorCode = ` - const nScript = document.createElement('script'); - nScript.innerHTML = ${JSON.stringify(mainCode)}; - document.documentElement.append(nScript); - nScript.remove(); - `; +// WORKAROUND: TypeError: Failed to set the 'innerHTML' property on 'Element': This document requires 'TrustedHTML' assignment. +if (window.trustedTypes && !trustedTypes.defaultPolicy) { + const passThroughFn = (x) => x; + trustedTypes.createPolicy('default', { + createHTML: passThroughFn, + createScriptURL: passThroughFn, + createScript: passThroughFn, + }); +} +const nScript = document.createElement('script'); +nScript.innerHTML = ${JSON.stringify(mainCode)}; +document.documentElement.append(nScript); +nScript.remove(); +`; const nInjector = document.createElement('injector'); nInjector.setAttribute('onclick', injectorCode);