Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get tab notifications in chrome extension, so that the extension can show desktop alert each time when new notification arrives

Is it possible to read the tab notifications (facebook, twitter, gmail etc shows a notification count) through a google chrome extension. So that I can display an desktop alert whenever a new notification arrives.enter image description here

like image 270
nbk Avatar asked Sep 28 '11 08:09

nbk


People also ask

How do I add Chrome to notifications and actions in Windows 10?

Click on Notifications & actions . Click on the app Google Chrome and toggle the switch On or Off to show notifications in action center, control the number of notification visible in the action center, control sound, set priority of notification in the action center and many more. Once completed, Close the window.


1 Answers

You could do this by periodically checking the titles of all relevant tabs, extracting the current notification number (if available) and notifying the user if a change in the number of notifications is detected.

However, depending on how often you perform this check the user will likely notice before you. An alternative approach would be to use content scripts to inject JavaScript (again, only to relevant pages - facebook etc) that listens to changes in the page's title element and sends a message to your background page when one is detected which should then display a desktop notification.

I didn't know if this would work originally as I wasn't aware that title changes could be listened to but a quick Google provided this answer;

how to listen for changes to the title element?

So all you should need to do is change the code in that question's answer to send a message to your background page instead of showing an alert. For example;

function titleModified() {
    chrome.extension.sendRequest({
        title: document.title
    });
}

The background page must have an onRequest listener that would then attempt to parse the title received and determine whether or not a notification has been made.

Remember to correctly setup the permissions in your manifest in order to inject the JavaScript when your users are on the target sites as well as using desktop notifications.

like image 51
neocotic Avatar answered Oct 10 '22 20:10

neocotic