Created
February 12, 2015 21:30
-
-
Save anonymous/bc7753ebf3cd77583a97 to your computer and use it in GitHub Desktop.
Notify mentions for Reddichat
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment