Skip to content

Commit

Permalink
improve json-prune scriptlet #171 AG-11879
Browse files Browse the repository at this point in the history
Merge in ADGUARD-FILTERS/scriptlets from fix/AG-11879 to release/v1.6

Squashed commit of the following:

commit d50891e
Merge: 96c52f9 bc97ef4
Author: Stanislav A <[email protected]>
Date:   Mon Mar 14 17:07:17 2022 +0300

    Merge branch 'release/v1.6' into fix/AG-11879

commit 96c52f9
Author: Stanislav A <[email protected]>
Date:   Mon Mar 14 15:25:34 2022 +0300

    add comment

commit 2a4abf1
Author: Stanislav A <[email protected]>
Date:   Fri Mar 11 16:13:06 2022 +0300

    improve json-prune scriptlet
  • Loading branch information
stanislav-atr committed Mar 14, 2022
1 parent bc97ef4 commit d3b284d
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
21 changes: 19 additions & 2 deletions src/scriptlets/json-prune.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,13 +77,17 @@ import {
* ```
* example.org#%#//scriptlet('json-prune')
* ```
*
* 7. Call with only second argument will log the current hostname and matched json payload at the console
* ```
* example.org#%#//scriptlet('json-prune', '', '"id":"117458"')
* ```
*/
/* eslint-enable max-len */
export function jsonPrune(source, propsToRemove, requiredInitialProps, stack) {
if (!!stack && !matchStackTrace(stack, new Error().stack)) {
return;
}

// eslint-disable-next-line no-console
const log = console.log.bind(console);
const prunePaths = propsToRemove !== undefined && propsToRemove !== ''
Expand All @@ -99,6 +103,19 @@ export function jsonPrune(source, propsToRemove, requiredInitialProps, stack) {
}

let shouldProcess;

// Only log hostname and matched JSON payload if only second argument is present
if (prunePaths.length === 0 && requiredPaths.length > 0) {
const rootString = JSON.stringify(root);
const matchRegex = toRegExp(requiredPaths.join(''));
const shouldLog = matchRegex.test(rootString);
if (shouldLog) {
log(window.location.hostname, root);
shouldProcess = false;
return shouldProcess;
}
}

for (let i = 0; i < requiredPaths.length; i += 1) {
const requiredPath = requiredPaths[i];
const lastNestedPropName = requiredPath.split('.').pop();
Expand Down Expand Up @@ -138,7 +155,7 @@ export function jsonPrune(source, propsToRemove, requiredInitialProps, stack) {
* @param {Object} root
*/
const jsonPruner = (root) => {
if (prunePaths.length === 0) {
if (prunePaths.length === 0 && requiredPaths.length === 0) {
log(window.location.hostname, root);
return root;
}
Expand Down
12 changes: 12 additions & 0 deletions tests/scriptlets/json-prune.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,18 @@ test('does NOT remove propsToRemove if invoked without parameter propsToRemove a
JSON.parse('{"a":1, "b":2}');
});

test('logs matched object and hostname if invoked with only second arg', (assert) => {
assert.expect(2);
console.log = (host, params) => {
// eslint-disable-next-line compat/compat
assert.strictEqual(host, window.location.hostname, 'should log hostname in console');
assert.deepEqual(params, { a: 1 }, 'should log parameters in console');
};
runScriptlet('json-prune', '', '"a":1');
JSON.parse('{"a":1}');
JSON.parse('{"b":2}');
});

test('removes propsToRemove + stack match', (assert) => {
const stackMatch = 'json-prune';

Expand Down

0 comments on commit d3b284d

Please sign in to comment.