contentscript.jsChrome擴展標籤SendMessage函數contentscript錯誤
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"});
});
background.js
chrome.browserAction.onClicked.addListener(function(tab) {
chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
chrome.tabs.sendMessage(tabs[0].id, {greeting: "hello"}, function(response) {
console.log(response.farewell);
});
});
});
這有時工作,有時沒有。
案件不起作用:
1)當我重新加載擴展,單擊現有選項卡 2)擴展程序圖標,當我添加一個斷點內background.js
案件做工作:
1)當我重新加載擴展並且重新加載/加載新標籤和沒有斷點內background.js加入
引發的錯誤通常是:
Port: Could not establish connection. Receiving end does not exist. lastError:29
Error in event handler for 'undefined': Cannot read property 'farewell' of undefined TypeError: Cannot read property 'farewell' of undefined
at chrome-extension://glbcapgiojbbnjhngjdmoglaamjbjjak/background.js:16:28
at <error: illegal access>
at Event.dispatchToListener (event_bindings:356:21)
at Event.dispatch_ (event_bindings:342:27)
at Event.dispatch (event_bindings:362:17)
at Object.chromeHidden.Port.dispatchOnDisconnect (miscellaneous_bindings:258:27)
有沒有人可以對此有所瞭解?我覺得特別奇怪的是,斷點會導致它失敗(就像暫停background.js會導致事件監聽器死掉一樣)