Chrome 扩展中的 RequireJS
我正在构建一个 Chrome 扩展,它将一些 JavaScript 添加到维基百科文章中。据我所知,使用 RequireJS 的唯一方法是添加行
<script data-main="scripts/bla" src="scripts/require-jquery.js>
但是,在我的 Chrome 扩展中,我无权访问 HTML 来添加此行。有什么建议吗?
I am building a Chrome extension which adds some JavaScript to Wikipedia articles. As far as I know, the only way to use RequireJS is to add the line
<script data-main="scripts/bla" src="scripts/require-jquery.js>
However, in my Chrome extension, I don't have access to the HTML to add this line. Any suggestions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您确实可以通过内容脚本,但是内容脚本只能访问由内容脚本 本身。
有多种方法可以包含来自 Chrome 扩展程序的脚本,如何包含它取决于您计划使用它做什么。
如果您希望将其显示在浏览器或页面操作 您可以将其从清单中作为内容脚本包含进来,也可以使用脚本标记引用它来自亲戚的 popup.html插件中的资源。
来自清单:
来自 popup.html:
如果您希望将其显示在后台页面中,您可以可以使用插件中相关资源的脚本标签从后台页面引用它。
来自background.html
如果您希望将其包含在浏览器页面本身中,那么您需要在页面的DOM 上使用动态脚本注入。您可以通过内容脚本访问页面的 DOM。请注意,如果您使用此技术加载 JavaScript,您的插件 JavaScript(从后台页面、内容脚本或弹出脚本)将无法访问它。
您可以使用 chrome.extension.getURL 方法从扩展程序加载 requirejs,也可以从互联网上的托管位置加载 requirejs。
You do have access to the DOM of the page from the Chrome Extension via a content script, however the content script will only have access to the JavaScript objects created by the content script itself.
There are many ways to include scripts from a Chrome extension, how you include it will be based on what you plan to do with it.
If you want it in the popup page of a browser or page action you can either include it from the manifest as a content script or reference it using a script tag in the popup.html from a relative resource in your plugin.
From manifest:
From popup.html:
If you want it in the background page you can reference it from the background page using a script tag from a relative resource in your plugin.
From background.html
If you want it to be included in the browser page itself then you need to use dynamic script injection on the page's DOM. You have access to the page's DOM from a content script. Please note that if you load the JavaScript using this technique you plugin JavaScript (from a background page, content script or popup script) will not have access to it.
You can either load the requirejs from your extension using the
chrome.extension.getURL
method or from the a hosted location on the internet.以下是在后台页面中执行此操作的方法。
在
manifest.json
中:在
main.js
中:而不是这个:
您应该使用这个:
请参阅这个问题 - 在没有 data-main 的情况下使用 Require.js
您也可以使用此原理在内容页面中执行相同的操作。
顺便说一句,好问题。我喜欢亚当·艾尔斯的答案。
Here is how you can do it in the background page.
In
manifest.json
:In
main.js
:Instead of this:
You should use this:
See this question - Using Require.js without data-main
You can also do the same thing in the content page using this principle.
Nice Question by the way. I liked Adam Ayres's answers.
下面的编辑对于普通的 require.js 仍然适用,但通过分叉 RequireJS
Github 找到了解决方法:https://github。 com/jeroendelau/requirejs
Bower:bower install requirejs-for-browser-extensions
原始帖子
将其注入到内容脚本中是迄今为止最难的。并且上述答案不完整或不正确。
背景和弹出窗口:
遵循 @nafis 之前的回答,他们将使用
内容脚本
这是一个非常棘手的问题,api 描述中的这一部分是关键:
直觉上这应该是正确的
manifest.json
myscript.js
这不起作用
问题是 requirejs 将通过在标头中注入
标签来加载所有依赖项。这些脚本标签在 PAGE 环境中执行,而不是在特殊的 EXTENSION 环境中执行。这很重要。
没有
会起作用的。存储、消息传递、跨站请求都不是
因此
,为了使它们能够工作,需要将 requirejs 加载的模块注入到扩展环境中。可以使用 requirejs 插件来更改加载行为。
由于这种工作方式,该解决方案非常不优雅,并且它阻止您在脚本下的调试器中看到脚本。但如果你绝望的话,它会起作用。
myscript.js
EDIT below is still true for plain vanilla require.js, but found a workaround by forking RequireJS
Github: https://github.com/jeroendelau/requirejs
Bower: bower install requirejs-for-browser-extensions
ORIGNAL POST
Injecting it into the content script is by far the hardest. And the above answers are incomplete or incorrect.
Background and Popup:
Go with earlier answer by @nafis, they will work
Content Script
This is a very tricky one, and this part from the api description is key:
Intuitively this should be correct
manifest.json
myscript.js
THIS WILL NOT WORK
The problem is that requirejs will proceed to load all your dependencies by injecting
<script>
tags in the header. These script tags are executed in the PAGE environment, not in the special EXTENSION environment. And this matters.environment
will work. storage, messaging, cross-site request are all not
available
So in order for those to work, the modules loaded by requirejs need to be injected into the extensions environment. It is possible to use a requirejs plugin to change load behavior.
Because of the way this works the solution is very inelegant, AND it prevents your from seeing the scripts in the debugger under scripts. But if you're desperate, it will works.
myscript.js
要在内容脚本中使用它,您只需将其作为您的第一个依赖项包含在manifest.json中即可:
它不会污染全局命名空间(窗口)所以不用担心;它仅适用于扩展脚本。
另外,在manifest.json中,您必须通过将它们命名为“web_accessible_resources”来命名requirejs能够使用的文件,以便轻松地将它们全部放在一个文件夹中(例如js/),以便您可以使用通配符:
然后在您的脚本(例如 myscript.js)中使用 requirejs 只需使用 chrome.extension.getURL 请求依赖项,如下所示:
To use it in a content script you can just include it in the manifest.json as your first dependency:
It will NOT pollute the global namespace (window) so no worries about that; its only available for extension scripts.
Also in manifest.json you have to name the files that requirejs will be able to use by naming them as "web_accessible_resources", to make this easy put them all in a folder (e.g. js/) so you can use a wildcard:
And then in your script (e.g. myscript.js) to use requirejs just ask for the dependencies using
chrome.extension.getURL
, like this: