【发布时间】:2022-01-21 04:23:42
【问题描述】:
我阅读了一些 chrome 文档并得到了 this 基本示例的工作。
现在我想根据发生的事件发出请求。该事件被触发并且 contentUpdateData() 运行,但函数中的chrome.runtime.sendMessage 似乎不起作用。任何想法为什么?
/* content.js */
var data = []
chrome.runtime.onMessage.addListener(
function(request, sesnder, sendResponse) {
if (request.message === 'popupClicked') {
contentUpdateData();
}
}
)
function contentUpdateData() {
console.log('Code works up to here. Button clicked in popup.html, recieved in content.js. Need to get info from background.js')
chrome.runtime.sendMessage({greeting: "hello"}, function(response) {
console.log(response.farewell);
data = response.data
});
}
/* background.js basic example from chrome */
chrome.runtime.onMessage.addListener(
function(request, sender, sendResponse) {
console.log(sender.tab ?
"from a content script:" + sender.tab.url :
"from the extension");
if (request.greeting === "hello")
sendResponse({farewell: "goodbye", data: null});
}
);
【问题讨论】:
标签: javascript