javascript Chrome 扩展 getUrl 在注入的文件中不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33144234/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Chrome extension getUrl not working in injected file
提问by Bharath Kumar
I'm developing a Chrome extension, is there any way to get the chrome.extension.getURL('file path')
method from injected file? I'm unable to access above method from injected file.
我正在开发 Chrome 扩展程序,有什么方法可以chrome.extension.getURL('file path')
从注入的文件中获取该方法吗?我无法从注入的文件访问上述方法。
manifest.json
清单文件.json
{
"name": "Name",
"version": "0.1",
"description": "Name chrome extension",
"background": {
"persistent": false,
"scripts": ["js/background.js"]
},
"permissions": [
"tabs",
"https://*/*"
],
"content_scripts": [
{
"matches": ["https://mail.google.com/*"],
"js": ["js/content.js"],
"run_at": "document_end"
}
],
"web_accessible_resources": [
"js/injected.js",
"html/popup.html"
],
"manifest_version": 2
}
injected.js
注入.js
console.log("Ok injected file worked");
console.log("Url: ", chrome.extension.getURL('html/popup.html'));
contentScript.js
内容脚本.js
var s = document.createElement('script');
s.src = chrome.extension.getURL('js/injected.js');
(document.head || document.documentElement).appendChild(s);
采纳答案by Sid
No you cant, once you inject the script in a page, it cannot access chrome.extension.getURl
. But you can communicate between your injected script
and content script
. One of the methods is using custom events.
不,你不能,一旦你在页面中注入脚本,它就无法访问chrome.extension.getURl
. 但是您可以在您的injected script
和之间进行通信content script
。其中一种方法是使用自定义事件。
mainfest.json
:
mainfest.json
:
{
"name": "Name",
"version": "0.1",
"description": "Name chrome extension",
"background": {
"persistent": false,
"scripts": ["js/background.js"]
},
"permissions": [
"tabs",
"https://*/*"
],
"content_scripts": [
{
"matches": ["https://mail.google.com/*"],
"js": ["js/content.js"],
"run_at": "document_end"
}
],
"web_accessible_resources": [
"js/injected.js",
"html/popup.html"
],
"manifest_version": 2
}
In your injected script
:
在您的injected script
:
console.log("Ok injected file worked");
document.addEventListener('yourCustomEvent', function (e)
{
var url=e.detail;
console.log("received "+url);
});
In your content script
:
在您的content script
:
var s = document.createElement('script');
s.src = chrome.extension.getURL('js/injected.js');
(document.head || document.documentElement).appendChild(s);
s.onload = function(){
var url=chrome.runtime.getURL("html/popup.html");
var evt=document.createEvent("CustomEvent");
evt.initCustomEvent("yourCustomEvent", true, true, url);
document.dispatchEvent(evt);
};
回答by Nishan Miranda
You have to mention the file_path or file_names in the web_accessible_resources of extension manifest.
EG:
您必须在扩展清单的 web_accessible_resources 中提及 file_path 或 file_names。
例如:
"web_accessible_resources":[
"styles/*",
"yourfilename.js"
]
After that you can have have the file in injected script by calling the method.
"chrome.extension.getURL('yourfilename.js')";
之后,您可以通过调用该方法将文件放入注入脚本中。
"chrome.extension.getURL('yourfilename.js')";
回答by Drapaster
Add into extension manifest.json
添加到扩展 manifest.json
"web_accessible_resources": ["*.js"]
From "web_accessible_resources" manual page:
从“web_accessible_resources”手册页:
These resources would then be available in a webpage via the URL chrome-extension://[PACKAGE ID]/[PATH], which can be generated with the extension.getURL method.
然后,这些资源将通过 URL chrome-extension://[PACKAGE ID]/[PATH] 在网页中可用,该 URL 可以使用 extension.getURL 方法生成。