Skip to content

Commit

Permalink
[extension] fix 'this document requires TrustedHTML assignment'
Browse files Browse the repository at this point in the history
Related issues:
- #253
  • Loading branch information
drunkwinter committed Sep 12, 2024
1 parent 5c6aced commit b026501
Showing 1 changed file with 14 additions and 5 deletions.
19 changes: 14 additions & 5 deletions src/extension/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit b026501

Please sign in to comment.