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

Find a way to inject scripts faster than we do it at the moment #1029

Closed
ameshkov opened this issue May 28, 2018 · 2 comments
Closed

Find a way to inject scripts faster than we do it at the moment #1029

ameshkov opened this issue May 28, 2018 · 2 comments
Assignees
Milestone

Comments

@ameshkov
Copy link
Member

We need to find a way to improve the current situation and reliably inject our scripts BEFORE the in-page scripts are executed.

There's no 100% reliable way (relevant feature request: https://bugs.chromium.org/p/chromium/issues/detail?id=471801), but we can try some hacks in order to achieve a better result than we have at the moment.

First of all, try figure what Tampermonkey does (as I recall they're using onCommitted just like we do, but I may be wrong).

Here's what I suggest:

  1. Split the injection function into a couple of parts;
  2. Use the async onBeforeRequest listener to prepare scripts to be injected (and save it to some temp cache with -- use tabId and frameId pair as a cache key);
  3. Use both onResponseStarted (unreliable but fast) and onCommitted (reliable but not fast enough) listeners to inject scripts;
  4. Use onCompleted and onErrorOccurred to clean up the cache;
  5. Handle the possibility of duplicate injection in the script;
  6. CSS must be injected in the onCommitted handler only;
@maximtop
Copy link
Contributor

This feature is not reliable, but for some sites, it should inject scripts faster than inline scripts of the site itself.
In order to check this issue, you should

  1. add user rule maximtop.github.io#%#console.log('hi')
  2. open site https://maximtop.github.io/test-script-order/
  3. open developer tools
  4. word hi should fire early than other logs

Also in 2.10 JavaScript rules correctly work for iframes in Firefox

@Alex-302
Copy link
Member

The real example, when it is required:
https://kurs.com.ru/
add the rule
kurs.com.ru#%#AG_abortInlineScript(/!function/, 'String.fromCharCode');
Expected result - ad script, located in <head>, is aborted:
image
Add .js|$third-party,domain=kurs.com.ru,important to see ads(dev tools must be closed)
Screenshot:

image

@zebrum zebrum closed this as completed Aug 13, 2018
adguard pushed a commit that referenced this issue Aug 9, 2021
…odule

Merge in EXTENSIONS/browser-extension from fix/AG-9455 to feature/AG-2737

Squashed commit of the following:

commit 2d72a67
Author: Vladimir Zhelvis <[email protected]>
Date:   Mon Aug 9 18:52:40 2021 +0300

    fix error message

commit d166200
Merge: 7085785 71f9d66
Author: Vladimir Zhelvis <[email protected]>
Date:   Mon Aug 9 17:33:54 2021 +0300

    Merge branch 'feature/AG-2737' into fix/AG-9455

commit 7085785
Author: Vladimir Zhelvis <[email protected]>
Date:   Mon Aug 9 17:32:52 2021 +0300

    add tests

commit e123448
Author: Vladimir Zhelvis <[email protected]>
Date:   Mon Aug 9 12:13:36 2021 +0300

    handle request network error
gorhill added a commit to gorhill/uBlock that referenced this issue Nov 6, 2022
Related issue:
- uBlockOrigin/uBlock-issues#2350

As per AdGuard findings, it's possible (though unreliable) to try
to inject scriptlets at webRequest.onResponseStarted time, which
increases scriptlet injection reliability overall when injecting
from multiple entry points.

uBO was already injecting at webNavigation.onCommitted and
main content script time, and adding webRequest.onResponseStarted
as an entry point for scriptlet injection increases reliability
for webpages which executes inline scripts at the top of the DOM.

References:
- AdguardTeam/AdguardBrowserExtension#1029
- https://github.com/AdguardTeam/AdguardBrowserExtension/blob/9ab85be5/Extension/src/background/webrequest.js#L620
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

4 participants