Skip to content

Commit

Permalink
remove legacy wrappers from prevent-setTimeout and prevent-setInterva…
Browse files Browse the repository at this point in the history
…l scriptlets
  • Loading branch information
stanislav-atr committed Jan 12, 2023
1 parent c19db5d commit 53884e6
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 62 deletions.
32 changes: 1 addition & 31 deletions src/scriptlets/prevent-setInterval.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,37 +137,9 @@ import {
*/
/* eslint-enable max-len */
export function preventSetInterval(source, matchCallback, matchDelay) {
// if browser does not support Proxy (e.g. Internet Explorer),
// we use none-proxy "legacy" wrapper for preventing
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Proxy
const isProxySupported = typeof Proxy !== 'undefined';

const nativeInterval = window.setInterval;

// logs setIntervals to console if no arguments have been specified
const shouldLog = ((typeof matchCallback === 'undefined') && (typeof matchDelay === 'undefined'));

const legacyIntervalWrapper = (callback, delay, ...args) => {
let shouldPrevent = false;
if (shouldLog) {
hit(source);
// https://github.com/AdguardTeam/Scriptlets/issues/105
logMessage(source, `setInterval(${String(callback)}, ${delay})`, true);
} else {
shouldPrevent = isPreventionNeeded({
callback,
delay,
matchCallback,
matchDelay,
});
}
if (shouldPrevent) {
hit(source);
return nativeInterval(noopFunc, delay);
}
return nativeInterval.apply(window, [callback, delay, ...args]);
};

const handlerWrapper = (target, thisArg, args) => {
const callback = args[0];
const delay = args[1];
Expand Down Expand Up @@ -195,9 +167,7 @@ export function preventSetInterval(source, matchCallback, matchDelay) {
apply: handlerWrapper,
};

window.setInterval = isProxySupported
? new Proxy(window.setInterval, setIntervalHandler)
: legacyIntervalWrapper;
window.setInterval = new Proxy(window.setInterval, setIntervalHandler);
}

preventSetInterval.names = [
Expand Down
32 changes: 1 addition & 31 deletions src/scriptlets/prevent-setTimeout.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,37 +137,9 @@ import {
*/
/* eslint-enable max-len */
export function preventSetTimeout(source, matchCallback, matchDelay) {
// if browser does not support Proxy (e.g. Internet Explorer),
// we use none-proxy "legacy" wrapper for preventing
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Proxy
const isProxySupported = typeof Proxy !== 'undefined';

const nativeTimeout = window.setTimeout;

// logs setTimeouts to console if no arguments have been specified
const shouldLog = ((typeof matchCallback === 'undefined') && (typeof matchDelay === 'undefined'));

const legacyTimeoutWrapper = (callback, delay, ...args) => {
let shouldPrevent = false;
if (shouldLog) {
hit(source);
// https://github.com/AdguardTeam/Scriptlets/issues/105
logMessage(source, `setTimeout(${String(callback)}, ${delay})`, true);
} else {
shouldPrevent = isPreventionNeeded({
callback,
delay,
matchCallback,
matchDelay,
});
}
if (shouldPrevent) {
hit(source);
return nativeTimeout(noopFunc, delay);
}
return nativeTimeout.apply(window, [callback, delay, ...args]);
};

const handlerWrapper = (target, thisArg, args) => {
const callback = args[0];
const delay = args[1];
Expand Down Expand Up @@ -195,9 +167,7 @@ export function preventSetTimeout(source, matchCallback, matchDelay) {
apply: handlerWrapper,
};

window.setTimeout = isProxySupported
? new Proxy(window.setTimeout, setTimeoutHandler)
: legacyTimeoutWrapper;
window.setTimeout = new Proxy(window.setTimeout, setTimeoutHandler);
}

preventSetTimeout.names = [
Expand Down

0 comments on commit 53884e6

Please sign in to comment.