Skip to content

Commit

Permalink
improve raw delay parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
stanislav-atr committed Dec 15, 2022
1 parent e0e6698 commit baca1d8
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 7 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

### Changed

- add decimal delay matching for `prevent-setInterval` and `prevent-seTimeout` [#247](https://github.com/AdguardTeam/Scriptlets/issues/247)
- add decimal delay matching for `prevent-setInterval` and `prevent-setTimeout` [#247](https://github.com/AdguardTeam/Scriptlets/issues/247)

## v1.7.10

Expand Down
5 changes: 3 additions & 2 deletions src/helpers/prevent-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
parseMatchArg,
isValidMatchNumber,
isValidMatchStr,
parseRawDelay,
} from './string-utils';

/**
Expand Down Expand Up @@ -46,8 +47,8 @@ export const isPreventionNeeded = ({
const { isInvertedDelayMatch, delayMatch } = parseDelayArg(matchDelay);

// https://github.com/AdguardTeam/Scriptlets/issues/247
// type check is required to pass non-number values like null
const parsedDelay = typeof delay === 'number' ? Math.floor(delay) : delay;
// Parse delay for decimal, string and non-number values
const parsedDelay = parseRawDelay(delay);

let shouldPrevent = false;
// https://github.com/AdguardTeam/Scriptlets/issues/105
Expand Down
11 changes: 11 additions & 0 deletions src/helpers/string-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,17 @@ export const parseDelayArg = (delay) => {
return { isInvertedDelayMatch, delayMatch };
};

/**
* Parses delay argument of setTimeout / setInterval methods into
* rounded down number for number/string values or passes on for other types.
* Needed for prevent-setTimeout and prevent-setInterval
* @param {number|string} delay
* @returns parsed delay
*/
export const parseRawDelay = (delay) => {
return parseInt(delay, 10) ? Math.floor(parseInt(delay, 10)) : delay;
};

/**
* Converts object to string for logging
* @param {Object} obj data object
Expand Down
2 changes: 2 additions & 0 deletions src/scriptlets/prevent-setInterval.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
escapeRegExp,
nativeIsFinite,
isValidMatchNumber,
parseRawDelay,
} from '../helpers/index';

/* eslint-disable max-len */
Expand Down Expand Up @@ -219,4 +220,5 @@ preventSetInterval.injections = [
escapeRegExp,
nativeIsFinite,
isValidMatchNumber,
parseRawDelay,
];
2 changes: 2 additions & 0 deletions src/scriptlets/prevent-setTimeout.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
isValidStrPattern,
nativeIsFinite,
isValidMatchNumber,
parseRawDelay,
} from '../helpers/index';

/* eslint-disable max-len */
Expand Down Expand Up @@ -222,4 +223,5 @@ preventSetTimeout.injections = [
isValidStrPattern,
nativeIsFinite,
isValidMatchNumber,
parseRawDelay,
];
17 changes: 16 additions & 1 deletion tests/scriptlets/prevent-setInterval.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -405,16 +405,22 @@ test('match any callback + decimal delay', (assert) => {
testIntervals.push(intervalTest3);
});

test('match any callback + non-number delay', (assert) => {
test('match any callback + non-number, decimal and string delays', (assert) => {
const done = assert.async();
window.one = 'old one';
window.two = 'old two';
window.three = 'old three';
window.four = 'old four';
window.five = 'old five';
// We need to run our assertion after all timeouts
setTimeout(() => {
assert.equal(window.one, 'old one', 'property \'one\' should NOT be changed');
assert.equal(window.two, 'NEW TWO', 'property \'two\' should be changed');
assert.equal(window.three, 'NEW THREE', 'property \'three\' should be changed');

assert.equal(window.four, 'old four', 'property \'four\' should NOT be changed');
assert.equal(window.five, 'NEW FIVE', 'property \'five\' should be changed');

assert.strictEqual(window.hit, 'FIRED', 'hit fired');
done();
}, 100);
Expand All @@ -435,4 +441,13 @@ test('match any callback + non-number delay', (assert) => {
const third = () => { window.three = 'NEW THREE'; };
const intervalTest3 = setInterval(third, false);
testIntervals.push(intervalTest3);

// test with string delays
const fourth = () => { window.four = 'NEW FOUR'; };
const intervalTest4 = setInterval(fourth, '25.123');
testIntervals.push(intervalTest4);

const fifth = () => { window.five = 'NEW FIVE'; };
const intervalTest5 = setInterval(fifth, '10');
testIntervals.push(intervalTest5);
});
21 changes: 18 additions & 3 deletions tests/scriptlets/prevent-setTimeout.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -406,27 +406,33 @@ test('match any callback + decimal delay', (assert) => {
testTimeouts.push(timeoutTest3);
});

test('match any callback + non-number delay', (assert) => {
test('match any callback + non-number, decimal and string delays', (assert) => {
const done = assert.async();
window.one = 'one';
window.two = 'two';
window.three = 'three';
window.four = 'old four';
window.five = 'old five';
// We need to run our assertion after all timeouts
nativeSetTimeout(() => {
assert.equal(window.one, 'one', 'property \'one\' should NOT be changed');
assert.equal(window.two, 'NEW TWO', 'property \'two\' should be changed');
assert.equal(window.three, 'NEW THREE', 'property \'three\' should be changed');

assert.equal(window.four, 'old four', 'property \'four\' should NOT be changed');
assert.equal(window.five, 'NEW FIVE', 'property \'five\' should be changed');

assert.strictEqual(window.hit, 'FIRED', 'hit fired');
done();
}, 100);

// run scriptlet code
const scriptletArgs = ['', '30'];
const scriptletArgs = ['', '25'];
runScriptlet(name, scriptletArgs);

// only this one SHOULD NOT be prevented because of delay mismatch
const first = () => { window.one = 'NEW ONE'; };
const timeoutTest1 = setTimeout(first, 30.153);
const timeoutTest1 = setTimeout(first, 25.123);
testTimeouts.push(timeoutTest1);

const second = () => { window.two = 'NEW TWO'; };
Expand All @@ -436,4 +442,13 @@ test('match any callback + non-number delay', (assert) => {
const third = () => { window.three = 'NEW THREE'; };
const timeoutTest3 = setTimeout(third, true);
testTimeouts.push(timeoutTest3);

// test with string delays
const fourth = () => { window.four = 'NEW FOUR'; };
const timeoutTest4 = setTimeout(fourth, '25.123');
testTimeouts.push(timeoutTest4);

const fifth = () => { window.five = 'NEW FIVE'; };
const timeoutTest5 = setTimeout(fifth, '10');
testTimeouts.push(timeoutTest5);
});

0 comments on commit baca1d8

Please sign in to comment.