Scriptlets icon indicating copy to clipboard operation
Scriptlets copied to clipboard

Support generating a constructor in set-constant scriptlet

Open piquark6046 opened this issue 2 years ago • 2 comments

Generating a constructor in set-constant scriptlet means that a filters list maintainer can make a fake class with custom methods and properties.

For example, a filters list maintainer can use it as the following filters:

example.com#%#//scriptlet('set-constant', 'human', 'constructor')
example.com#%#//scriptlet('set-constant', 'human.prototype.sleep', 'noopFunc')
example.com#%#//scriptlet('set-constant', 'human.prototype.IsSleeping', 'false')
example.com#%#//scriptlet('set-constant', 'human.prototype.IsSleepingFunc', 'falseFunc')

The above filters can be converted to the following Typescript:

class human {
  constructor() {
    this.IsSleeping = false
  }

  sleep() {
    // noopFunc
  }

  IsSleepingFunc() {
    return false
  }
}

The generating a constructor in set-constant scriptlet can help to resolve a advertisement/tracking-related class in a normal <script> element or javascript file without allowing the advertisement/tracking-related another javascript file. (E.g., https://github.com/AdguardTeam/AdguardFilters/issues/161389)

piquark6046 avatar Oct 05 '23 14:10 piquark6046

@piquark6046 did you try noopFunc instead of construstor in your first rule?

slavaleleka avatar Jan 26 '24 14:01 slavaleleka

Regarding shoppinghow.kakao.com case, something like this:

window.AdfitNativeAds = function () {};
window.AdfitNativeAds.prototype.init = function () {};
window.AdfitNativeAds.prototype.renderAs = function () {
  return this;
};
window.AdfitNativeAds.prototype.error = function () {
  return this;
};

seems to fix issue with broken website when t1.daumcdn.net/kas/static/na.min.js is blocked.

So, if I'm not wrong, for this case, probably adding noopThis as set-constant value should fixes this problem. And then we could use something like:

shoppinghow.kakao.com#%#//scriptlet('set-constant', 'AdfitNativeAds', 'noopFunc')
shoppinghow.kakao.com#%#//scriptlet('set-constant', 'AdfitNativeAds.prototype.init', 'noopFunc')
shoppinghow.kakao.com#%#//scriptlet('set-constant', 'AdfitNativeAds.prototype.renderAs', 'noopThis')
shoppinghow.kakao.com#%#//scriptlet('set-constant', 'AdfitNativeAds.prototype.error', 'noopThis')

More info regarding shoppinghow.kakao.com case:

As far as I understand, website is doing something like (just very simplified example):

console.log('start');
new AdfitNativeAds({foo:1}).renderAs().error().init();
console.log('end');

and it breaks on new AdfitNativeAds...

AdamWr avatar Jan 26 '24 15:01 AdamWr