简体   繁体   English

使用Chrome Extension Manifest V3,如何执行代码字符串?

[英]Using Chrome Extension Manifest V3, How Do I Execute Code Strings?

I'm writing a Chrome extension using Manifest V3 and trying to find a way to execute code strings dynamically (similar to userscripts).我正在使用 Manifest V3 编写 Chrome 扩展程序,并试图找到一种动态执行代码字符串的方法(类似于用户脚本)。 There are methods to execute code strings (eval, setTimeout, new Function, etc.);有执行代码字符串的方法(eval、setTimeout、new Function等); however, they seem forbidden in Manifest V3.然而,它们在 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.虽然 setTimeout 仍然有效,但 Chrome 团队表示,当提交到 Chrome 网上应用店时,将不再接受执行代码字符串。 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/ 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.看到一些关于这个问题的论坛帖子和 crbugs 发布,但它们似乎都过时了,没有明确的答案。 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.较新但仍然过时(2020 年)的帖子指出,Manifest V3 将具有用例(例如 TamperMonkey),其中可以动态注册用户脚本,但只能在给用户提示之后。 The only two methods I see that let an extension run scripts dynamically are “chrome.scripting.registerContentScripts” and “chrome.scripting.executeScript”.我看到的唯一两种让扩展程序动态运行脚本的方法是“chrome.scripting.registerContentScripts”和“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.旧方法“chrome.tabs.executeScript”曾经允许执行代码字符串,但现在已弃用。

Is there a way that is allowed by the Chrome Web Store or any update to this issue? Chrome 网上应用店或对此问题的任何更新是否有允许的方法?

Edit:编辑:

If executing scripts dynamically from files is the only way:如果从文件动态执行脚本是唯一的方法:

  1. Do Chrome Extensions allow saving files in the extension directory? Chrome 扩展允许在扩展目录中保存文件吗? Or would it invalidate the extension (the extension is automatically uninstalled because of some security policy)?或者它会使扩展程序无效(由于某些安全策略,扩展程序会自动卸载)?
  2. Can the two current methods (mentioned above) execute files outside of the extension if it saves the files on the user's hard drive?如果将文件保存在用户的硬盘驱动器上,当前的两种方法(如上所述)是否可以执行扩展名之外的文件?

Thanks.谢谢。

In sandbox, code string can be executed.在沙箱中,可以执行代码字符串。

https://developer.chrome.com/docs/extensions/mv3/manifest/sandbox/ https://developer.chrome.com/docs/extensions/mv3/manifest/sandbox/

There is several restrictions to the sandbox code, for example, it cannot access to DOM.沙箱代码有几个限制,例如,它不能访问 DOM。 It depends on what you want to do by the code string.这取决于您想通过代码字符串做什么。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 Chrome 扩展清单 v3 中,我如何指示 manifest.json 仅在某些网页上运行我的扩展而不使用 activeTab 权限? - In Chrome extension Manifest v3, how do i instruct manifest.json to run my extension only on certain webpages without using activeTab permission? 如何从带有 Chrome Extension Manifest v3 的网页获取 DOM? - How do I get the DOM from a webpage with Chrome Extension Manifest v3? 如何在清单 v3 Chrome 扩展程序中定期调用 function? - How do I call a function periodically in a manifest v3 Chrome extension? 使用清单 v3 将谷歌分析添加到 chrome 扩展中 - Add google analytics into a chrome extension using manifest v3 如何在 Chrome 扩展 Manifest V3 中存储敏感数据 - How to store sentitive data in Chrome extension Manifest V3 您如何在 Chrome Extension Manifest V3 Background Service Worker 中每秒调用 function? - How do you call a function every second in a Chrome Extension Manifest V3 Background Service Worker? Chrome 扩展清单 v3 和内联脚本 - Chrome extension manifest v3 and inline scripts 在 Chrome 扩展中集成 GA,清单 v3 - Integrating GA in chrome extension, manifest v3 后台 Chrome 扩展清单 V3 库 - Chrome Extension Manifest V3 Libraries in Background 如何在 Chrome 扩展清单 v3 的选项卡中播放音频 stream - How can I get audio stream playing in a tab in chrome extension manifest v3
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM