Firefox extension: about PopupNotifications.jsm in

2019-07-12 20:02发布

问题:

I've made a small (bootstrapped) extension to notify me about for some changes on a site. Everything has done, except of displaying notifications.

As I understand HTML5 Notification is not accessible from extensions.

Then I found another way with PopupNotifications.jsm. But the common sample of usage is not working, because "gBrowser is not defined". This variable is used to creating a notification.

I don't want to use any external extensions to work with notifications (I've found at least two). I need a standalone extension.

There is another way - using of the sdk. But I am not ready to use it just for notifications. I want to do my extension as simplest as it possible.

Example I found here: https://developer.mozilla.org/en-US/docs/Using_popup_notifications

OK, now the question: what the best way to add notifications to my extension with minimal changes? Continue with PopupNotifications.jsm? Or try to createInstanse of nsIDOMDesktopNotification (but I don't know suited class name, like '@mozilla.org/...'). What can you suggest me?

回答1:

You can use the alert service or create it manually from chrome://global/content/alerts/alert.xul:

function notify(title, msg, url, img, name, isClickable) {

    var listener = {
        observe: function(subject, topic, data) { // jshint ignore:line
            // if (topic === 'alertclickcallback') {
            // }

            // if (topic === 'alertfinished') {
            // }
        },
    };

    try {
        var alertWin = Cc['@mozilla.org/alerts-service;1'].getService(Ci.nsIAlertsService);
        alertWin.showAlertNotification(img, title, msg, isClickable, url, listener, name);
    }
    catch (e) {
        // fix for when alerts-service is not available (e.g. SUSE)
        var ALERT_XUL = 'chrome://global/content/alerts/alert.xul';
        var FF_WIN_OPTS = 'chrome,dialog=yes,titlebar=no,popup=yes';

        // arguments[0] --> the image src url
        // arguments[1] --> the alert title
        // arguments[2] --> the alert text
        // arguments[3] --> is the text clickable?
        // arguments[4] --> the alert cookie to be passed back to the listener
        // arguments[5] --> the alert origin reported by the look and feel
        // 0: bottom right.
        // 2: bottom left
        // 4: top right
        // 6: top left
        // arguments[6] --> bidi
        // arguments[7] --> lang
        // arguments[8] --> replaced alert window (nsIDOMWindow)
        // // i. e. previous alert
        // arguments[9] --> an optional callback listener (nsIObserver)
        // arguments[10] -> the nsIURI.hostPort of the origin, optional
        var winArgs = [img, title, msg, isClickable, url, 2, null, null, null, listener, name];

        // aParentwindow, aUrl, aWindowName, aWindowFeatures, aWindowArguments (nsISupports)
        var win = Services.ww.openWindow(null, ALERT_XUL, '_blank', FF_WIN_OPTS, null);
        win.arguments = winArgs;
    }
}