gpt4 book ai didi

javascript - Chrome 扩展 - onMessage 监听器不工作

转载 作者:行者123 更新时间:2023-11-30 05:37:35 25 4
gpt4 key购买 nike

我正在尝试执行一个脚本,该脚本在单击 chrome 扩展程序时操纵页面的 html。 (不涉及弹出窗口)

问题是我无法使 alert("test") 正常工作(位于 content.js 中)。我做错了什么?

list .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"
]


}

背景.js

chrome.browserAction.onClicked.addListener(function(tab) {
chrome.tabs.sendMessage(tab.id, {
'action': 'SwapNameAndSurname',
'data' : {'Name' : 'John', 'Surname' : 'Doe'}
});
});

内容.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"});
});

最佳答案

我被困在这个问题上有一段时间了。

刷新扩展页面就完成了工作。

关于javascript - Chrome 扩展 - onMessage 监听器不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22725179/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com