-
Notifications
You must be signed in to change notification settings - Fork 56
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
Proposal: provide a way to register a "page script" #85
Comments
Yes, the ability to interact with the main world script is important. For example:
|
This is already planned in Chrome's MV3, https://crbug.com/1054624. |
@tophf dynamic content scripts while being an awesome addition, are a completely different thing. I am talking about scripts that are executed in the context of a page. |
As you can see in the linked bug comments, the ability to inject scripts in the main world of the page will be a part of the API. |
Oh, this is awesome, thank you! |
Moreover, I see that the |
I can confirm both isolated and main worlds work as expected, as well as arguments passed along, and returned values. The only weird thing during my tests is that explicitly throwing within the callback doesn't reject, but it early returns ... not sure if it's meant or a bug, but I wouldn't mind returning errors and check these, although I am not sure these survive the structured serialization once returned. |
executeScript returns the last evaluated expression, it doesn't care if it's early or not and executeScript doesn't use the Structured Clone algo, it uses JSON serialization just like it did in MV2 so only the few basic types are supported. |
@tophf thank you for the extra clarification. I have noticed that |
Someone should probably open a new issue here to suggest that extensions must use the structured clone algo both for executeScript and extension messaging. Firefox already uses this algo for the latter, IDK about executeScript. I also think that executeScript should automatically resolve Promise inside the injected code. |
Will there be a way to register the "main-world" scripts in In manifest v2, inline Example of const script = document.createElement("script");
script.textContent = "window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ = function(){ /* blah */ }";
(document.head || document.documentElement).append(script); |
@apple502j, MV3 restriction on inline scripts can be trivially circumvented via |
but that doesn't bypass the CSP |
For any lost souls out there (like myself) looking for a solution, early main-world script injection is supported in Chrome since 102.0.5005.61 Stable per https://chromiumdash.appspot.com/commit/e5ad3451c17b21341b0b9019b074801c44c92c9f and https://bugs.chromium.org/p/chromium/issues/detail?id=1207006#c9 |
does main-world script injection still get hit by csp? |
Yes, it does. |
The MAIN world script can run in pages with CSP that forbids inline js, so theoretically it can be said the CSP of the page was circumvented to allow running this code. However, the script can't use eval and create dynamic functions if those are forbidden by page CSP. The DOM things created by such script like |
Firefox has not implemented this yet, but it's tracked at https://bugzilla.mozilla.org/show_bug.cgi?id=1736575 |
The root problem is that since MV3 forbids
eval
, there's no way to inject a page script that would work before the page scripts.Check out the examples below:
MV2 example
content_script.js
:MV3 example
script.js
:content_script.js
:Setting
scriptTag.async = true
should probably solve the scripts execution order, but what if we need the page script to be parameterized? We'll have to establish a communication channel between the content script and the page script which will be asynchronous and inevitably postpone the execution.Proposal
It would help a lot if we had an API similar to
chrome.scripting.executeScript
, but for executing scripts in the page's context and available to content scripts. This kind of API can also be a solution to other issues mentioned in this repo (see #77 #78), the injected script's context may be enriched with a "communication" object.Use cases
Content blockers like AdGuard, uBO, and ABP have an option to run the so-called "scriptlets" (see an example here). The timing is crucial for these scriptlets.
Let's take an example:
abort-on-property-read
: aborts a script when it attempts to read the specified property.[many domains...]#%#//scriptlet("abort-on-property-read", "BetterJsPop")
. Example website:gledajcrtace.xyz
. This rule blocks a very popular pop-up ad script. And as I've said, the timing is crucial for this scriptlet to work properly, otherwise we may not be able to define the property before it's initialized by the page.The current workaround solution for MV3:
The proposed solution:
The text was updated successfully, but these errors were encountered: