Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Desktop notifications from content scripts

I am trying to show a simple desktop notification code from a content script, but it doesn't seem to work.. I have added the permissions in the maifest.json file. Is there a restriction on showing them from the content script ?

like image 286
sharath Avatar asked Aug 21 '10 06:08

sharath


People also ask

How do I see all desktop notifications?

Windows 10 puts notifications and quick actions in Action Center—right on the taskbar—where you can get to them instantly. Select Action Center on the taskbar to open it. (You can also swipe in from the right edge of your screen, or press Windows logo key + A.)

What is a desktop notification?

Desktop notifications create a popup message each time an inbound call is received. Popup messages provide both a visual and audio notification of an inbound call and appear even when the browser is minimized.


2 Answers

You can't show notifications directly through a content script. But, you can show them through the background page.

Your manifest.js should look something like this:

    {
     "name": "Notify This",
     "version": "0.1",
     "permissions": [
        "notifications"
     ],
     "background_page": "background.html",
     "content_scripts": [
       {
        "matches": ["http://www.example.com/*"],
        "js": ["contentscript.js"]
       }
     ]
    }    

Then use the chrome.extension.sendRequest():

    // in your contentscript.js
    chrome.extension.sendRequest({msg: "Sup?"}, function(response) { // optional callback - gets response
        console.log(response.returnMsg);
    });

And on the receiving end you should have a onRequest listener:

    // in your background.html
    chrome.extension.onRequest.addListener(
      function(request, sender, sendResponse) {

            // Create a simple text notification:
        var notify = webkitNotifications.createNotification(
          '48.png',  // icon url - can be relative
          'Hello!',  // notification title
          request.msg  // notification body text
        );

        notify.show();

        setTimeout(function(){ notify.cancel(); },5000);
        sendResponse({returnMsg: "All good!"}); // optional response
      });
like image 101
hitautodestruct Avatar answered Sep 23 '22 23:09

hitautodestruct


Yes, notifications use Chrome specific API, and the content script is only valid for general javascript etc... The background page is where all chrome specific API's are capable of running... First you'll need to register your background page in the manifest.json file - like this:

 "background_page": "background.html",

Also in the manifest file, Allow the required permissions:

"permissions": [ "notifications" ],

Then your script in the background page should look like this :

<script>
setTimeout("setNotification();",1); 
function setNotification(){
  var n
  if (window.webkitNotifications.checkPermission() != 0){
    setNotification();
    return false;
  }
n = window.webkitNotifications.createHTMLNotification('http://www.your-notification-address.com');
n.show();}
</script>
like image 30
David Avatar answered Sep 22 '22 23:09

David