Skip to content

Commit 2f91f88

Browse files
committed
AG-27573 Improve set-constant - add additional argument to set proxy trap. #330
Squashed commit of the following: commit 155b283 Author: Slava Leleka <v.leleka@adguard.com> Date: Wed Nov 15 12:07:06 2023 +0200 update compatibility table commit c16e6c1 Author: Adam Wróblewski <adam@adguard.com> Date: Wed Nov 15 07:51:18 2023 +0100 Add example to documentation commit bcd55a5 Merge: bf05db3 b50f9c9 Author: Adam Wróblewski <adam@adguard.com> Date: Tue Nov 14 19:17:08 2023 +0100 Merge branch 'master' into fix/AG-27573_01 commit bf05db3 Author: Adam Wróblewski <adam@adguard.com> Date: Tue Nov 14 18:51:41 2023 +0100 Improve set-constant - add additional argument to set proxy trap
1 parent b50f9c9 commit 2f91f88

File tree

5 files changed

+33
-16
lines changed

5 files changed

+33
-16
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77
<!-- TODO: add @added tag to the files with specific version -->
88
<!-- during new scriptlets or redirects releasing -->
99

10+
## [Unreleased]
11+
12+
### Added
13+
14+
- ability to set proxy trap in `set-constant` scriptlet [#330](https://github.com/AdguardTeam/Scriptlets/issues/330)
15+
1016
## [v1.9.91] - 2023-11-13
1117

1218
### Added
@@ -284,6 +290,7 @@ prevent inline `onerror` and match `link` tag [#276](https://github.com/AdguardT
284290
- `metrika-yandex-tag` [#254](https://github.com/AdguardTeam/Scriptlets/issues/254)
285291
- `googlesyndication-adsbygoogle` [#252](https://github.com/AdguardTeam/Scriptlets/issues/252)
286292

293+
[Unreleased]: https://github.com/AdguardTeam/Scriptlets/compare/v1.9.91...HEAD
287294
[v1.9.91]: https://github.com/AdguardTeam/Scriptlets/compare/v1.9.83...v1.9.91
288295
[v1.9.83]: https://github.com/AdguardTeam/Scriptlets/compare/v1.9.72...v1.9.83
289296
[v1.9.72]: https://github.com/AdguardTeam/Scriptlets/compare/v1.9.70...v1.9.72

scripts/compatibility-table.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -269,9 +269,6 @@
269269
{
270270
"ubo": "spoof-css.js"
271271
},
272-
{
273-
"ubo": "replace-node-text.js (rpnt.js)"
274-
},
275272
{
276273
"ubo": "trusted-set-constant.js (trusted-set.js)"
277274
},
@@ -316,6 +313,9 @@
316313
},
317314
{
318315
"ubo": "trusted-set-session-storage-item.js"
316+
},
317+
{
318+
"ubo": "trusted-replace-node-text.js (trusted-rpnt.js, replace-node-text.js, rpnt.js)"
319319
}
320320
],
321321
"redirects": [
@@ -524,4 +524,4 @@
524524
"ubo": "noop-0.5s.mp3"
525525
}
526526
]
527-
}
527+
}

src/scriptlets/set-constant.js

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ import {
4343
* ### Syntax
4444
*
4545
* ```text
46-
* example.org#%#//scriptlet('set-constant', property, value[, stack])
46+
* example.org#%#//scriptlet('set-constant', property, value[, stack,[ valueWrapper[, setProxyTrap]]])
4747
* ```
4848
*
4949
* - `property` — required, path to a property (joined with `.` if needed). The property must be attached to `window`.
@@ -74,6 +74,7 @@ import {
7474
* - `asCallback` – function returning callback, that would return value
7575
* - `asResolved` – Promise that would resolve with value
7676
* - `asRejected` – Promise that would reject with value
77+
* - `setProxyTrap` – optional, boolean, if set to true, proxy trap will be set on the object
7778
*
7879
* ### Examples
7980
*
@@ -113,10 +114,19 @@ import {
113114
* ✔ document.fifth.catch((reason) => reason === 42) // promise rejects with specified number
114115
* ```
115116
*
117+
* ```adblock
118+
* ! Any access to `window.foo.bar` will return `false` and the proxy trap will be set on the `foo` object
119+
* ! It may be required in the case when `foo` object is overwritten by website script
120+
* ! Related to this issue - https://github.com/AdguardTeam/Scriptlets/issues/330
121+
* example.org#%#//scriptlet('set-constant', 'foo.bar', 'false', '', '', 'true')
122+
*
123+
* ✔ window.foo.bar === false
124+
* ```
125+
*
116126
* @added v1.0.4.
117127
*/
118128
/* eslint-enable max-len */
119-
export function setConstant(source, property, value, stack = '', valueWrapper = '') {
129+
export function setConstant(source, property, value, stack = '', valueWrapper = '', setProxyTrap = false) {
120130
const uboAliases = [
121131
'set-constant.js',
122132
'ubo-set-constant.js',
@@ -299,7 +309,7 @@ export function setConstant(source, property, value, stack = '', valueWrapper =
299309
// Get properties which should be checked and remove first one
300310
// because it's current object
301311
const propertiesToCheck = property.split('.').slice(1);
302-
if (!isProxyTrapSet) {
312+
if (setProxyTrap && !isProxyTrapSet) {
303313
isProxyTrapSet = true;
304314
a = new Proxy(a, {
305315
get: (target, propertyKey, val) => {

tests/scriptlets/set-constant.test.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -486,7 +486,7 @@ if (!isSupported) {
486486

487487
// https://github.com/AdguardTeam/Scriptlets/issues/330
488488
test('Check overriden value - array', (assert) => {
489-
runScriptletFromTag('pageData.__banners.0.commercial.mediaUrl', '');
489+
runScriptletFromTag('pageData.__banners.0.commercial.mediaUrl', '', '', '', 'true');
490490
const done = assert.async();
491491
window.pageData = {
492492
__banners: [{
@@ -522,7 +522,7 @@ if (!isSupported) {
522522

523523
// https://github.com/AdguardTeam/Scriptlets/issues/330
524524
test('Check overriden - array 2', (assert) => {
525-
runScriptletFromTag('pageData.__banners.0.commercial.mediaUrl', '');
525+
runScriptletFromTag('pageData.__banners.0.commercial.mediaUrl', '', '', '', 'true');
526526
const done = assert.async();
527527
window.pageData = {
528528
__banners: [{
@@ -652,7 +652,7 @@ if (!isSupported) {
652652

653653
// https://github.com/AdguardTeam/Scriptlets/issues/330
654654
test('Check overriden value - object', (assert) => {
655-
runScriptletFromTag('foo.prototype.abc.qwerty', 'false');
655+
runScriptletFromTag('foo.prototype.abc.qwerty', 'false', '', '', 'true');
656656
window.foo = function name() { };
657657
window.foo.prototype = { bar: 1 };
658658
window.foo.prototype.abc = {
@@ -665,7 +665,7 @@ if (!isSupported) {
665665

666666
// https://github.com/AdguardTeam/Scriptlets/issues/330
667667
test('Override value 2 times - object', (assert) => {
668-
runScriptletFromTag('foo.prototype.abc.qwerty', 'false');
668+
runScriptletFromTag('foo.prototype.abc.qwerty', 'false', '', '', 'true');
669669
window.foo = function name() { };
670670
window.foo.prototype = { bar: 1 };
671671
window.foo.prototype.abc = {
@@ -699,7 +699,7 @@ if (!isSupported) {
699699

700700
// https://github.com/AdguardTeam/Scriptlets/issues/330
701701
test('Check overriden value - object + similar object which should not be overriden', (assert) => {
702-
runScriptletFromTag('foo.prototype.abc.qwerty', 'false');
702+
runScriptletFromTag('foo.prototype.abc.qwerty', 'false', '', '', 'true');
703703
window.foo = function name() { };
704704
window.foo.prototype = { bar: 1 };
705705
window.foo.prototype.abc = {
@@ -754,7 +754,7 @@ if (!isSupported) {
754754
});
755755

756756
test('Test for reassignment 1', (assert) => {
757-
runScriptletFromTag('zxcv.test.bar.qw', 'trueFunc');
757+
runScriptletFromTag('zxcv.test.bar.qw', 'trueFunc', '', '', 'true');
758758
const funcOne = () => 1;
759759
window.zxcv = {};
760760
// Reassign
@@ -772,7 +772,7 @@ if (!isSupported) {
772772
});
773773

774774
test('Test for reassignment 2', (assert) => {
775-
runScriptletFromTag('WO.adblock.useAdblocker', 'false');
775+
runScriptletFromTag('WO.adblock.useAdblocker', 'false', '', '', 'true');
776776

777777
window.WO = window.WO || {};
778778
window.WO.strings = window.WO.strings || {};
@@ -797,7 +797,7 @@ if (!isSupported) {
797797
});
798798

799799
test('Check if proxy was not set many times', (assert) => {
800-
runScriptletFromTag('proxy.test.abc', 'trueFunc');
800+
runScriptletFromTag('proxy.test.abc', 'trueFunc', '', '', 'true');
801801

802802
// Expected number of calls to getOwnPropertyDescriptor
803803
const EXPECTED_NUMBER = 4;

wiki/compatibility-table.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,6 @@
8484
| | | race |
8585
| | window.name-defuser.js | |
8686
| | spoof-css.js | |
87-
| | replace-node-text.js (rpnt.js) | |
8887
| | trusted-set-constant.js (trusted-set.js) | |
8988
| | trusted-set-cookie.js | |
9089
| | trusted-set-local-storage-item.js | |
@@ -100,6 +99,7 @@
10099
| | trusted-prune-inbound-object.js | |
101100
| | trusted-prune-outbound-object.js | |
102101
| | trusted-set-session-storage-item.js | |
102+
| | trusted-replace-node-text.js (trusted-rpnt.js, replace-node-text.js, rpnt.js) | |
103103

104104

105105
## <a id="redirects"></a> Redirects compatibility table

0 commit comments

Comments
 (0)