我正在开发 Chrome 扩展程序,有没有办法从注入文件中获取 chrome.extension.getURL('file path')
方法?我无法从注入的文件访问上述方法。
清单.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
}
注入.js
console.log("Ok injected file worked");
console.log("Url: ", chrome.extension.getURL('html/popup.html'));
内容脚本.js
var s = document.createElement('script');
s.src = chrome.extension.getURL('js/injected.js');
(document.head || document.documentElement).appendChild(s);
原文由 Bharath Kumar 发布,翻译遵循 CC BY-SA 4.0 许可协议
不,你不能,一旦你在页面中注入脚本,它就无法访问
chrome.extension.getURl
。但是您可以在injected script
和content script
之间进行通信。其中一种方法是使用自定义事件。mainfest.json
:在你的
injected script
:在你的
content script
: