Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve "set-constant" scriptlet #65

Closed
AdamWr opened this issue Dec 31, 2019 · 10 comments
Closed

Improve "set-constant" scriptlet #65

AdamWr opened this issue Dec 31, 2019 · 10 comments

Comments

@AdamWr
Copy link
Member

AdamWr commented Dec 31, 2019

For example, if there is a script like this:

var test = {
    number: 4,
    boolean: true
}

and we want to set test.number to 0 and test.boolean to false using scriptlets like:
#%#//scriptlet("set-constant", "test.number", "0")
#%#//scriptlet("set-constant", "test.boolean", "false")
then only one of these scriptlets work actually.

Expected behavior - both scriptlets should work.

Test page - https://adamwr.github.io/test-page/index.html
Rules:
adamwr.github.io#%#//scriptlet("set-constant", "test.number", "0")
adamwr.github.io#%#//scriptlet("set-constant", "test.boolean", "false")

Screenshot

image

@ameshkov ameshkov added the enhancement Improvement of existent feature label Jan 6, 2020
@ameshkov ameshkov added this to the 1.3 milestone Jan 6, 2020
@adguard-bot adguard-bot modified the milestones: 1.3, 1.4, 1.5, 1.2 Feb 5, 2020
@ameshkov ameshkov modified the milestones: 1.2, 1.3 Mar 3, 2020
@oleedd
Copy link

oleedd commented Jun 22, 2020

It is because this scriplet uses getters and setters (in Object.defineProperty) instead of just value here:

image
It is generally unreasonable. It modifies objects a lot.

I have this result:
image

@ameshkov ameshkov modified the milestones: 1.3, 2.0 Jul 21, 2020
@ameshkov
Copy link
Member

  1. Using just value is not possible because, at the moment when the scriptlet is executed, there may be no test object at all. We need to detect when it's created and only then we can define the property.

  2. Solving this requires having a "context" object that can be used by scriptlets to "communicate". I suppose it'd be better to do this after the "new approach" task: New approach to scriptlets #83

@oleedd
Copy link

oleedd commented Jul 21, 2020

Using get/set and value doesn't differ in this. You still need to detect creating of object even with get/set.

@slavaleleka
Copy link
Contributor

gorhill/uBlock@c33de41 👀

@ameshkov
Copy link
Member

A brief comment on this: set-constant now creates configurable property, and in this case, it is possible to "chain" property calls. This does solve the immediate issue but makes it possible for page scripts to override this property on its own. We should keep an eye on what websites do with this.

@AdamWr
Copy link
Member Author

AdamWr commented Sep 5, 2020

I'm not sure if it's related to this problem, but it seems that using delete operator causes that set-constant and abort-on-property-read/write scriptlets with chained property do not work.

Example code:

var test = {};
delete window.test;
var test = {
  number: 999,
  boolean: true
};

Test page - https://adamwr.github.io/test-page/delete.html
Rule:
adamwr.github.io#%#//scriptlet("set-constant", "test.number", "0")
or
adamwr.github.io#%#//scriptlet("abort-on-property-write", "test.number")

There is a console.log function which prints test.number, so for set-constant scriptlet it should shows 0 in dev console, and for abort-on-property-write it should shows undefined, but it seems that scriptlets do not work.

@ameshkov
Copy link
Member

ameshkov commented Dec 6, 2021

It bothers me that this task takes that much time to solve, there are already quite a lot of places in the filters where we need this behavior.

If it does not fit into the current set-constant implementation, we might want to consider adding a different scriptlet or adding a new parameter to set-constant that will change its logic.

@slavaleleka @maximtop guys, what do you think about that?

@maximtop
Copy link
Contributor

maximtop commented Dec 6, 2021

Values can be used instead of setters and getters if we do not need any checks when someone tries to change this "constant".

@contribucious
Copy link
Contributor

From @ameshkov: (19 Aug 2020)

A brief comment on this: set-constant now creates configurable property, and in this case, it is possible to "chain" property calls. This does solve the immediate issue but makes it possible for page scripts to override this property on its own. We should keep an eye on what websites do with this.

↪️ The bold part is a no-no for me. Too risky and problematic over time, even for the follow-up of its own commits over time … Especially for large websites that can easily afford to quickly counter. 😕

@contribucious
Copy link
Contributor

contribucious commented Dec 18, 2021

For info/context, it would have been useful to me recently: ✌️
see AdguardTeam/AdguardFilters#102134 > part Bonus n°1 — Websites from the TF1 galaxy ….

Read more (updated)  

In this case, to have:

tf1.fr#%#//scriptlet('set-constant', 'tv.freewheel.SDK.Util.pingURLWithForm', 'trueFunc')
tf1.fr#%#//scriptlet('set-constant', 'tv.freewheel.SDK.Util.pingURLWithImage', 'trueFunc')
tf1.fr#%#//scriptlet('set-constant', 'tv.freewheel.SDK.Util.pingURLWithScript', 'trueFunc')
tf1.fr#%#//scriptlet('set-constant', 'tv.freewheel.SDK.Util.pingURLWithXMLHTTPRequest', 'trueFunc')
tf1.fr#%#//scriptlet('set-constant', 'tv.freewheel.SDK.Util.sendAdRequestWithXMLHTTPRequest', 'trueFunc')

instead of:

! TODO: Change AG_defineProperty to scriptlet when this issue will be fixed - https://github.com/AdguardTeam/Scriptlets/issues/65
tf1.fr#%#AG_defineProperty('tv.freewheel.SDK.Util.pingURLWithForm', { get: function() { return function() {return true;}; } });
tf1.fr#%#AG_defineProperty('tv.freewheel.SDK.Util.pingURLWithImage', { get: function() { return function() {return true;}; } });
tf1.fr#%#AG_defineProperty('tv.freewheel.SDK.Util.pingURLWithScript', { get: function() { return function() {return true;}; } });
tf1.fr#%#AG_defineProperty('tv.freewheel.SDK.Util.pingURLWithXMLHTTPRequest', { get: function() { return function() {return true;}; } });
tf1.fr#%#AG_defineProperty('tv.freewheel.SDK.Util.sendAdRequestWithXMLHTTPRequest', { get: function() { return function() {return true;}; } });

 

ℹ️ Also, I'm facing a similar situation for one of my future PRs.  |  EDIT: this one. ☺️

@slavaleleka slavaleleka removed this from the 2.0 milestone Jan 12, 2022
adguard pushed a commit that referenced this issue Mar 15, 2022
Merge in ADGUARD-FILTERS/scriptlets from fix/AG-149 to release/v1.6

Squashed commit of the following:

commit ff551dc
Merge: fe099c9 9d4d229
Author: Stanislav A <s.atroschenko@adguard.com>
Date:   Tue Mar 15 15:50:54 2022 +0300

    Merge branch 'release/v1.6' into fix/AG-149

commit fe099c9
Author: Stanislav A <s.atroschenko@adguard.com>
Date:   Tue Mar 15 12:48:24 2022 +0300

    rename test function

commit da26b67
Author: Stanislav A <s.atroschenko@adguard.com>
Date:   Mon Mar 14 15:19:36 2022 +0300

    add configurable & new tests

commit 4dd1bfd
Author: Stanislav A <s.atroschenko@adguard.com>
Date:   Fri Mar 11 11:44:43 2022 +0300

    add comments & fix typo

commit 6529828
Author: Stanislav A <s.atroschenko@adguard.com>
Date:   Tue Mar 8 21:07:08 2022 +0300

    improve set-constant scriptlet

commit eb73fa1
Author: Stanislav A <s.atroschenko@adguard.com>
Date:   Sun Feb 20 22:40:57 2022 +0300

    improve set-cosntant scriptlet
adguard pushed a commit that referenced this issue Mar 24, 2022
* commit 'c06a8e539488e1ebfcf72c85a39a0ef5b9c028bb': (32 commits)
  AG-13382 update docs, validate noopjson
  push updated dist to github while release build. AG-13356
  remove tagcommander redirect AG-11959
  add readme info about testing ad debugging AG-12716
  add artifacts to scriptlets builds AG-12487
  improve set-constant scriptlet #65 AG-149
  improve prevent-xhr scriptlet #199 AG-13007
  improve googletagservices-gpt redirect #193 AG-12767
  add wcslog redirect #94 AG-3906
  improve ati-smarttag redirect #200 AG-13141
  improve json-prune scriptlet #171 AG-11879
  fix yandex metrika on avito #198 AG-12973
  improve metrika-yandex-tag redirect #189 AG-12550
  add prebid-ads redirect #190 AG-12558
  remove no-floc scriptlet AG-12869
  add noopjson redirect. AG-12796 #195
  Revert "revert tests and fix converter again"
  revert tests and fix converter again
  add conversion test for wildcard tld AG-12636
  add prevent-element-src-loading scriptlet #180 AG-12328
  ...
adguard pushed a commit that referenced this issue Apr 15, 2022
…2 to master

* commit '84180cec7974246d60fea77c7cb8029dd4f12ac8':
  Update build after merge
  Fix test 2
  Fix test
  Update readme
  fix lint
  Add tests
  Redirect: metrika-yandex-tag
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

8 participants