Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Show unread count on button (version 1.6.10) #20
  • Loading branch information
Rob--W committed Apr 9, 2016
1 parent 8b711d0 commit c16a3df
Show file tree
Hide file tree
Showing 11 changed files with 65 additions and 3 deletions.
Binary file added Chrome/icon19.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Chrome/icon38.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Chrome/icon48.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
12 changes: 11 additions & 1 deletion Chrome/manifest.json
@@ -1,7 +1,7 @@
{
"name": "Desktop Notifications for Stack Exchange",
"description": "Real-time desktop notifications for the Stack Exchange.",
"version": "1.6.8",
"version": "1.6.10",
"manifest_version": 2,
"background": {
"scripts": [
Expand All @@ -20,6 +20,16 @@
"optional_permissions": [
"background"
],
"browser_action": {
"default_icon": {
"19": "icon19.png",
"38": "icon38.png"
}
},
"icons": {
"48": "icon48.png",
"128": "icon.png"
},
"permissions": [
"https://api.stackexchange.com/2.1/inbox*",
"notifications",
Expand Down
12 changes: 12 additions & 0 deletions Chrome/using-websocket.js
Expand Up @@ -279,13 +279,25 @@ function showNotification() {
_notification.show();
}
}
function updateBageText() {
chrome.browserAction.setBadgeText({
text: String(getUnreadCount() || ''),
});
}

chrome.browserAction.onClicked.addListener(function() {
// Do the same as when a notification is clicked.
// This is just so that *something* happens when the button is clicked.
openTab(getLink() || generateDefaultLink());
});

// When the UID changes, restart socket (socket will be closed if UID is empty)
eventEmitter.on('change:uid', function(id) {
if (localStorage.getItem('autostart') != '0') startSocket();
});
// When unread count is set, show a notification
eventEmitter.on('change:unread', showNotification);
eventEmitter.on('change:unread', updateBageText);
StackExchangeInbox.on('change:unread', setUnreadCount);
StackExchangeInbox.on('error', function(error_message) {
console.log(error_message);
Expand Down
Binary file added Firefox/data/icon32.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 5 additions & 0 deletions Firefox/data/options.html
Expand Up @@ -15,6 +15,11 @@
} else {
// Some dummy value.
background.setUnreadCount(42);
clearTimeout(showNotificationForTesting.timer);
showNotificationForTesting.timer = setTimeout(function() {
// Restore original value.
background.setUnreadCount(0);
}, 5000);
}
}
</script>
Expand Down
7 changes: 7 additions & 0 deletions Firefox/data/using-websocket.js
Expand Up @@ -189,13 +189,20 @@ function showNotification() {
postMessage('{"method":"hideNotification"}', '*');
}
}
function updateBageText() {
postMessage(JSON.stringify({
method: 'updateBageText',
text: String(getUnreadCount() || ''),
}), '*');
}

// When the UID changes, restart socket (socket will be closed if UID is empty)
eventEmitter.on('change:uid', function(id) {
if (localStorage.getItem('autostart') != '0') startSocket();
});
// When unread count is set, show a notification
eventEmitter.on('change:unread', showNotification);
eventEmitter.on('change:unread', updateBageText);
StackExchangeInbox.on('change:unread', setUnreadCount);
StackExchangeInbox.on('error', function(error_message) {
console_log(error_message);
Expand Down
13 changes: 12 additions & 1 deletion Firefox/lib/main.js
Expand Up @@ -26,6 +26,8 @@ const DUMMY_AUTH_USERNAME = 'dummy_auth_username';
if (!sstorage.localStorageData) sstorage.localStorageData = {};

var optionsPanel;
let optionsButton; // "browser action"

// Read the optional auth token from storage and launch the panel
require('sdk/passwords').search({
realm: 'stackexchange-notifications',
Expand Down Expand Up @@ -79,7 +81,10 @@ function onReady(token) {
let options = {
id: 'widget-desktop-notifications-se',
label: 'Real-time desktop notifications for Stack Exchange\'s inbox',
icon: data.url('icon.png'),
icon: {
16: data.url('icon.png'),
32: data.url('icon32.png'),
},
};
let button = require('sdk/ui/button/toggle').ToggleButton(options);
button.on('change', function(state) {
Expand All @@ -96,6 +101,9 @@ function onReady(token) {
checked: false
});
});

// Set global variable so that the button badge text can be updated later.
optionsButton = button;
}
function onStorageChange(mutation) {
if (mutation.type === 'setItem') {
Expand All @@ -116,6 +124,9 @@ function onOptionsMessage(message) {
});
}
break;
case 'updateBageText':
optionsButton.badge = message.text;
break;
case 'showNotification':
var matchesURL = function(url) {
return message.data.link.indexOf(url) === 0;
Expand Down
2 changes: 1 addition & 1 deletion Firefox/package.json
Expand Up @@ -3,7 +3,7 @@
"license": "MIT",
"author": "Rob Wu <rob@robwu.nl>",
"homepage": "https://stackapps.com/q/3780/9699",
"version": "1.6.9",
"version": "1.6.10",
"fullName": "Desktop notifications for Stack Exchange's inbox",
"id": "stackexchange-notifications@jetpack",
"main": "lib/main",
Expand Down
17 changes: 17 additions & 0 deletions Makefile
@@ -1,11 +1,28 @@
all: firefox chrome
.PHONY: firefox chrome

define mkicon
inkscape -z -w "$2" -h "$2" se-logo.svg -e "$1icon$2.png"
pngcrush -q -brute "$1icon$2.png"{,.} && mv "$1icon$2.png"{.,}
endef

chrome:
cd Chrome && 7z u -tzip ../extension.zip *

firefox:
cd Firefox && jpm xpi

icons-chrome:
$(call mkicon,Chrome/,19)
$(call mkicon,Chrome/,38)
$(call mkicon,Chrome/,48)

icons-firefox:
# No 16x16 is generated because we use the favicon for that,
# which is optimized for legibility at a small (16x16) size.
$(call mkicon,Firefox/data/,32)

icons: icons-chrome icons-firefox

clean:
rm extension.zip

0 comments on commit c16a3df

Please sign in to comment.