From da672d4dbf202042160e3eacd6bad3c197d20d93 Mon Sep 17 00:00:00 2001 From: Raymond Hill Date: Sun, 26 Mar 2023 13:14:00 -0400 Subject: [PATCH] Add extra check to detect function declaration in scriptlets Related feedback: - https://www.reddit.com/r/uBlockOrigin/comments/122pqyg/ --- src/js/scriptlet-filtering.js | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/src/js/scriptlet-filtering.js b/src/js/scriptlet-filtering.js index 3cf05a5b3..8cddeede1 100644 --- a/src/js/scriptlet-filtering.js +++ b/src/js/scriptlet-filtering.js @@ -171,15 +171,19 @@ const lookupScriptlet = function(rawToken, scriptletMap, dependencyMap) { if ( Array.isArray(details.dependencies) === false ) { continue; } dependencies.push(...details.dependencies); } - scriptletMap.set( - rawToken, - [ 'try {', content, '} catch (e) {', '}' ].join('\n') - ); + scriptletMap.set(rawToken, [ + 'try {', + '// >>>> scriptlet start', + content, + '// <<<< scriptlet end', + '} catch (e) {', + '}', + ].join('\n')); }; // Fill-in scriptlet argument placeholders. const patchScriptlet = function(content, args) { - if ( content.startsWith('function') ) { + if ( content.startsWith('function') && content.endsWith('}') ) { content = `(${content})({{args}});`; } if ( args.startsWith('{') && args.endsWith('}') ) { @@ -354,7 +358,7 @@ scriptletFilteringEngine.retrieve = function(request, options = {}) { fullCode.push(code); } cacheDetails = { - code: fullCode.join('\n'), + code: fullCode.join('\n\n'), tokens: Array.from($scriptlets), exceptions: Array.from($exceptions), };