I’m writing a Chrome extension using Manifest V3 and trying to find a way to execute code strings dynamically (similar to userscripts). There are methods to execute code strings (eval, setTimeout, new Function, etc.); however, they seem forbidden in Manifest V3. While setTimeout still works, the Chrome team states that executing code strings will no longer be accepted when submitted to the Chrome Web Store. The code needs to be part of the extension when submitted. Also stated in the link below under “Remotely Hosted Code”:
https://developer.chrome.com/docs/extensions/mv3/intro/mv3-migration/
Saw some forum posts and crbugs posted about this issue, but they all seem outdated without a definitive answer. Like the link above, older posts state that it will no longer be possible to execute code strings. Newer but still outdated (2020) posts state that Manifest V3 will have use cases (such as TamperMonkey) where userscripts can be registered dynamically but only after giving the user a prompt. The only two methods I see that let an extension run scripts dynamically are “chrome.scripting.registerContentScripts” and “chrome.scripting.executeScript”. Both methods execute code from files, not strings. The older method “chrome.tabs.executeScript” used to allow executing code strings but is now deprecated.
Is there a way that is allowed by the Chrome Web Store or any update to this issue?
Edit 1:
If executing scripts dynamically from files is the only way:
Edit 2:
Reworded 2 (under Edit 1).
Edit 3:
Thanks to all that have replied. Doesn’t seem like there is a solution to this issue, yet. There have been no public announcements that I have heard or read about as to how (if it’s possible) script code strings can be dynamically executed (that would not be rejected by the Chrome Web Store).
As of November 13, 2021, you can do one of the following:
Thanks everyone.
In sandbox, code string can be executed.
https://developer.chrome.com/docs/extensions/mv3/manifest/sandbox/
There is several restrictions to the sandbox code, for example, it cannot access to DOM. It depends on what you want to do by the code string.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With