Created
June 28, 2013 19:28
-
-
Save agentlame/5887387 to your computer and use it in GitHub Desktop.
Mod Mail Desktop Notifications
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
| // ==UserScript== | |
| // @name Mod Mail Desktop Notifications | |
| // @namespace http://www.reddit.com/r/agentlame | |
| // @author reddit, agentlame | |
| // @description Show desktop notifications for new mod mail threads (webkit only) | |
| // @include http://www.reddit.com/* | |
| // @include http://reddit.com/* | |
| // @include http://*.reddit.com/* | |
| // @downloadURL | |
| // @version 0.9 | |
| // ==/UserScript== | |
| (function () { | |
| redditInfo = { | |
| request: function(options) { | |
| if (!options.data) { options.data = {} } | |
| options.data['app'] = 'shine' | |
| $.ajax(options) | |
| }, | |
| update: function(callback) { | |
| this.request({ | |
| url: 'http://www.reddit.com/api/me.json', | |
| success: function(resp) { | |
| if (resp.data) { | |
| console.log('Updated reddit user data', resp.data) | |
| this.storeModhash(resp.data.modhash) | |
| if (callback) { callback(resp.data) } | |
| } | |
| }.bind(this), | |
| error: function() { callback(false) } | |
| }) | |
| }, | |
| fetchMail: function(callback) { | |
| this.request({ | |
| url: 'http://www.reddit.com/message/moderator.json', | |
| success: function(resp) { | |
| if (resp.data) { | |
| callback(resp.data.children) | |
| } | |
| }, | |
| error: function() { callback(false) } | |
| }) | |
| }, | |
| init: function() { | |
| this.modhash = localStorage['modhash'] | |
| }, | |
| storeModhash: function(modhash) { | |
| localStorage['modhash'] = this.modhash = modhash | |
| } | |
| } | |
| mailNotifier = { | |
| lastSeen: null, | |
| notify: function(messages) { | |
| this.setLastSeen(); | |
| var newIdx = null, | |
| lastSeen = this.lastSeen, | |
| newCount = 0 | |
| for (i = 0; i < messages.length; i++) { | |
| var messageTime = messages[i].data.created_utc*1000 | |
| //console.log(lastSeen, messageTime); | |
| if (!lastSeen || messageTime > lastSeen) { | |
| newCount++ | |
| if (!newIdx) { newIdx = i } | |
| this.lastSeen = Math.max(this.lastSeen, messageTime) | |
| } | |
| } | |
| console.log('New messages: ', newCount) | |
| var title, text | |
| if (newCount == 1) { | |
| var message = messages[newIdx] | |
| title = '/r/' + message.data.subreddit + ': ' + message.data.subject | |
| text = 'From: /u/' + message.data.author + '\n' + message.data.body | |
| } else if (newCount > 1) { | |
| title = 'New Mod Mail' | |
| text = 'You have ' + newCount + ' new messages.' | |
| } | |
| if (newCount > 0) { | |
| localStorage['Toolbox.ModMailPro.lastseen'] = this.lastSeen; | |
| this.showNotification(title, text) | |
| } | |
| }, | |
| setLastSeen: function(){ | |
| this.lastSeen = new Date().getTime(); | |
| if (localStorage['Toolbox.ModMailPro.lastseen']){ | |
| this.lastSeen = localStorage['Toolbox.ModMailPro.lastseen']; | |
| } | |
| }, | |
| clear: function() { | |
| if (this.notification) { | |
| this.notification.cancel() | |
| } | |
| }, | |
| notification: null, | |
| showNotification: function(title, text) { | |
| if (this.notification) { | |
| this.notification.cancel() | |
| } | |
| // Move this completely out of here. | |
| if ( window.webkitNotifications ) { | |
| if ( window.webkitNotifications.checkPermission() ) { | |
| $('<li><a href="javascript:;" id="show_button">allow notifications!</a></li>').appendTo('#header-bottom-left'); | |
| document.querySelector('#show_button').addEventListener('click', function() { | |
| if (window.webkitNotifications.checkPermission() != 0) { | |
| window.webkitNotifications.requestPermission(); | |
| //window.location.reload(); | |
| } | |
| // loop wait here. | |
| }, false); | |
| } | |
| } | |
| var n = this.notification = | |
| webkitNotifications.createNotification('', title, text) | |
| this.notification.onclick = function() { | |
| window.open('http://www.reddit.com/message/moderator/') | |
| n.cancel() | |
| } | |
| this.notification.show() | |
| } | |
| } | |
| mailChecker = { | |
| lastchecked: null, | |
| checkInterval: 2*60*1000, | |
| timer: null, | |
| interval: null, | |
| start: function() { | |
| if (!this.interval) { | |
| localStorage['Toolbox.ModMailPro.checkinterval'] = this.checkInterval; | |
| console.log('Starting periodic mail check.') | |
| this.interval = window.setInterval(this.check, this.checkInterval) | |
| this.check() | |
| } | |
| }, | |
| stop: function() { | |
| if (this.interval) { | |
| console.log('Stopping periodic mail check.') | |
| window.clearInterval(this.interval) | |
| this.interval = null | |
| } | |
| }, | |
| check: function() { | |
| this.timer = localStorage['Toolbox.ModMailPro.checkinterval']; | |
| this.lastchecked = localStorage['Toolbox.ModMailPro.lastchecked'] || -1; | |
| if ((new Date().getTime() - this.lastchecked) < this.timer) { | |
| console.log('Checked less than check interval, likely running on another page, or a reload.'); | |
| return; | |
| } | |
| // Update last checked. | |
| localStorage['Toolbox.ModMailPro.lastchecked'] = new Date().getTime(); | |
| redditInfo.update(function(info) { | |
| if (info.has_mod_mail) { | |
| redditInfo.fetchMail(mailNotifier.notify.bind(mailNotifier)) | |
| } else { | |
| mailNotifier.clear() | |
| } | |
| }) | |
| } | |
| } | |
| redditInfo.init(); | |
| mailChecker.start(); | |
| })(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment