0
我在看這個網頁上的代碼演示:https://developer.chrome.com/apps/messaging爲什麼我需要在一次性請求中使用「sendResponse」?
的代碼是這樣的:
Content.js
chrome.runtime.sendMessage({greeting: "hello"}, function(response) {
console.log(response.farewell);
});
Background.js
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"});
});
我主要理解t他編碼,但有一點我不明白是「sendResponse」。如果我刪除sendResponse({farewell: "goodbye"});
,代碼仍然正常工作,這是很好的。但是,如果我從function(request, sender, sendResponse) {
中刪除sendResponse
,則擴展名不會隨消息傳遞一起執行。所以基本上我想知道爲什麼我需要這個參數,即使我不使用它。謝謝