I'm trying to implement my own chrome extension on which, on a certain event, create a browser notification and fills the popup with data calculated in background.js
Here is my manifest.json
file:
{
"name": "Dummy name",
"description": "Description",
"manifest_version": 2,
"version": "1.1.3",
"icons": {
"16": "icon_16.png",
"48": "icon_48.png",
"128": "icon_128.png",
"256": "icon_256.png"
},
"browser_action": {
"default_icon": "icon_48.png",
"default_title": "Test",
"default_popup": "popup.html"
},
"permissions": ["background","webRequest","webRequestBlocking","webNavigation","tabs","notifications"],
"background": {
"scripts":["jquery-1.8.1.min.js","classy.js","background.js"]
}
}
My call to sendMessage
in background.js
show : function(result) {
var that = this;
chrome.extension.sendMessage({greeting: "hello"}, function(response) {
console.log(response);
});
if(window.webkitNotifications) {
var notification = webkitNotifications.createHTMLNotification('notification.html');
notification.show();
setTimeout(function(){
notification.cancel();
}, '7000');
}
}
My message listener in popup.js
(from chrome extension samples)
chrome.extension.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"});
});
The only error I get is a
Port error: Could not establish connection. Receiving end does not exist.
Thank you for your help!
chrome.extension.onMessage
. – Kepneralert('')
dialog to see whether the methods occur in the expected order. Side note, you can directly communicate between popup/background viachrome.extension.getBackgroundPage()
(gets access to background's globalwindow
object from the popup) andchrome.extension.getViews({type:'popup'})[0]
(to get the globalwindow
object of a popup, if existent, from the background page). – Kepnerchrome.extension
is deprecated since Chrome v58, usechrome.runtime
– Mujikstorage
event to keep track of changes to the state while popup is open (which is the equivalent of sending a message to the popup). I have posted an answer below. – Pincer