Skip to content

Instantly share code, notes, and snippets.

Created February 12, 2015 21:30
Show Gist options
  • Select an option

  • Save anonymous/bc7753ebf3cd77583a97 to your computer and use it in GitHub Desktop.

Select an option

Save anonymous/bc7753ebf3cd77583a97 to your computer and use it in GitHub Desktop.

Revisions

  1. @invalid-email-address Anonymous created this gist Feb 12, 2015.
    49 changes: 49 additions & 0 deletions notif-bookmark.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,49 @@
    javascript:(function() {
    var username = "milcom_";
    var timeToDisplayNotifInSeconds = 5;

    var notificationRegistrar = function(){

    var notify = function(message, callback) {
    var notification = new Notification(message);
    callback(notification);
    };

    angular.element(document.body).injector().invoke(['$rootScope', 'shrub-socket', 'shrub-ui/notifications', function($rootScope, socket, notifications) {
    if (window.notifyMentions) {
    window.notifyMentions = false;
    notifications.add({text: 'Notifications for mentions disabled.'});
    } else {
    window.notifyMentions = true;
    socket.on('reddichat.chat.message', function(message) {
    if(message.text.search(username) > -1) {
    notify(message. from + ": " + message.text, function(notification){
    setTimeout(function(){
    notification.close();
    }, timeToDisplayNotifInSeconds * 1000);
    });
    }
    });

    notifications.add({text: 'Notifications for mentions enabled.'});
    }
    $rootScope.$digest();
    }]);
    };

    var registerNotificationsIfPermitted = function(callback) {
    if(!("Notification" in window)) {
    alert("This browser does not support desktop notification");
    } else if(Notification.permission == "granted") {
    callback();
    } else {
    Notification.requestPermission(function(permission){
    if(permission == "granted") {
    callback();
    }
    });
    }
    };
    registerNotificationsIfPermitted(notificationRegistrar);

    })();