I am trying to send a message from a content script to the background script in a chrome extension that triggers a rich notification to open. I can already achieve this but it breaks the rest of my extension.
In my content script I have a call to chrome.extension.sendMessage inside of which I load my extension code. This was all working fine until I added my notification code, I decided to use chrome Rich Notifications API as I would like to have buttons in my notification eventually, and I am led to believe that only the background script can open the rich notification, hence the need for messages. If I comment out the chrome.runtime.OnMessage.addListener function in background.js my extension logic loads properly again, so something about that call is conflicting with the chrome.extension.sendMessage function in inject.js.
Can anyone explain why this happens and how to fix it?
A simplified version of my code is as follows:
manifest.json
{ "name": "Test", "version": "0.0.1", "manifest_version": 2, "description": "Test "permissions": [ "notifications" ], "background": { "persistent": false, "scripts": ["background.js"] }, "content_scripts": [ { "matches": [ "mywebsite/*" ], "js": [ "inject.js", ] } ], "web_accessible_resources": [ "notificationIcon.png" ] }
background.js
chrome.runtime.onMessage.addListener(function(request, sender) { if (request.type == "notification") chrome.notifications.create('notification', request.options, function() { }); });
inject.js
chrome.extension.sendMessage({}, function(response) { //code to initialize my extension }); //code to send message to open notification. This will eventually move into my extension logic chrome.runtime.sendMessage({type: "notification", options: { type: "basic", iconUrl: chrome.extension.getURL("icon128.png"), title: "Test", message: "Test" }});
A content script is a part of your extension that runs in the context of a particular web page (as opposed to background scripts which are part of the extension, or scripts which are part of the website itself, such as those loaded using the <script> element).
Receiving End Does Not Exist Error Happening? This error usually happens due to a problem with a specific Chrome extension inside your main document. The program cannot establish a connection, and many web developers are usually faced with this error while working on Chrome.
js') is a JavaScript script that runs once our extension either gets installed or the user refreshes the extension manually. The background script doesn't actually have access to any of the webpages your user is viewing, so your background script can't manipulate the DOM.
The problem was caused because my listener in background.js was not returning a response. So the function response of my chrome.extension.sendMessage was never being executed.
I changed my background.js to be:
chrome.runtime.onMessage.addListener(function(request, sender, sendResponse) { if (request.type == "worktimer-notification") chrome.notifications.create('worktimer-notification', request.options, function() { }); sendResponse(); });
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With