Skip to content

Commit

Permalink
add typecheck for delay + tests
Browse files Browse the repository at this point in the history
  • Loading branch information
stanislav-atr committed Dec 15, 2022
1 parent 60505e9 commit e0e6698
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 3 deletions.
7 changes: 4 additions & 3 deletions src/helpers/prevent-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,18 +46,19 @@ export const isPreventionNeeded = ({
const { isInvertedDelayMatch, delayMatch } = parseDelayArg(matchDelay);

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

let shouldPrevent = false;
// https://github.com/AdguardTeam/Scriptlets/issues/105
const callbackStr = String(callback);
if (delayMatch === null) {
shouldPrevent = matchRegexp.test(callbackStr) !== isInvertedMatch;
} else if (!matchCallback) {
shouldPrevent = (roundedDelay === delayMatch) !== isInvertedDelayMatch;
shouldPrevent = (parsedDelay === delayMatch) !== isInvertedDelayMatch;
} else {
shouldPrevent = matchRegexp.test(callbackStr) !== isInvertedMatch
&& (roundedDelay === delayMatch) !== isInvertedDelayMatch;
&& (parsedDelay === delayMatch) !== isInvertedDelayMatch;
}
return shouldPrevent;
};
32 changes: 32 additions & 0 deletions tests/scriptlets/prevent-setInterval.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -404,3 +404,35 @@ test('match any callback + decimal delay', (assert) => {
const intervalTest3 = setInterval(third, 10.95);
testIntervals.push(intervalTest3);
});

test('match any callback + non-number delay', (assert) => {
const done = assert.async();
window.one = 'old one';
window.two = 'old two';
window.three = 'old three';
// 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.strictEqual(window.hit, 'FIRED', 'hit fired');
done();
}, 100);

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

// only this one SHOULD NOT be prevented because of delay mismatch
const one = () => { window.one = 'NEW ONE'; };
const intervalTest1 = setInterval(one, 25.123);
testIntervals.push(intervalTest1);

const second = () => { window.two = 'NEW TWO'; };
const intervalTest2 = setInterval(second, null);
testIntervals.push(intervalTest2);

const third = () => { window.three = 'NEW THREE'; };
const intervalTest3 = setInterval(third, false);
testIntervals.push(intervalTest3);
});
32 changes: 32 additions & 0 deletions tests/scriptlets/prevent-setTimeout.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -405,3 +405,35 @@ test('match any callback + decimal delay', (assert) => {
const timeoutTest3 = setTimeout(third, 10.95);
testTimeouts.push(timeoutTest3);
});

test('match any callback + non-number delay', (assert) => {
const done = assert.async();
window.one = 'one';
window.two = 'two';
window.three = 'three';
// 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.strictEqual(window.hit, 'FIRED', 'hit fired');
done();
}, 100);

// run scriptlet code
const scriptletArgs = ['', '30'];
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);
testTimeouts.push(timeoutTest1);

const second = () => { window.two = 'NEW TWO'; };
const timeoutTest2 = setTimeout(second, null);
testTimeouts.push(timeoutTest2);

const third = () => { window.three = 'NEW THREE'; };
const timeoutTest3 = setTimeout(third, true);
testTimeouts.push(timeoutTest3);
});

0 comments on commit e0e6698

Please sign in to comment.