0
我正在創建一個捕獲網站截圖的Google Chrome擴展程序,我需要我的popup.js
才能發送消息給我的background.js
。我不斷收到此錯誤:Error in response to storage.get: TypeError: Cannot read property 'addListener' of undefined
。對storage.get的響應錯誤:TypeError:無法讀取未定義的屬性'addListener'
這裏是我的background.js當前的代碼:
chrome.storage.sync.get({
extensionBehavior: 'onClick',
logIn: false
}, function(items) {
if(items.extensionBehavior == 'onClick'){
// When the extension icon is clicked, send a message to the content script
chrome.runtime.onMessage.addListener(function(request, sender, sendResponse) {
if (!request.message){
var img;
chrome.tabs.captureVisibleTab(null, {}, function(dataURL){
img = dataURL;
chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
chrome.tabs.sendMessage(tabs[0].id, {"message": tabs[0].url, "statusCode": 0, "image": img, "note" : request.note}, function(response){});
});
});
}
});
}
else {
/* ... */
}
});
我popup.js正確發送消息,但我的背景劇本一直給我同樣的錯誤。 chrome.storage.sync.get
內的chrome.runtime
API不可用?在我添加該方法之前,chrome.tabs
API工作正常,彈出窗口肯定是發送消息(我測試了它)。
'chrome.runtime.onMessage'未定義。 –
但我在內容腳本中使用了'chrome.runtime.onMessage.addListener',它工作得很好。 @SpencerWieczorek –
不知道這些Chrome API,但嘗試'console.log(chrome.runtime)'看看它是你的預期。 – Oriol