From 24eae6e666de08beb8d45c430eeaf949d6ef0bc0 Mon Sep 17 00:00:00 2001 From: Kerrick Long Date: Mon, 18 Jul 2011 19:38:18 -0500 Subject: [PATCH] Desktop notifications for orangereds! --- _locales/en_US/messages.json | 11 +++++++ fancy-settings/manifest.js | 33 ++++++++++++++++++++ html/background.html | 1 + js/common.js | 57 +++++++++++++++++++++++++++++++++-- manifest.json | 3 +- pix/mail.png | Bin 0 -> 2890 bytes 6 files changed, 101 insertions(+), 4 deletions(-) create mode 100644 pix/mail.png 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 0000000000000000000000000000000000000000..c7097ffcf75e2e6a7fd5e1650609da7f6fcf8696 GIT binary patch literal 2890 zcmV-Q3$^r#P)KLZ*U+IBfRsybQWXdwQbLP>6pAqfylh#{fb6;Z(vMMVS~$e@S=j*ftg6;Uhf59&ghTmgWD0l;*T zI709Y^p6lP1rIRMx#05C~cW=H_Aw*bJ-5DT&Z2n+x)QHX^p z00esgV8|mQcmRZ%02D^@S3L16t`O%c004NIvOKvYIYoh62rY33S640`D9%Y2D-rV&neh&#Q1i z007~1e$oCcFS8neI|hJl{-P!B1ZZ9hpmq0)X0i`JwE&>$+E?>%_LC6RbVIkUx0b+_+BaR3cnT7Zv!AJxW zizFb)h!jyGOOZ85F;a?DAXP{m@;!0_IfqH8(HlgRxt7s3}k3K`kFu>>-2Q$QMFfPW!La{h336o>X zu_CMttHv6zR;&ZNiS=X8v3CR#fknUxHUxJ0uoBa_M6WNWeqIg~6QE69c9o#eyhGvpiOA@W-aonk<7r1(?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