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

Comments

Projects
None yet
4 participants
@ameshkov
Copy link
Member

commented May 28, 2018

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;

@ameshkov ameshkov added this to the 2.10 milestone May 28, 2018

@maximtop

This comment has been minimized.

Copy link
Contributor

commented Jul 16, 2018

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

This comment has been minimized.

Copy link
Member

commented Jul 16, 2018

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 Aug 13, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.