EDIT dưới đây vẫn còn đúng cho vani đồng bằng yêu cầu .js, nhưng tìm thấy cách giải quyết bằng cách yêu cầu RequireJS
Github: https://github.com/jeroendelau/requirejs
Bower: Chòi chơi cho cài đặt requirejs-cho plug-in mở rộng
orignal POST
Tiêm nó thành kịch bản nội dung đến nay là khó khăn nhất. Và các câu trả lời ở trên không đầy đủ hoặc không chính xác.
Bối cảnh và Popup:
Go với câu trả lời trước đó của @nafis, họ sẽ làm việc
Content Script
Đây là một trong rất khôn lanh, và phần này từ api description là chìa khóa:
Execution environment
Content scripts execute in a special environment called an isolated world. They have access to the DOM of the page they are injected into, but not to any JavaScript variables or functions created by the page. It looks to each content script as if there is no other JavaScript executing on the page it is running on. The same is true in reverse: JavaScript running on the page cannot call any functions or access any variables defined by content scripts.
Cách trực quan này phải chính xác
manifest.json
{
"name": "My extension",
...
"content_scripts": [
{
"matches": ["http://www.google.com/*"],
"css": ["mystyles.css"],
"js": ["requirejs.js", "myscript.js"]
}
],
...
"web_accessible_resources": [
"js/*"
],
...
}
myscript.js
require(["myFancyModule"], function (FM) {
...
});
này sẽ không hoạt
Vấn đề là requirejs sẽ tiến hành tải tất cả phụ thuộc của bạn bằng cách tiêm <script>
thẻ trong tiêu đề. Các thẻ tập lệnh này được thực hiện trong môi trường PAGE, không phải trong môi trường EXTENSION đặc biệt. Và điều này quan trọng.
- Dependencies không tải từ requirejs không được tải trong trang môi trường
- Nếu chủ sở hữu trang web đã alreay thêm requirejs, nó migh xung đột
- Bạn có thể quyết định tiêm require.js vào trang, như @Adam gợi ý, nhưng trong trường hợp đó, không có tính năng tiện ích mở rộng nào của bạn sẽ hoạt động.lưu trữ, nhắn tin, yêu cầu qua nhiều trang web không phải là có sẵn
Vì vậy, để những người làm việc, các mô-đun được tải bởi requirejs cần phải được đưa vào môi trường tiện ích mở rộng. Có thể sử dụng plugin requirejs để thay đổi hành vi tải.
Do cách làm việc này là giải pháp rất thanh nha, VÀ nó ngăn cản bạn từ khi nhìn thấy các script trong trình gỡ lỗi dưới kịch bản. Nhưng nếu bạn tuyệt vọng, nó sẽ hoạt động.
myscript.js
/**
* Inject the plugin straight into requirejs
*/
define("Injector", {
load: function (name, req, onload, config) {
//Load the script using XHR, from background
var oReq = new XMLHttpRequest();
oReq.addEventListener("load", function() {
//Find depenencies in the script, and prepend the
//Injector! plugin, forcing the load to go through this
//plugin.
var modified = getDeps(oReq.response)
//have requirejs load the module from text
//it will evaluate the define, and process dependencies
onload.fromText(modified);
});
oReq.open("GET", req.toUrl(name) + ".js");
oReq.send();
//Find dependencies and prepend Injector!
function getDeps(script)
{
//extract the define call, reduced to a single line
var defineCall = script.match(/define([\s\S])*?{/m)[0].split("\n").join("");
//extract dependenceis from the call
var depsMatch = defineCall.match(/\[([\s\S]*?)\]/);
//if there are dependencies, inject the injectors
if (depsMatch)
{
var deps = depsMatch[0];
var replaced = deps.replace(/(\'|\")([\s\S]*?)\1/g, '$1Injector!$2$1');
return script.replace(/define([\s\S]*?)\[[\s\S]*?\]/m, 'define$1' + replaced);
}
//no dependencies, return script
return script;
}
}
});
/**
* Call all your dependencies using the plugin
*/
require(["Injector!myFancyModule"], function (FM) {
chrome.storage.local.get({something});
});
Ông có thể làm rõ rằng hơn một chút? Cách bạn thường bao gồm các mô-đun với require.js sẽ thông qua javascript, không phải là thẻ tập lệnh: yêu cầu (["some/module", "a.js", "b.js"]); – Julian
Tôi biết cách bao gồm các mô-đun, nhưng làm cách nào để tự bao gồm requireJS * sau khi tải trang và chỉ sử dụng JavaScript? – Randomblue
Đây không phải là một câu trả lời thực sự cho câu hỏi của bạn, nhưng bạn có thể sử dụng chỉ cần tạo một kịch bản Greasemonkey và thực hiện tất cả các mã javascript của bạn [http://greasemonkey.mozdev.org/authoring.html](http://greasemonkey.mozdev .org/authoring.html) – tyranitar