diff --git a/src/helpers/log-message.js b/src/helpers/log-message.js index 59504576..86e02e3b 100644 --- a/src/helpers/log-message.js +++ b/src/helpers/log-message.js @@ -12,8 +12,26 @@ * @param {boolean} [forced=false] to log message unconditionally */ export const logMessage = (source, message, forced = false) => { - if (forced || source.verbose) { - // eslint-disable-next-line no-console - console.log(`${source.name}: ${message}`); + const { + name, + ruleText, + verbose, + } = source; + + if (!forced && !verbose) { + return; } + + let messageStr = `${name}: ${message};`; + + // Extract scriptlet part from rule text + if (ruleText) { + const RULE_MARKER = '#%#'; + const markerIdx = ruleText.indexOf(RULE_MARKER); + const extension = ruleText.slice(markerIdx, ruleText.length); + messageStr += ` cannot apply rule: ${extension}`; + } + + // eslint-disable-next-line no-console + console.log(messageStr); }; diff --git a/src/scriptlets/adjust-setInterval.js b/src/scriptlets/adjust-setInterval.js index 3743a659..2570081a 100644 --- a/src/scriptlets/adjust-setInterval.js +++ b/src/scriptlets/adjust-setInterval.js @@ -71,7 +71,7 @@ export function adjustSetInterval(source, matchCallback, matchDelay, boost) { // https://github.com/AdguardTeam/Scriptlets/issues/221 if (!isValidCallback(callback)) { // eslint-disable-next-line max-len - const message = `Scriptlet can't be applied because of invalid callback: '${String(callback)}'.`; + const message = `Scriptlet can't be applied because of invalid callback: '${String(callback)}'`; logMessage(source, message); } else if (matchRegexp.test(callback.toString()) && isDelayMatched(matchDelay, delay)) { delay *= getBoostMultiplier(boost); diff --git a/src/scriptlets/adjust-setTimeout.js b/src/scriptlets/adjust-setTimeout.js index 94322384..eefdd61e 100644 --- a/src/scriptlets/adjust-setTimeout.js +++ b/src/scriptlets/adjust-setTimeout.js @@ -71,7 +71,7 @@ export function adjustSetTimeout(source, matchCallback, matchDelay, boost) { // https://github.com/AdguardTeam/Scriptlets/issues/221 if (!isValidCallback(callback)) { // eslint-disable-next-line max-len - const message = `Scriptlet can't be applied because of invalid callback: '${String(callback)}'.`; + const message = `Scriptlet can't be applied because of invalid callback: '${String(callback)}'`; logMessage(source, message); } else if (matchRegexp.test(callback.toString()) && isDelayMatched(matchDelay, delay)) { delay *= getBoostMultiplier(boost); diff --git a/src/scriptlets/trusted-replace-fetch-response.js b/src/scriptlets/trusted-replace-fetch-response.js index 27b8039d..1bb21916 100644 --- a/src/scriptlets/trusted-replace-fetch-response.js +++ b/src/scriptlets/trusted-replace-fetch-response.js @@ -88,7 +88,7 @@ export function trustedReplaceFetchResponse(source, pattern = '', replacement = // Only allow pattern as empty string for logging purposes if (pattern === '' && replacement !== '') { - logMessage(source, 'Pattern argument should not be empty string.'); + logMessage(source, 'Pattern argument should not be empty string'); return; } const shouldLog = pattern === '' && replacement === ''; diff --git a/src/scriptlets/trusted-set-cookie-reload.js b/src/scriptlets/trusted-set-cookie-reload.js index 0155088f..abe83916 100644 --- a/src/scriptlets/trusted-set-cookie-reload.js +++ b/src/scriptlets/trusted-set-cookie-reload.js @@ -72,11 +72,11 @@ import { export function trustedSetCookieReload(source, name, value, offsetExpiresSec = '', path = '/') { if (typeof name === 'undefined') { - logMessage(source, 'Cookie name should be specified.'); + logMessage(source, 'Cookie name should be specified'); return; } if (typeof value === 'undefined') { - logMessage(source, 'Cookie value should be specified.'); + logMessage(source, 'Cookie value should be specified'); return; } diff --git a/src/scriptlets/trusted-set-cookie.js b/src/scriptlets/trusted-set-cookie.js index cc62efe3..6e7ba574 100644 --- a/src/scriptlets/trusted-set-cookie.js +++ b/src/scriptlets/trusted-set-cookie.js @@ -71,11 +71,11 @@ import { export function trustedSetCookie(source, name, value, offsetExpiresSec = '', path = '/') { if (typeof name === 'undefined') { - logMessage(source, 'Cookie name should be specified.'); + logMessage(source, 'Cookie name should be specified'); return; } if (typeof value === 'undefined') { - logMessage(source, 'Cookie value should be specified.'); + logMessage(source, 'Cookie value should be specified'); return; } diff --git a/src/scriptlets/trusted-set-local-storage-item.js b/src/scriptlets/trusted-set-local-storage-item.js index 3ffefecd..1b4f23e4 100644 --- a/src/scriptlets/trusted-set-local-storage-item.js +++ b/src/scriptlets/trusted-set-local-storage-item.js @@ -56,12 +56,12 @@ import { export function trustedSetLocalStorageItem(source, key, value) { if (typeof key === 'undefined') { - logMessage(source, 'Item key should be specified.'); + logMessage(source, 'Item key should be specified'); return; } if (typeof value === 'undefined') { - logMessage(source, 'Item value should be specified.'); + logMessage(source, 'Item value should be specified'); return; } diff --git a/tests/scriptlets/adjust-setInterval.test.js b/tests/scriptlets/adjust-setInterval.test.js index 90379025..0cd9a6ab 100644 --- a/tests/scriptlets/adjust-setInterval.test.js +++ b/tests/scriptlets/adjust-setInterval.test.js @@ -288,7 +288,7 @@ test('no match -- invalid callback - undefined', (assert) => { assert.strictEqual(window.hit, undefined, 'hit should not fire'); assert.strictEqual( loggedMessage, - `${name}: Scriptlet can't be applied because of invalid callback: '${String(callback)}'.`, // eslint-disable-line max-len + `${name}: Scriptlet can't be applied because of invalid callback: '${String(callback)}';`, // eslint-disable-line max-len 'console.logged warning ok', ); clearInterval(testInterval); diff --git a/tests/scriptlets/adjust-setTimeout.test.js b/tests/scriptlets/adjust-setTimeout.test.js index 999ce1ed..26a630dc 100644 --- a/tests/scriptlets/adjust-setTimeout.test.js +++ b/tests/scriptlets/adjust-setTimeout.test.js @@ -288,7 +288,7 @@ test('no match -- invalid callback - undefined', (assert) => { assert.strictEqual(window.hit, undefined, 'hit should not fire'); assert.strictEqual( loggedMessage, - `${name}: Scriptlet can't be applied because of invalid callback: '${String(callback)}'.`, // eslint-disable-line max-len + `${name}: Scriptlet can't be applied because of invalid callback: '${String(callback)}';`, // eslint-disable-line max-len 'console.logged warning ok', ); clearTimeout(testTimeout); diff --git a/tests/scriptlets/log-eval.test.js b/tests/scriptlets/log-eval.test.js index b12a6beb..d455d5f2 100644 --- a/tests/scriptlets/log-eval.test.js +++ b/tests/scriptlets/log-eval.test.js @@ -32,7 +32,7 @@ test('logs eval calls', (assert) => { if (input.indexOf('trace') > -1) { return; } - assert.strictEqual(input, `${name}: eval("${evalStr}")`, 'console.hit input should be equal'); + assert.strictEqual(input, `${name}: eval("${evalStr}");`, 'console.hit input should be equal'); }; runScriptlet(name); const evalWrap = eval; @@ -52,7 +52,7 @@ test('logs new Function() calls', (assert) => { if (input.indexOf('trace') > -1) { return; } - assert.strictEqual(input, `${name}: new Function(${args.join(', ')})`, 'console.hit input should be equal'); + assert.strictEqual(input, `${name}: new Function(${args.join(', ')});`, 'console.hit input should be equal'); }; runScriptlet(name); diff --git a/tests/scriptlets/nowebrtc.test.js b/tests/scriptlets/nowebrtc.test.js index 5e13b273..f0f217c9 100644 --- a/tests/scriptlets/nowebrtc.test.js +++ b/tests/scriptlets/nowebrtc.test.js @@ -87,7 +87,7 @@ if (!isSupported) { return; } // eslint-disable-next-line max-len - const EXPECTED_LOG_STR = `${name}: Document tried to create an RTCPeerConnection: ${TEST_URL_VALUE}`; + const EXPECTED_LOG_STR = `${name}: Document tried to create an RTCPeerConnection: ${TEST_URL_VALUE};`; assert.ok(endsWith(input, EXPECTED_LOG_STR), 'console.hit input'); }; diff --git a/tests/scriptlets/prevent-requestAnimationFrame.test.js b/tests/scriptlets/prevent-requestAnimationFrame.test.js index 1cf65991..280a9736 100644 --- a/tests/scriptlets/prevent-requestAnimationFrame.test.js +++ b/tests/scriptlets/prevent-requestAnimationFrame.test.js @@ -62,7 +62,11 @@ test('prevent-requestAnimationFrame: no args -- logging', (assert) => { // do test checking after scriptlet's execution end setTimeout(() => { assert.strictEqual(window.hit, 'FIRED', 'hit fired'); - assert.strictEqual(loggedMessage, `prevent-requestAnimationFrame: requestAnimationFrame(${testFunction.toString()})`, 'console.hit input'); + assert.strictEqual( + loggedMessage, + `prevent-requestAnimationFrame: requestAnimationFrame(${testFunction.toString()});`, + 'console.hit input', + ); assert.strictEqual(window[logProperty], 'changed', 'property changed'); clearGlobalProps(logProperty); done(); diff --git a/tests/scriptlets/prevent-setInterval.test.js b/tests/scriptlets/prevent-setInterval.test.js index 47b75559..f4bf43b0 100644 --- a/tests/scriptlets/prevent-setInterval.test.js +++ b/tests/scriptlets/prevent-setInterval.test.js @@ -69,7 +69,7 @@ test('no args -- logging', (assert) => { assert.strictEqual(window.hit, 'FIRED', 'hit fired'); assert.strictEqual( loggedMessage, - `prevent-setInterval: setInterval(${callback.toString()}, ${timeout})`, + `prevent-setInterval: setInterval(${callback.toString()}, ${timeout});`, 'console.hit input ok', ); assert.strictEqual(window[agLogSetInterval], 'changed', 'property changed'); diff --git a/tests/scriptlets/prevent-setTimeout.test.js b/tests/scriptlets/prevent-setTimeout.test.js index 0010cfa8..499b737c 100644 --- a/tests/scriptlets/prevent-setTimeout.test.js +++ b/tests/scriptlets/prevent-setTimeout.test.js @@ -69,7 +69,7 @@ test('no args -- logging', (assert) => { assert.strictEqual(window.hit, 'FIRED', 'hit fired'); assert.strictEqual( loggedMessage, - `prevent-setTimeout: setTimeout(${callback.toString()}, ${timeout})`, + `prevent-setTimeout: setTimeout(${callback.toString()}, ${timeout});`, 'console.hit input ok', ); assert.strictEqual(window[agLogSetTimeout], 'changed', 'property changed'); diff --git a/tests/scriptlets/prevent-window-open.test.js b/tests/scriptlets/prevent-window-open.test.js index 89e804d0..6dedd651 100644 --- a/tests/scriptlets/prevent-window-open.test.js +++ b/tests/scriptlets/prevent-window-open.test.js @@ -184,7 +184,7 @@ test('new syntax: log checking - only url', (assert) => { if (input.indexOf('trace') > -1) { return; } - const EXPECTED_LOG_STR = `${name}: ${testUrl}`; + const EXPECTED_LOG_STR = `${name}: ${testUrl};`; assert.strictEqual(input, EXPECTED_LOG_STR, 'console.hit input'); }; @@ -208,7 +208,7 @@ test('new syntax: log checking - url + args', (assert) => { return; } // eslint-disable-next-line max-len - const EXPECTED_LOG_STR = `${name}: ${testUrl}, ${testWindowName}, ${testWindowFeatures}`; + const EXPECTED_LOG_STR = `${name}: ${testUrl}, ${testWindowName}, ${testWindowFeatures};`; assert.strictEqual(input, EXPECTED_LOG_STR, 'console.hit input'); }; diff --git a/tests/scriptlets/remove-attr.test.js b/tests/scriptlets/remove-attr.test.js index 22e9c7b0..ac56d423 100644 --- a/tests/scriptlets/remove-attr.test.js +++ b/tests/scriptlets/remove-attr.test.js @@ -306,7 +306,11 @@ test('invalid selector — no match', (assert) => { if (input.indexOf('trace') > -1) { return; } - assert.strictEqual(input, `${name}: Invalid selector arg: '${selector}'`, 'logged error for invalid remove-attr selector'); + assert.strictEqual( + input, + `${name}: Invalid selector arg: '${selector}';`, + 'logged error for invalid remove-attr selector;', + ); }; runScriptlet(name, scriptletArgs); diff --git a/tests/scriptlets/remove-class.test.js b/tests/scriptlets/remove-class.test.js index e3b11ba9..107e65c7 100644 --- a/tests/scriptlets/remove-class.test.js +++ b/tests/scriptlets/remove-class.test.js @@ -276,7 +276,7 @@ test('invalid selector — no match', (assert) => { if (input.indexOf('trace') > -1) { return; } - assert.strictEqual(input, `${name}: Invalid selector arg: '${selectors}'`, 'logged error for invalid remove-class selector'); + assert.strictEqual(input, `${name}: Invalid selector arg: '${selectors}';`, 'logged error for invalid remove-class selector'); }; assert.strictEqual(window.hit, undefined, 'hit SHOULD NOT fire'); diff --git a/tests/scriptlets/trusted-set-cookie.test.js b/tests/scriptlets/trusted-set-cookie.test.js index db70bc8e..1385c0bd 100644 --- a/tests/scriptlets/trusted-set-cookie.test.js +++ b/tests/scriptlets/trusted-set-cookie.test.js @@ -112,9 +112,11 @@ test('Set cookie with invalid expires', (assert) => { if (input.indexOf('trace') > -1) { return; } - assert.strictEqual(input, - `${name}: Invalid offsetExpiresSec value: ${expiresSec}`, - 'logs correctly on invalid offsetExpiresSec'); + assert.strictEqual( + input, + `${name}: Invalid offsetExpiresSec value: ${expiresSec};`, + 'logs correctly on invalid offsetExpiresSec', + ); }; runScriptlet(name, [cName, cValue, `${expiresSec}`]);