Skip to content

Commit

Permalink
Improve trusted-replace-node-text scriptlet
Browse files Browse the repository at this point in the history
Related discussion:
brave/adblock-resources#194
  • Loading branch information
gorhill committed Aug 4, 2024
1 parent d42329a commit 4f0d130
Showing 1 changed file with 15 additions and 1 deletion.
16 changes: 15 additions & 1 deletion assets/resources/scriptlets.js
Original file line number Diff line number Diff line change
Expand Up @@ -730,6 +730,18 @@ function replaceNodeTextFn(
safe.uboLog(logPrefix, 'Quitting');
}
};
const textContentFactory = (( ) => {
const out = { createScript: s => s };
const { trustedTypes: tt } = self;
if ( tt instanceof Object ) {
if ( typeof tt.getPropertyType === 'function' ) {
if ( tt.getPropertyType('script', 'textContent') === 'TrustedScript' ) {
return tt.createPolicy('uBO', out);
}
}
}
return out;
})();
let sedCount = extraArgs.sedCount || 0;
const handleNode = node => {
const before = node.textContent;
Expand All @@ -747,7 +759,9 @@ function replaceNodeTextFn(
const after = pattern !== ''
? before.replace(rePattern, replacement)
: replacement;
node.textContent = after;
node.textContent = node.nodeName === 'SCRIPT'
? textContentFactory.createScript(after)
: after;
if ( safe.logLevel > 1 ) {
safe.uboLog(logPrefix, `Text before:\n${before.trim()}`);
}
Expand Down

2 comments on commit 4f0d130

@ryanbr
Copy link
Contributor

@ryanbr ryanbr commented on 4f0d130 Aug 4, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This broke YT when we used this rule without issue: https://pastebin.com/raw/A0ECMevT @gorhill
ubo-bta

@gorhill
Copy link
Owner Author

@gorhill gorhill commented on 4f0d130 Aug 4, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I cannot reproduce breakage, including when forcing trusted types using ||youtube.com^$csp=require-trusted-types-for 'script'.

What does the page dev console say?

Please sign in to comment.