我一直在搜索 SO 并阅读谷歌文档,但我似乎找不到解决方案。
我的 Chrome 扩展程序正在注入内容脚本,我想设置一个onRequest.listener
以便将请求发送到内容脚本。这是我以前为onRequest.listener
. 问题是由于某种未知原因,我不断收到此错误。
错误信息:
Uncaught TypeError: Cannot ready property 'onRequest' of undefined
contentscript.js line 1;
这是相关的代码...
清单.json
{
"name": "Injector Extension",
"version": "1.0",
"manifest_version": 1,
"icons": { "128": "icon.png" },
"browser_action": {
"default_icon": "icon.png",
"default_title": "Injector Extension",
"default_popup": "popup.html"
},
"options_page": "options.html",
"background": {
"page": "background.html"
},
"permissions": [
"tabs",
"http://*/*",
"https://*/*",
"unlimitedStorage"],
"content_scripts": [{
"matches": [" (injector specific url) "],
"js": ["contentscript.js"]
}],
"web_accessible_resources": ["js/script.js"]
}
内容脚本
chrome.extension.onRequest.addListener(function(request, sender, sendResponse) {
if (request.method == "fromPopup") {
// Send JSON data back to Popup.
sendResponse({data: "from Content Script to Popup"});
} else {
sendResponse({}); // snub them.
}
});
弹出窗口
chrome.tabs.getSelected(null, function(tab) {
chrome.tabs.sendRequest(tab.id, {method: "fromPopup", tabid: tab.id}, function(response) {
console.log(response.data);
});
});