I am creating a Google Chrome extension that captures screenshots of websites, and I need my popup.js
to be able to send a message to my background.js
. I keep getting this error: Error in response to storage.get: TypeError: Cannot read property 'addListener' of undefined
.
Here is my current code for 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 {
/* ... */
}
});
My popup.js is sending the message properly, but my background script keeps giving me the same error. Is the chrome.runtime
API not available inside of chrome.storage.sync.get
? Before I added that method, the chrome.tabs
API was working fine, and the popup is definitely sending the message (I tested it).
答案 0 :(得分:1)
The problem was that I was sending tabs.sendMessage
instead of runtime.sendMessage
with my popup.js. The tabs API sends messages to the content scripts, while the runtime API can send messages anywhere.