Skip to content

Commit 156aa22

Browse files
committed
use proxy for prevent-setTimeout/setInterval. AG-5975 #110
Merge in ADGUARD-FILTERS/scriptlets from fix/AG-5975 to master Squashed commit of the following: commit 7800ec5 Author: Slava Leleka <v.leleka@adguard.com> Date: Fri Jul 9 07:54:25 2021 +0300 bump version, rebuild commit f97cc25 Author: Slava Leleka <v.leleka@adguard.com> Date: Thu Jul 8 15:23:46 2021 +0300 use proxy for prevent-setTimeout/setInterval
1 parent a21596e commit 156aa22

9 files changed

Lines changed: 285 additions & 59 deletions

File tree

dist/build.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
version=1.3.23
1+
version=1.4.0

dist/cjs/scriptlets.cjs.js

Lines changed: 93 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11

22
/**
33
* AdGuard Scriptlets
4-
* Version 1.3.23
4+
* Version 1.4.0
55
*/
66

77
/**
@@ -1721,6 +1721,10 @@ abortOnPropertyWrite.injections = [randomId, setPropertyAccess, getPropertyInCha
17211721
/* eslint-enable max-len */
17221722

17231723
function preventSetTimeout(source, match, delay) {
1724+
// if browser does not support Proxy (e.g. Internet Explorer),
1725+
// we use none-proxy "legacy" wrapper for preventing
1726+
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Proxy
1727+
var isProxySupported = typeof Proxy !== 'undefined';
17241728
var nativeTimeout = window.setTimeout;
17251729
var log = console.log.bind(console); // eslint-disable-line no-console
17261730
// logs setTimeouts to console if no arguments have been specified
@@ -1735,20 +1739,30 @@ function preventSetTimeout(source, match, delay) {
17351739
isInvertedDelayMatch = _parseDelayArg.isInvertedDelayMatch,
17361740
delayMatch = _parseDelayArg.delayMatch;
17371741

1738-
var timeoutWrapper = function timeoutWrapper(callback, timeout) {
1742+
var getShouldPrevent = function getShouldPrevent(callbackStr, timeout) {
1743+
var shouldPrevent = false;
1744+
1745+
if (!delayMatch) {
1746+
shouldPrevent = matchRegexp.test(callbackStr) !== isInvertedMatch;
1747+
} else if (!match) {
1748+
shouldPrevent = timeout === delayMatch !== isInvertedDelayMatch;
1749+
} else {
1750+
shouldPrevent = matchRegexp.test(callbackStr) !== isInvertedMatch && timeout === delayMatch !== isInvertedDelayMatch;
1751+
}
1752+
1753+
return shouldPrevent;
1754+
};
1755+
1756+
var legacyTimeoutWrapper = function legacyTimeoutWrapper(callback, timeout) {
17391757
var shouldPrevent = false; // https://github.com/AdguardTeam/Scriptlets/issues/105
17401758

17411759
var cbString = String(callback);
17421760

17431761
if (shouldLog) {
17441762
hit(source);
17451763
log("setTimeout(".concat(cbString, ", ").concat(timeout, ")"));
1746-
} else if (!delayMatch) {
1747-
shouldPrevent = matchRegexp.test(cbString) !== isInvertedMatch;
1748-
} else if (!match) {
1749-
shouldPrevent = timeout === delayMatch !== isInvertedDelayMatch;
17501764
} else {
1751-
shouldPrevent = matchRegexp.test(cbString) !== isInvertedMatch && timeout === delayMatch !== isInvertedDelayMatch;
1765+
shouldPrevent = getShouldPrevent(cbString, timeout);
17521766
}
17531767

17541768
if (shouldPrevent) {
@@ -1763,7 +1777,32 @@ function preventSetTimeout(source, match, delay) {
17631777
return nativeTimeout.apply(window, [callback, timeout].concat(args));
17641778
};
17651779

1766-
window.setTimeout = timeoutWrapper;
1780+
var handlerWrapper = function handlerWrapper(target, thisArg, args) {
1781+
var callback = args[0];
1782+
var timeout = args[1];
1783+
var shouldPrevent = false; // https://github.com/AdguardTeam/Scriptlets/issues/105
1784+
1785+
var cbString = String(callback);
1786+
1787+
if (shouldLog) {
1788+
hit(source);
1789+
log("setTimeout(".concat(cbString, ", ").concat(timeout, ")"));
1790+
} else {
1791+
shouldPrevent = getShouldPrevent(cbString, timeout);
1792+
}
1793+
1794+
if (shouldPrevent) {
1795+
hit(source);
1796+
args[0] = noopFunc;
1797+
}
1798+
1799+
return target.apply(thisArg, args);
1800+
};
1801+
1802+
var setTimeoutHandler = {
1803+
apply: handlerWrapper
1804+
};
1805+
window.setTimeout = isProxySupported ? new Proxy(window.setTimeout, setTimeoutHandler) : legacyTimeoutWrapper;
17671806
}
17681807
preventSetTimeout.names = ['prevent-setTimeout', // aliases are needed for matching the related scriptlet converted into our syntax
17691808
'no-setTimeout-if.js', // new implementation of setTimeout-defuser.js
@@ -1880,6 +1919,10 @@ preventSetTimeout.injections = [hit, noopFunc, parseMatchArg, parseDelayArg, toR
18801919
/* eslint-enable max-len */
18811920

18821921
function preventSetInterval(source, match, delay) {
1922+
// if browser does not support Proxy (e.g. Internet Explorer),
1923+
// we use none-proxy "legacy" wrapper for preventing
1924+
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Proxy
1925+
var isProxySupported = typeof Proxy !== 'undefined';
18831926
var nativeInterval = window.setInterval;
18841927
var log = console.log.bind(console); // eslint-disable-line no-console
18851928
// logs setIntervals to console if no arguments have been specified
@@ -1894,20 +1937,30 @@ function preventSetInterval(source, match, delay) {
18941937
isInvertedDelayMatch = _parseDelayArg.isInvertedDelayMatch,
18951938
delayMatch = _parseDelayArg.delayMatch;
18961939

1897-
var intervalWrapper = function intervalWrapper(callback, interval) {
1940+
var getShouldPrevent = function getShouldPrevent(callbackStr, interval) {
1941+
var shouldPrevent = false;
1942+
1943+
if (!delayMatch) {
1944+
shouldPrevent = matchRegexp.test(callbackStr) !== isInvertedMatch;
1945+
} else if (!match) {
1946+
shouldPrevent = interval === delayMatch !== isInvertedDelayMatch;
1947+
} else {
1948+
shouldPrevent = matchRegexp.test(callbackStr) !== isInvertedMatch && interval === delayMatch !== isInvertedDelayMatch;
1949+
}
1950+
1951+
return shouldPrevent;
1952+
};
1953+
1954+
var legachyIntervalWrapper = function legachyIntervalWrapper(callback, interval) {
18981955
var shouldPrevent = false; // https://github.com/AdguardTeam/Scriptlets/issues/105
18991956

19001957
var cbString = String(callback);
19011958

19021959
if (shouldLog) {
19031960
hit(source);
19041961
log("setInterval(".concat(cbString, ", ").concat(interval, ")"));
1905-
} else if (!delayMatch) {
1906-
shouldPrevent = matchRegexp.test(cbString) !== isInvertedMatch;
1907-
} else if (!match) {
1908-
shouldPrevent = interval === delayMatch !== isInvertedDelayMatch;
19091962
} else {
1910-
shouldPrevent = matchRegexp.test(cbString) !== isInvertedMatch && interval === delayMatch !== isInvertedDelayMatch;
1963+
shouldPrevent = getShouldPrevent(cbString, interval);
19111964
}
19121965

19131966
if (shouldPrevent) {
@@ -1922,7 +1975,32 @@ function preventSetInterval(source, match, delay) {
19221975
return nativeInterval.apply(window, [callback, interval].concat(args));
19231976
};
19241977

1925-
window.setInterval = intervalWrapper;
1978+
var handlerWrapper = function handlerWrapper(target, thisArg, args) {
1979+
var callback = args[0];
1980+
var interval = args[1];
1981+
var shouldPrevent = false; // https://github.com/AdguardTeam/Scriptlets/issues/105
1982+
1983+
var cbString = String(callback);
1984+
1985+
if (shouldLog) {
1986+
hit(source);
1987+
log("setTimeout(".concat(cbString, ", ").concat(interval, ")"));
1988+
} else {
1989+
shouldPrevent = getShouldPrevent(cbString, interval);
1990+
}
1991+
1992+
if (shouldPrevent) {
1993+
hit(source);
1994+
args[0] = noopFunc;
1995+
}
1996+
1997+
return target.apply(thisArg, args);
1998+
};
1999+
2000+
var setIntervalHandler = {
2001+
apply: handlerWrapper
2002+
};
2003+
window.setInterval = isProxySupported ? new Proxy(window.setInterval, setIntervalHandler) : legachyIntervalWrapper;
19262004
}
19272005
preventSetInterval.names = ['prevent-setInterval', // aliases are needed for matching the related scriptlet converted into our syntax
19282006
'no-setInterval-if.js', // new implementation of setInterval-defuser.js

dist/redirects.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11

22
/**
33
* AdGuard Scriptlets
4-
* Version 1.3.23
4+
* Version 1.4.0
55
*/
66

77
var Redirects = (function () {

dist/redirects.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#
22
# AdGuard Scriptlets (Redirects Source)
3-
# Version 1.3.23
3+
# Version 1.4.0
44
#
55
- title: 1x1-transparent.gif
66
description: |-

dist/scriptlets.corelibs.json

Lines changed: 3 additions & 3 deletions
Large diffs are not rendered by default.

dist/scriptlets.js

Lines changed: 93 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11

22
/**
33
* AdGuard Scriptlets
4-
* Version 1.3.23
4+
* Version 1.4.0
55
*/
66

77
(function () {
@@ -1723,6 +1723,10 @@
17231723
/* eslint-enable max-len */
17241724

17251725
function preventSetTimeout(source, match, delay) {
1726+
// if browser does not support Proxy (e.g. Internet Explorer),
1727+
// we use none-proxy "legacy" wrapper for preventing
1728+
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Proxy
1729+
var isProxySupported = typeof Proxy !== 'undefined';
17261730
var nativeTimeout = window.setTimeout;
17271731
var log = console.log.bind(console); // eslint-disable-line no-console
17281732
// logs setTimeouts to console if no arguments have been specified
@@ -1737,20 +1741,30 @@
17371741
isInvertedDelayMatch = _parseDelayArg.isInvertedDelayMatch,
17381742
delayMatch = _parseDelayArg.delayMatch;
17391743

1740-
var timeoutWrapper = function timeoutWrapper(callback, timeout) {
1744+
var getShouldPrevent = function getShouldPrevent(callbackStr, timeout) {
1745+
var shouldPrevent = false;
1746+
1747+
if (!delayMatch) {
1748+
shouldPrevent = matchRegexp.test(callbackStr) !== isInvertedMatch;
1749+
} else if (!match) {
1750+
shouldPrevent = timeout === delayMatch !== isInvertedDelayMatch;
1751+
} else {
1752+
shouldPrevent = matchRegexp.test(callbackStr) !== isInvertedMatch && timeout === delayMatch !== isInvertedDelayMatch;
1753+
}
1754+
1755+
return shouldPrevent;
1756+
};
1757+
1758+
var legacyTimeoutWrapper = function legacyTimeoutWrapper(callback, timeout) {
17411759
var shouldPrevent = false; // https://github.com/AdguardTeam/Scriptlets/issues/105
17421760

17431761
var cbString = String(callback);
17441762

17451763
if (shouldLog) {
17461764
hit(source);
17471765
log("setTimeout(".concat(cbString, ", ").concat(timeout, ")"));
1748-
} else if (!delayMatch) {
1749-
shouldPrevent = matchRegexp.test(cbString) !== isInvertedMatch;
1750-
} else if (!match) {
1751-
shouldPrevent = timeout === delayMatch !== isInvertedDelayMatch;
17521766
} else {
1753-
shouldPrevent = matchRegexp.test(cbString) !== isInvertedMatch && timeout === delayMatch !== isInvertedDelayMatch;
1767+
shouldPrevent = getShouldPrevent(cbString, timeout);
17541768
}
17551769

17561770
if (shouldPrevent) {
@@ -1765,7 +1779,32 @@
17651779
return nativeTimeout.apply(window, [callback, timeout].concat(args));
17661780
};
17671781

1768-
window.setTimeout = timeoutWrapper;
1782+
var handlerWrapper = function handlerWrapper(target, thisArg, args) {
1783+
var callback = args[0];
1784+
var timeout = args[1];
1785+
var shouldPrevent = false; // https://github.com/AdguardTeam/Scriptlets/issues/105
1786+
1787+
var cbString = String(callback);
1788+
1789+
if (shouldLog) {
1790+
hit(source);
1791+
log("setTimeout(".concat(cbString, ", ").concat(timeout, ")"));
1792+
} else {
1793+
shouldPrevent = getShouldPrevent(cbString, timeout);
1794+
}
1795+
1796+
if (shouldPrevent) {
1797+
hit(source);
1798+
args[0] = noopFunc;
1799+
}
1800+
1801+
return target.apply(thisArg, args);
1802+
};
1803+
1804+
var setTimeoutHandler = {
1805+
apply: handlerWrapper
1806+
};
1807+
window.setTimeout = isProxySupported ? new Proxy(window.setTimeout, setTimeoutHandler) : legacyTimeoutWrapper;
17691808
}
17701809
preventSetTimeout.names = ['prevent-setTimeout', // aliases are needed for matching the related scriptlet converted into our syntax
17711810
'no-setTimeout-if.js', // new implementation of setTimeout-defuser.js
@@ -1882,6 +1921,10 @@
18821921
/* eslint-enable max-len */
18831922

18841923
function preventSetInterval(source, match, delay) {
1924+
// if browser does not support Proxy (e.g. Internet Explorer),
1925+
// we use none-proxy "legacy" wrapper for preventing
1926+
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Proxy
1927+
var isProxySupported = typeof Proxy !== 'undefined';
18851928
var nativeInterval = window.setInterval;
18861929
var log = console.log.bind(console); // eslint-disable-line no-console
18871930
// logs setIntervals to console if no arguments have been specified
@@ -1896,20 +1939,30 @@
18961939
isInvertedDelayMatch = _parseDelayArg.isInvertedDelayMatch,
18971940
delayMatch = _parseDelayArg.delayMatch;
18981941

1899-
var intervalWrapper = function intervalWrapper(callback, interval) {
1942+
var getShouldPrevent = function getShouldPrevent(callbackStr, interval) {
1943+
var shouldPrevent = false;
1944+
1945+
if (!delayMatch) {
1946+
shouldPrevent = matchRegexp.test(callbackStr) !== isInvertedMatch;
1947+
} else if (!match) {
1948+
shouldPrevent = interval === delayMatch !== isInvertedDelayMatch;
1949+
} else {
1950+
shouldPrevent = matchRegexp.test(callbackStr) !== isInvertedMatch && interval === delayMatch !== isInvertedDelayMatch;
1951+
}
1952+
1953+
return shouldPrevent;
1954+
};
1955+
1956+
var legachyIntervalWrapper = function legachyIntervalWrapper(callback, interval) {
19001957
var shouldPrevent = false; // https://github.com/AdguardTeam/Scriptlets/issues/105
19011958

19021959
var cbString = String(callback);
19031960

19041961
if (shouldLog) {
19051962
hit(source);
19061963
log("setInterval(".concat(cbString, ", ").concat(interval, ")"));
1907-
} else if (!delayMatch) {
1908-
shouldPrevent = matchRegexp.test(cbString) !== isInvertedMatch;
1909-
} else if (!match) {
1910-
shouldPrevent = interval === delayMatch !== isInvertedDelayMatch;
19111964
} else {
1912-
shouldPrevent = matchRegexp.test(cbString) !== isInvertedMatch && interval === delayMatch !== isInvertedDelayMatch;
1965+
shouldPrevent = getShouldPrevent(cbString, interval);
19131966
}
19141967

19151968
if (shouldPrevent) {
@@ -1924,7 +1977,32 @@
19241977
return nativeInterval.apply(window, [callback, interval].concat(args));
19251978
};
19261979

1927-
window.setInterval = intervalWrapper;
1980+
var handlerWrapper = function handlerWrapper(target, thisArg, args) {
1981+
var callback = args[0];
1982+
var interval = args[1];
1983+
var shouldPrevent = false; // https://github.com/AdguardTeam/Scriptlets/issues/105
1984+
1985+
var cbString = String(callback);
1986+
1987+
if (shouldLog) {
1988+
hit(source);
1989+
log("setTimeout(".concat(cbString, ", ").concat(interval, ")"));
1990+
} else {
1991+
shouldPrevent = getShouldPrevent(cbString, interval);
1992+
}
1993+
1994+
if (shouldPrevent) {
1995+
hit(source);
1996+
args[0] = noopFunc;
1997+
}
1998+
1999+
return target.apply(thisArg, args);
2000+
};
2001+
2002+
var setIntervalHandler = {
2003+
apply: handlerWrapper
2004+
};
2005+
window.setInterval = isProxySupported ? new Proxy(window.setInterval, setIntervalHandler) : legachyIntervalWrapper;
19282006
}
19292007
preventSetInterval.names = ['prevent-setInterval', // aliases are needed for matching the related scriptlet converted into our syntax
19302008
'no-setInterval-if.js', // new implementation of setInterval-defuser.js

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@adguard/scriptlets",
3-
"version": "1.3.24",
3+
"version": "1.4.0",
44
"description": "AdGuard's JavaScript library of Scriptlets and Redirect resources",
55
"scripts": {
66
"watch": "rollup -c -w",

0 commit comments

Comments
 (0)