rob*_*más 11 jquery cdn google-chrome-extension
我希望jQuery可以在内容脚本和控制台中访问(即,从网页 - 我相信,但我不确定,这就是你使用的原因web_accessible_resources).
注意:我同意下面的Zig Mandel,他说你不应该使用CDN来加载jquery,因为它只能节省少量空间并留下CDN可能关闭的可能性.在这一点上,我只是想知道为什么这不起作用.
为什么这不起作用:
"content_scripts": [
{
...
"js": ["foo.js", "https://code.jquery.com/jquery-1.10.1.min.js", "https://code.jquery.com/jquery-1.10.1.min.map"],
"run_at": "document_idle",
"all_frames": true
}
],
"content_security_policy": "script-src 'self' https://code.jquery.com; object-src 'self'",
"web_accessible_resources": [ "https://code.jquery.com/jquery-1.10.1.min.js", "https://code.jquery.com/jquery-1.10.1.min.map"],
Run Code Online (Sandbox Code Playgroud)
我加载扩展程序时收到的错误是:
--------------------------- Extension error
--------------------------- Could not load extension from 'C:\Users\[me]\Documents\GitHub\foo'. Could not load
javascript '' for content script.
--------------------------- OK
---------------------------
Run Code Online (Sandbox Code Playgroud)
当我需要在jQuery的(或一些自定义调试库等)web_accessible_resources与中时content_scripts?
你必须在jQuery中包含一个javascript文件web_accessible_resources,然后注入它.包含jQuery content_scripts仅供扩展中的其他内容脚本使用.如何注入代码(无论是否为本地)的示例:
function inject(script) {
if (script.match(/^http\:\/\//)){
var ssrc = document.createElement("script");
ssrc.setAttribute("src", script);
ssrc.addEventListener('load', function() {
var ssrc = document.createElement("script");
ssrc.textContent = "(" + callback.toString() + ")();";
document.body.appendChild(script);
}, false);
document.body.appendChild(script);
}
else {
var s = document.createElement('script');
s.src = chrome.extension.getURL(script);
s.onload = function () {
this.parentNode.removeChild(this);
};
(document.head || document.documentElement).appendChild(s);
}
}
[path_to_javascript, ...].forEach(inject) // put the javascript filename you'd like to inject in this array.
Run Code Online (Sandbox Code Playgroud)
您需要阅读有关网络可访问资源的更多信息,它们根本不是为了这个。推荐的方法是将其作为扩展源的一部分。帮助页面向您展示了如何做到这一点。现在,如果出于某种奇怪的原因你真的想从 CDN 导入它,这是可能的。您已经添加了必要的“自我”修改权限。现在需要修改内容页面html并在页面中手动注入脚本。不值得,因为除了稍小且较慢的扩展之外,您什么也得不到。