diff --git a/_locales/en_US/messages.json b/_locales/en_US/messages.json index 7f3427e..54b46ab 100644 --- a/_locales/en_US/messages.json +++ b/_locales/en_US/messages.json @@ -388,5 +388,16 @@ "content": "" } } + }, + "orangered_received": { + "message": "You have an $ORANGERED$!", + "placeholders": { + "ORANGERED": { + "content": "orangered" + } + } + }, + "orangered_action": { + "message": "Click here to be taken to your inbox." } } diff --git a/fancy-settings/manifest.js b/fancy-settings/manifest.js index 6369548..9af2684 100755 --- a/fancy-settings/manifest.js +++ b/fancy-settings/manifest.js @@ -67,6 +67,39 @@ this.manifest = { "type": "description", "text": "
When checked, this will add the following to each commment you post from within Mostly Harmless:
Posted from Mostly Harmless, a Google Chrome extension for awesome redditors.
" }, + { + "tab": "Preferences", + "group": "Orangered Notifications", + "name": "checkMail", + "type": "checkbox", + "label": "Check for orangereds" + }, + { + "tab": "Preferences", + "group": "Orangered Notifications", + "name": "mailInterval", + "type": "slider", + "label": "", + "max": 5, + "min": 0, + "step": 1, + "display": true, + "displayModifier": function (value) { + if (value === 0) + return "every 30 seconds"; + else if (value === 1) + return "every minute"; + return "every " + value.toString() + " minutes"; + } + + }, + { + "tab": "Preferences", + "group": "Orangered Notifications", + "name": "mailDescription", + "type": "description", + "text": "When checked, Mostly Harmless will check for new orangereds at the interval you set. If you get an orangered, it will display a desktop notification for five seconds.
" + }, { "tab": "Performance", "group": "Cache Time", diff --git a/html/background.html b/html/background.html index b95db07..b33169f 100644 --- a/html/background.html +++ b/html/background.html @@ -15,6 +15,7 @@ cache.removeAll(); button.setBadgeDefaults(); chrome.tabs.onUpdated.addListener(background.prepareBrowserAction); + background.watchMail(); } window.onload = initBackground; diff --git a/js/common.js b/js/common.js index cfcc4b7..248d66a 100644 --- a/js/common.js +++ b/js/common.js @@ -6,7 +6,9 @@ settings = new Store('settings', { 'freshCutoff': 7, 'popupWidth': 640, 'shamelessPlug': false, - 'waitForClick': false + 'waitForClick': false, + 'checkMail': true, + 'mailInterval': 1 }); cache = new Store('cache'); @@ -205,7 +207,7 @@ BrowserAction.prototype.setBadgeDefaults = function (tabId) { chrome.browserAction.setTitle({'title': 'Click to load data.', 'tabId': tabId}); chrome.browserAction.setBadgeBackgroundColor({'color': [192, 192, 192, 255], 'tabId': tabId}); chrome.browserAction.setPopup({popup: '', tabId: tabId}); - chrome.browserAction.onClicked.addListener(function(tab) { + chrome.browserAction.onClicked.addListener(function (tab) { reddit.getInfo(tab.url, tab.id); }); return true; @@ -258,7 +260,7 @@ BrowserAction.prototype.setBadgeError = function (tabId, text) { chrome.browserAction.setTitle({'title': text, 'tabId': tabId}); chrome.browserAction.setBadgeBackgroundColor({'color': [200, 0, 0, 255], 'tabId': tabId}); chrome.browserAction.setPopup({popup: '', tabId: tabId}); - chrome.browserAction.onClicked.addListener(function(tab) { + chrome.browserAction.onClicked.addListener(function (tab) { reddit.getInfo(tab.url, tab.id); }); return true; @@ -770,6 +772,55 @@ Background.prototype.prepareBrowserAction = function (tabId, info, tab) { return true; }; +/** + * Sets a timeout for the next time to run checkMail. + * @alias Background.watchMail() + * @return {Boolean} Returns true. + * @method + */ +Background.prototype.watchMail = function () { + var mailProcess; + + function showNotification (hasMail) { + var notification, notificationTimeout, mailInterval; + + mailInterval = (settings.get('mailInterval') === 0) ? 1000 * 30 : settings.get('mailInterval') * 1000 * 60; + + if (hasMail === true) { + console.log('Orangered! I need to show a notification...'); + notification = webkitNotifications.createNotification( + '/pix/mail.png', // icon url - can be relative + chrome.i18n.getMessage('orangered_received'), // notification title + chrome.i18n.getMessage('orangered_action') // notification body text + ); + notificationTimeout = window.setTimeout(function () { + notification.cancel(); + }, 1000 * 5); + notification.onclick = function () { + chrome.tabs.create({'url': 'http://' + reddit.domain + '/message/unread/', 'selected': true}); + notification.cancel(); + window.clearTimeout(notificationTimeout); + }; + notification.show(); + window.setTimeout(checkPrefs, mailInterval); + } + } + + function getMail () { + reddit.apiTransmit('GET', 'http://' + reddit.domain + '/api/me.json', null, function (response) { + showNotification(response.data.has_mail); + }); + } + + function checkPrefs () { + if (settings.get('checkMail') === true) { + getMail(); + } + } + + checkPrefs(); +}; + /** * Creates a new framework of popup processes * @classDescription Creates a new framework of background processes. diff --git a/manifest.json b/manifest.json index 474f83f..b190666 100644 --- a/manifest.json +++ b/manifest.json @@ -8,7 +8,8 @@ }, "permissions": [ "http://*.reddit.com/", - "tabs" + "tabs", + "notifications" ], "background_page": "html/background.html", "options_page": "fancy-settings/index.html", diff --git a/pix/mail.png b/pix/mail.png new file mode 100644 index 0000000..c7097ff Binary files /dev/null and b/pix/mail.png differ