Improve `trusted-replace-node-text` scriptlet

Related discussion:
https://github.com/brave/adblock-resources/pull/194
This commit is contained in:
Raymond Hill 2024-08-03 20:09:21 -04:00
parent d42329a3a3
commit 4f0d1301ab
No known key found for this signature in database
GPG Key ID: 25E1490B761470C2
1 changed files with 15 additions and 1 deletions

View File

@ -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;
@ -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()}`);
}