Skip to content

Commit

Permalink
Change logContent to verbose
Browse files Browse the repository at this point in the history
Update docs
  • Loading branch information
AdamWr committed Jun 24, 2024
1 parent c2503ef commit ce58a21
Show file tree
Hide file tree
Showing 10 changed files with 45 additions and 30 deletions.
10 changes: 6 additions & 4 deletions src/scriptlets/m3u-prune.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@ import {
* - `propsToRemove` — optional, string or regular expression
* to match the URL line (segment) which will be removed alongside with its tags
* - `urlToMatch` — optional, string or regular expression for matching the request's URL
* - `logContent` — optional, string, if set to 'logContent' will log original and modified M3U content
* - `verbose` — optional, boolean, if set to 'true' will log original and modified M3U content
*
* > `verbose` may be useful for debugging but it is not allowed for prod versions of filter lists.
*
* > Usage with no arguments will log response payload and URL to browser console;
* > it may be useful for debugging but it is not allowed for prod versions of filter lists.
Expand All @@ -57,7 +59,7 @@ import {
* 1. Removes a line which contains `example.com/video/`, only if request's URL contains `.m3u8` and log content
*
* ```adblock
* example.org#%#//scriptlet('m3u-prune', 'example.com/video/', '.m3u8', 'logContent')
* example.org#%#//scriptlet('m3u-prune', 'example.com/video/', '.m3u8', 'true')
* ```
*
* 1. Call with no arguments will log response payload and URL at the console
Expand All @@ -76,7 +78,7 @@ import {
*/
/* eslint-enable max-len */

export function m3uPrune(source, propsToRemove, urlToMatch = '', logContent) {
export function m3uPrune(source, propsToRemove, urlToMatch = '', verbose = false) {
// do nothing if browser does not support fetch or Proxy (e.g. Internet Explorer)
// https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/fetch
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Proxy
Expand All @@ -90,7 +92,7 @@ export function m3uPrune(source, propsToRemove, urlToMatch = '', logContent) {

let shouldPruneResponse = false;

const shouldLogContent = logContent === 'logContent';
const shouldLogContent = verbose === 'true';

const urlMatchRegexp = toRegExp(urlToMatch);

Expand Down
16 changes: 12 additions & 4 deletions src/scriptlets/trusted-replace-fetch-response.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,9 @@ import {
* - `value` is string or regular expression for matching the value of the option passed to fetch call;
* invalid regular expression will cause any value matching
* <!-- markdownlint-disable-next-line line-length -->
* - `logContent` — optional, string, if set to 'logContent' will log original and modified text content of fetch responses.
* - `verbose` — optional, boolean, if set to 'true' will log original and modified text content of fetch responses.
*
* > `verbose` may be useful for debugging but it is not allowed for prod versions of filter lists.
*
* > Usage with no arguments will log fetch calls to browser console;
* > it may be useful for debugging but it is not allowed for prod versions of filter lists.
Expand Down Expand Up @@ -98,13 +100,19 @@ import {
* 1. Replace "foo" text content with "bar" of all fetch responses for example.com and log original and modified text content <!-- markdownlint-disable-line line-length -->
*
* ```adblock
* example.org#%#//scriptlet('trusted-replace-fetch-response', 'foo', 'bar', 'example.com', 'logContent')
* example.org#%#//scriptlet('trusted-replace-fetch-response', 'foo', 'bar', 'example.com', 'true')
* ```
*
* @added v1.7.3.
*/
/* eslint-enable max-len */
export function trustedReplaceFetchResponse(source, pattern = '', replacement = '', propsToMatch = '', logContent) {
export function trustedReplaceFetchResponse(
source,
pattern = '',
replacement = '',
propsToMatch = '',
verbose = false,
) {
// do nothing if browser does not support fetch or Proxy (e.g. Internet Explorer)
// https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/fetch
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Proxy
Expand All @@ -120,7 +128,7 @@ export function trustedReplaceFetchResponse(source, pattern = '', replacement =
return;
}
const shouldLog = pattern === '' && replacement === '';
const shouldLogContent = logContent === 'logContent';
const shouldLogContent = verbose === 'true';

const nativeRequestClone = Request.prototype.clone;
const nativeFetch = fetch;
Expand Down
8 changes: 5 additions & 3 deletions src/scriptlets/trusted-replace-node-text.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@ import {
* If matched, the whole text will be removed. Case sensitive.
* - `pattern` — required, string or regexp for matching contents of `node.textContent` that should be replaced.
* - `replacement` — required, string to replace text content matched by `pattern`.
* - `logContent` — optional, string, if set to 'logContent' will log original and modified text content.
* - `...extraArgs` — optional, string, if includes 'verbose' will log original and modified text content.
*
* > `verbose` may be useful for debugging but it is not allowed for prod versions of filter lists.
*
* ### Examples
*
Expand Down Expand Up @@ -76,7 +78,7 @@ import {
* 3. Replace node's text content and log original and modified text content:
*
* ```adblock
* example.org#%#//scriptlet('trusted-replace-node-text', 'div', 'some', 'text', 'other text', 'logContent')
* example.org#%#//scriptlet('trusted-replace-node-text', 'div', 'some', 'text', 'other text', 'verbose')
* ```
*
* @added v1.9.37.
Expand Down Expand Up @@ -108,7 +110,7 @@ export function trustedReplaceNodeText(source, nodeName, textMatch, pattern, rep
}
}

const shouldLog = extraArgs.includes('logContent');
const shouldLog = extraArgs.includes('verbose');

const {
selector,
Expand Down
11 changes: 6 additions & 5 deletions src/scriptlets/trusted-replace-xhr-response.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,9 @@ import {
* - `name` — string or regular expression for matching XMLHttpRequest property name
* - `value` — string or regular expression for matching the value of the option
* passed to `XMLHttpRequest.open()` call
* <!-- markdownlint-disable-next-line line-length -->
* - `logContent` — optional, string, if set to 'logContent' will log original and modified text content of XMLHttpRequests.
* - `verbose` — optional, boolean, if set to 'true' will log original and modified text content of XMLHttpRequests.
*
* > `verbose` may be useful for debugging but it is not allowed for prod versions of filter lists.
*
* > Usage with no arguments will log XMLHttpRequest objects to browser console;
* > it may be useful for debugging but it is not allowed for prod versions of filter lists.
Expand Down Expand Up @@ -88,13 +89,13 @@ import {
* 1. Replace "foo" text content with "bar" of all XMLHttpRequests for example.com and log original and modified text content <!-- markdownlint-disable-line line-length -->
*
* ```adblock
* example.org#%#//scriptlet('trusted-replace-xhr-response', 'foo', 'bar', 'example.com', 'logContent')
* example.org#%#//scriptlet('trusted-replace-xhr-response', 'foo', 'bar', 'example.com', 'true')
* ```
*
* @added v1.7.3.
*/
/* eslint-enable max-len */
export function trustedReplaceXhrResponse(source, pattern = '', replacement = '', propsToMatch = '', logContent) {
export function trustedReplaceXhrResponse(source, pattern = '', replacement = '', propsToMatch = '', verbose = false) {
// do nothing if browser does not support Proxy (e.g. Internet Explorer)
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Proxy
if (typeof Proxy === 'undefined') {
Expand All @@ -109,7 +110,7 @@ export function trustedReplaceXhrResponse(source, pattern = '', replacement = ''
}

const shouldLog = pattern === '' && replacement === '';
const shouldLogContent = logContent === 'logContent';
const shouldLogContent = verbose === 'true';

const nativeOpen = window.XMLHttpRequest.prototype.open;
const nativeSend = window.XMLHttpRequest.prototype.send;
Expand Down
10 changes: 6 additions & 4 deletions src/scriptlets/xml-prune.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@ import {
* - `propsToMatch` — optional, XPath or selector of elements which will be removed from XML
* - `optionalProp` — optional, selector of elements that must occur in XML document
* - `urlToMatch` — optional, string or regular expression for matching the request's URL
* - `logContent` — optional, string, if set to 'logContent' will log original and modified XML content
* - `verbose` — optional, boolean, if set to 'true' will log original and modified XML content
*
* > `verbose` may be useful for debugging but it is not allowed for prod versions of filter lists.
*
* > Usage with no arguments will log response payload and URL to browser console;
* > it may be useful for debugging but it is not allowed for prod versions of filter lists.
Expand Down Expand Up @@ -63,7 +65,7 @@ import {
* 1. Remove `Period` tag whose `id` contains `-ad-`, only if request's URL contains `.mpd` and log content
*
* ```adblock
* example.org#%#//scriptlet('xml-prune', 'Period[id*="-ad-"]', '', '.mpd', 'logContent')
* example.org#%#//scriptlet('xml-prune', 'Period[id*="-ad-"]', '', '.mpd', 'true')
* ```
*
* 1. Remove `Period` tag whose `id` contains `pre-roll` and remove `duration` attribute from the `Period` tag
Expand Down Expand Up @@ -93,7 +95,7 @@ import {
*/
/* eslint-enable max-len */

export function xmlPrune(source, propsToRemove, optionalProp = '', urlToMatch = '', logContent) {
export function xmlPrune(source, propsToRemove, optionalProp = '', urlToMatch = '', verbose = false) {
// do nothing if browser does not support Reflect, fetch or Proxy (e.g. Internet Explorer)
// https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/fetch
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Proxy
Expand All @@ -107,7 +109,7 @@ export function xmlPrune(source, propsToRemove, optionalProp = '', urlToMatch =

let shouldPruneResponse = false;

const shouldLogContent = logContent === 'logContent';
const shouldLogContent = verbose === 'true';

const urlMatchRegexp = toRegExp(urlToMatch);

Expand Down
4 changes: 2 additions & 2 deletions tests/scriptlets/m3u-prune.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -354,8 +354,8 @@ if (!isSupported) {
const M3U8_PATH = M3U8_OBJECTS_PATH_01;
const MATCH_DATA = 'tvessaiprod.nbcuni.com/video/';
const MATCH_URL = '.m3u8';
const LOG_CONTENT = 'logContent';
const scriptletArgs = [MATCH_DATA, MATCH_URL, LOG_CONTENT];
const VERBOSE = 'true';
const scriptletArgs = [MATCH_DATA, MATCH_URL, VERBOSE];

runScriptlet(name, scriptletArgs);

Expand Down
4 changes: 2 additions & 2 deletions tests/scriptlets/trusted-replace-fetch-response.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -203,8 +203,8 @@ if (!isSupported) {
const PATTERN = '*';
const REPLACEMENT = 'foo bar';
const PROPS_TO_MATCH = 'test01 method:GET';
const LOG_CONTENT = 'logContent';
runScriptlet(name, [PATTERN, REPLACEMENT, PROPS_TO_MATCH, LOG_CONTENT]);
const VERBOSE = 'true';
runScriptlet(name, [PATTERN, REPLACEMENT, PROPS_TO_MATCH, VERBOSE]);

const response = await fetch(INPUT_JSON_PATH, init);
const actualTextContent = await response.text();
Expand Down
4 changes: 2 additions & 2 deletions tests/scriptlets/trusted-replace-node-text.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -146,15 +146,15 @@ test('Log content', (assert) => {
const textMatch = 'log';
const pattern = 'foo';
const replacement = 'bar';
const logContent = 'logContent';
const verbose = 'verbose';

const text = 'foo log';
const expectedText = 'bar log';

const nodeBefore = addNode('div', text);
const safeNodeBefore = addNode('a', text);

runScriptlet(name, [nodeName, textMatch, pattern, replacement, logContent]);
runScriptlet(name, [nodeName, textMatch, pattern, replacement, verbose]);

const nodeAfter = addNode('div', text);
const safeNodeAfter = addNode('span', text);
Expand Down
4 changes: 2 additions & 2 deletions tests/scriptlets/trusted-replace-xhr-response.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,8 @@ if (isSupported) {
const URL = `${FETCH_OBJECTS_PATH}/test01.json`;
const PATTERN = '*';
const REPLACEMENT = 'foo bar';
const LOG_CONTENT = 'logContent';
const MATCH_DATA = [PATTERN, REPLACEMENT, `${URL} method:${METHOD}`, LOG_CONTENT];
const VERBOSE = 'true';
const MATCH_DATA = [PATTERN, REPLACEMENT, `${URL} method:${METHOD}`, VERBOSE];

runScriptlet(name, MATCH_DATA);

Expand Down
4 changes: 2 additions & 2 deletions tests/scriptlets/xml-prune.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -186,8 +186,8 @@ if (!isSupported) {
const MATCH_DATA = "Period[id*='-ad-']";
const OPTIONAL_MATCH = '';
const MATCH_URL = '.mpd';
const LOG_CONTENT = 'logContent';
const scriptletArgs = [MATCH_DATA, OPTIONAL_MATCH, MATCH_URL, LOG_CONTENT];
const VERBOSE = 'true';
const scriptletArgs = [MATCH_DATA, OPTIONAL_MATCH, MATCH_URL, VERBOSE];

runScriptlet(name, scriptletArgs);

Expand Down

0 comments on commit ce58a21

Please sign in to comment.