Skip to content

Commit

Permalink
AG-27573 Improve set-constant - add additional argument to set proxy …
Browse files Browse the repository at this point in the history
…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
  • Loading branch information
AdamWr committed Nov 15, 2023
1 parent b50f9c9 commit 2f91f88
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 16 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
<!-- TODO: add @added tag to the files with specific version -->
<!-- during new scriptlets or redirects releasing -->

## [Unreleased]

### Added

- ability to set proxy trap in `set-constant` scriptlet [#330](https://github.com/AdguardTeam/Scriptlets/issues/330)

## [v1.9.91] - 2023-11-13

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

[Unreleased]: https://github.com/AdguardTeam/Scriptlets/compare/v1.9.91...HEAD
[v1.9.91]: https://github.com/AdguardTeam/Scriptlets/compare/v1.9.83...v1.9.91
[v1.9.83]: https://github.com/AdguardTeam/Scriptlets/compare/v1.9.72...v1.9.83
[v1.9.72]: https://github.com/AdguardTeam/Scriptlets/compare/v1.9.70...v1.9.72
Expand Down
8 changes: 4 additions & 4 deletions scripts/compatibility-table.json
Original file line number Diff line number Diff line change
Expand Up @@ -269,9 +269,6 @@
{
"ubo": "spoof-css.js"
},
{
"ubo": "replace-node-text.js (rpnt.js)"
},
{
"ubo": "trusted-set-constant.js (trusted-set.js)"
},
Expand Down Expand Up @@ -316,6 +313,9 @@
},
{
"ubo": "trusted-set-session-storage-item.js"
},
{
"ubo": "trusted-replace-node-text.js (trusted-rpnt.js, replace-node-text.js, rpnt.js)"
}
],
"redirects": [
Expand Down Expand Up @@ -524,4 +524,4 @@
"ubo": "noop-0.5s.mp3"
}
]
}
}
16 changes: 13 additions & 3 deletions src/scriptlets/set-constant.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ import {
* ### Syntax
*
* ```text
* example.org#%#//scriptlet('set-constant', property, value[, stack])
* example.org#%#//scriptlet('set-constant', property, value[, stack,[ valueWrapper[, setProxyTrap]]])
* ```
*
* - `property` — required, path to a property (joined with `.` if needed). The property must be attached to `window`.
Expand Down Expand Up @@ -74,6 +74,7 @@ import {
* - `asCallback` – function returning callback, that would return value
* - `asResolved` – Promise that would resolve with value
* - `asRejected` – Promise that would reject with value
* - `setProxyTrap` – optional, boolean, if set to true, proxy trap will be set on the object
*
* ### Examples
*
Expand Down Expand Up @@ -113,10 +114,19 @@ import {
* ✔ document.fifth.catch((reason) => reason === 42) // promise rejects with specified number
* ```
*
* ```adblock
* ! Any access to `window.foo.bar` will return `false` and the proxy trap will be set on the `foo` object
* ! It may be required in the case when `foo` object is overwritten by website script
* ! Related to this issue - https://github.com/AdguardTeam/Scriptlets/issues/330
* example.org#%#//scriptlet('set-constant', 'foo.bar', 'false', '', '', 'true')
*
* ✔ window.foo.bar === false
* ```
*
* @added v1.0.4.
*/
/* eslint-enable max-len */
export function setConstant(source, property, value, stack = '', valueWrapper = '') {
export function setConstant(source, property, value, stack = '', valueWrapper = '', setProxyTrap = false) {
const uboAliases = [
'set-constant.js',
'ubo-set-constant.js',
Expand Down Expand Up @@ -299,7 +309,7 @@ export function setConstant(source, property, value, stack = '', valueWrapper =
// Get properties which should be checked and remove first one
// because it's current object
const propertiesToCheck = property.split('.').slice(1);
if (!isProxyTrapSet) {
if (setProxyTrap && !isProxyTrapSet) {
isProxyTrapSet = true;
a = new Proxy(a, {
get: (target, propertyKey, val) => {
Expand Down
16 changes: 8 additions & 8 deletions tests/scriptlets/set-constant.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -486,7 +486,7 @@ if (!isSupported) {

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

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

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

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

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

test('Test for reassignment 1', (assert) => {
runScriptletFromTag('zxcv.test.bar.qw', 'trueFunc');
runScriptletFromTag('zxcv.test.bar.qw', 'trueFunc', '', '', 'true');
const funcOne = () => 1;
window.zxcv = {};
// Reassign
Expand All @@ -772,7 +772,7 @@ if (!isSupported) {
});

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

window.WO = window.WO || {};
window.WO.strings = window.WO.strings || {};
Expand All @@ -797,7 +797,7 @@ if (!isSupported) {
});

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

// Expected number of calls to getOwnPropertyDescriptor
const EXPECTED_NUMBER = 4;
Expand Down
2 changes: 1 addition & 1 deletion wiki/compatibility-table.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,6 @@
| | | race |
| | window.name-defuser.js | |
| | spoof-css.js | |
| | replace-node-text.js (rpnt.js) | |
| | trusted-set-constant.js (trusted-set.js) | |
| | trusted-set-cookie.js | |
| | trusted-set-local-storage-item.js | |
Expand All @@ -100,6 +99,7 @@
| | trusted-prune-inbound-object.js | |
| | trusted-prune-outbound-object.js | |
| | trusted-set-session-storage-item.js | |
| | trusted-replace-node-text.js (trusted-rpnt.js, replace-node-text.js, rpnt.js) | |


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

0 comments on commit 2f91f88

Please sign in to comment.