Skip to content

Improve scripts injection flow and logic #334

@ACTCD

Description

@ACTCD

When we complete the refactoring of #331, we could try to further optimize the script injection flow and speed up the scripts injection.

In content.js, we will accomplish scripts injection with just two browser.storage.local.get asynchronous operations:

  • Get extension settings information
    • Extension active switch
    • Scripts active switch
    • Global exclude-match list
    • match/include/exclude...
  • Get the matched scripts code
    • Prepare wrappers for each script
    • Add the corresponding GM APIs in the wrapper
    • Add script code in wrapper
    • Immediately inject scripts

This will do the fastest document-start scripts injection in content scripts, but cannot guarantee full priority over page scripts as there are still asynchronous operations.

In background.js, which is responsible for updating the relevant information in browser.storage.local, we can check and update the scripts corresponding to the url request as early as possible, we could use the following process:

  • Use webNavigation.onBeforeNavigate to get the request url
  • Get cached scripts list and file modification date via browser.storage.local.get
  • Get the current files list and file modification date through the native layer, and get new or modified scripts code
  • Prioritize updating scripts code matching url to browser.storage.local and related indexes, and then update other scripts.

Since the above process is optimized for injection speed, the background process may be completed later than the content scripts injection, which may cause the latest scripts code to not be applied when the page refresh/load of first time after the scripts is updated, but this should be acceptable, we can also provide an option switch to let the user decide whether to inject first or version first(For example, it is convenient for user script developers to debug).

The above is just an idea, not tested and finalized, it may be rejected or not implemented for practical reasons.

Metadata

Metadata

Assignees

Labels

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions