简体   繁体   中英

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. 在此输入图像描述

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM