[英]Chrome Extension - onMessage Listener not working
I am trying to execute a script which manipulates the page's html when a chrome extension is clicked. 我正在尝试执行一个脚本,该脚本在单击chrome扩展名时会操纵页面的html。 (No popups involed)
(不涉及弹出窗口)
The problem is that I can't get the alert("test") working (located in content.js). 问题是我无法使alert(“ test”)工作(位于content.js中)。 What am I doing wrong?
我究竟做错了什么?
Manifest.Json 的manifest.json
{
"name": "test",
"version": "1.0",
"manifest_version" :2,
"description": "test",
"browser_action" : {
"default_icon" :"icon.png"
},
"background": {
"scripts": ["background.js"]
},
"content_scripts": [
{
"matches": ["http://*/*","https://*/*"],
"js": ["content.js"]
}],
"permissions": [
"http://*/*",
"https://*/*",
"contextMenus",
"tabs"
]
}
background.js background.js
chrome.browserAction.onClicked.addListener(function(tab) {
chrome.tabs.sendMessage(tab.id, {
'action': 'SwapNameAndSurname',
'data' : {'Name' : 'John', 'Surname' : 'Doe'}
});
});
content.js content.js
chrome.runtime.onMessage.addListener(
function(request, sender, sendResponse) {
alert("test");
console.log(sender.tab ?
"from a content script:" + sender.tab.url :
"from the extension");
if (request.greeting == "hello")
sendResponse({farewell: "goodbye"});
});
I was stuck on this for a while. 我被困了一段时间。
Refreshing both the extension and the page did the job. 刷新扩展名和页面都可以完成工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.