Skip to content
This repository has been archived by the owner on Oct 6, 2021. It is now read-only.

Feature friends notification #2

Merged
merged 3 commits into from
Jul 27, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 34 additions & 9 deletions src/_locales/en/messages.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@
"browserActionErrorTitle": {
"message": "You should be logged into your Battlelog account"
},
"notificationTitle": {
"notificationUpdatesTitle": {
"message": "Battlelog updates"
},
"notificationMessage": {
"notificationUpdatesMessage": {
"message": "You got $count$ new updates",
"placeholders": {
"count": {
Expand All @@ -48,6 +48,22 @@
"notificationButton2Title": {
"message": "View all updates"
},
"notificationFriendsTitle": {
"message": "Battlelog friends"
},
"notificationFriendsMessage": {
"message": "Friend(s) $status$: $count$",
"placeholders": {
"count": {
"content": "$1",
"example": "7"
},
"status": {
"content": "$2",
"example": "online"
}
}
},
"optionsSaved": {
"message": "Options saved"
},
Expand All @@ -72,24 +88,33 @@
"optionsHeaderNotifications": {
"message": "Notifications"
},
"optionsSectionUpdatesHeader": {
"optionsSectionNotificationsUpdatesHeader": {
"message": "Updates"
},
"optionsCheckboxNotifications": {
"optionsCheckboxNotificationsUpdates": {
"message": "Displays a desktop notification when you get new updates"
},
"optionsSelectNotifications": {
"optionsSelectNotificationsUpdates": {
"message": "Check new updates each"
},
"optionsMinutes": {
"message": "minute(s)"
},
"optionsCheckboxNotificationSound": {
"optionsCheckboxNotificationUpdatesSound": {
"message": "Play sound"
},
"optionsTestNotifications": {
"message": "Send test notification"
},
"optionsSectionNotificationsFriendsHeader": {
"message": "Friends"
},
"optionsSectionNotificationsFriends": {
"message": "Displays a desktop notification with count of friends"
},
"optionsCheckboxNotificationsFriends": {
"message": "Activated"
},
"optionsMinutes": {
"message": "minute(s)"
},
"optionsHeaderAdvanced": {
"message": "Advanced"
},
Expand Down
43 changes: 35 additions & 8 deletions src/_locales/fr/messages.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@
"browserActionErrorTitle": {
"message": "Vous devez être connecté à votre compte Battlelog"
},
"notificationTitle": {
"notificationUpdatesTitle": {
"message": "Mises à jour Battlelog"
},
"notificationMessage": {
"notificationUpdatesMessage": {
"message": "Vous avez $count$ nouvelles mises à jour",
"placeholders": {
"count": {
Expand All @@ -47,6 +47,21 @@
},
"notificationButton2Title": {
"message": "Voir toutes les mises à jour"
},"notificationFriendsTitle": {
"message": "Amis Battlelog"
},
"notificationFriendsMessage": {
"message": "Ami(s) $status$ : $count$",
"placeholders": {
"count": {
"content": "$1",
"example": "7"
},
"status": {
"content": "$2",
"example": "online"
}
}
},
"optionsSaved": {
"message": "Options enregistrées"
Expand All @@ -72,24 +87,36 @@
"optionsHeaderNotifications": {
"message": "Notifications"
},
"optionsSectionUpdatesHeader": {
"optionsSectionNotificationsUpdatesHeader": {
"message": "Mises à jour"
},
"optionsCheckboxNotifications": {
"optionsSectionNotificationsUpdates": {
"message": "Affiche une notification de bureau lorsque vous recevez de nouvelles mises à jour"
},
"optionsSelectNotifications": {
"message": "Vérifiez les nouvelles mise à jour toutes les"
"optionsCheckboxNotificationsUpdates": {
"message": "Activé"
},
"optionsMinutes": {
"message": "minute(s)"
"optionsSelectNotificationsUpdates": {
"message": "Vérifiez les nouvelles mise à jour toutes les"
},
"optionsCheckboxNotificationSound": {
"message": "Emettre un son"
},
"optionsTestNotifications": {
"message": "Envoyer une notification de test"
},
"optionsSectionNotificationsFriendsHeader": {
"message": "Amis"
},
"optionsSectionNotificationsFriends": {
"message": "Affiche une notification de bureau avec le nombre d'amis"
},
"optionsCheckboxNotificationsFriends": {
"message": "Activé"
},
"optionsMinutes": {
"message": "minute(s)"
},
"optionsHeaderAdvanced": {
"message": "Avancé"
},
Expand Down
121 changes: 88 additions & 33 deletions src/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
var colorOnline = [120, 199, 83, 255];
var colorIngame = [96, 192, 246, 255];

var soundBleep = 'UI_Bleep_Notification.ogg';

var BASE_URL_TEST = 'https://raw.githubusercontent.com/Narno/Battlelog-Notifier/master/test/fixtures/';
var COMCENTER_PATH_TEST_ONLINE = 'comcenter/sync/online.json';
var COMCENTER_PATH_TEST_INGAME = 'comcenter/sync/ingame.json';
Expand Down Expand Up @@ -74,6 +76,29 @@
statusLabel = chrome.i18n.getMessage('statusOffline');
}
}
// desktop notification
// @todo move code
if (localStorage.notifFriendsIsActivated) {
if (localStorage.getItem('countIngame') !== null) {
var friendsIngameCountPrev = localStorage.getItem('countIngame');
if (friendsIngameCount > friendsIngameCountPrev) {
renderFriendsNotification(friendsIngameCount.toString(), chrome.i18n.getMessage('statusIngame'));
}
}
if (friendsIngameCount > 0) {
localStorage.setItem('countIngame', friendsIngameCount);
}
if (localStorage.getItem('countOnline') !== null) {
var friendsOnlineCountPrev = localStorage.getItem('countOnline');
if (friendsOnlineCount > friendsOnlineCountPrev) {
renderFriendsNotification(friendsOnlineCount.toString(), chrome.i18n.getMessage('statusOnline'));
}
}
if (friendsOnlineCount > 0) {
localStorage.setItem('countOnline', friendsOnlineCount);
}
}
//
// EA Origin not available
} else {
count = '0';
Expand Down Expand Up @@ -150,17 +175,17 @@
});
}

function showNotification() {
function showNotificationUpdates() {
var sound = false;
if (!chrome.notifications || localStorage.notifIsActivated != 'true') {
if (!chrome.notifications || localStorage.notifUpdatesIsActivated != 'true') {
return;
}
if (localStorage.notifIsSound == 'true') {
if (localStorage.notifUpdatesIsSound == 'true') {
sound = true;
}
new NotificationsCount(function (count) {
if (count !== false && count > 0) {
renderNotification(count, sound);
renderUpdatesNotification(count, sound);
}
});
}
Expand Down Expand Up @@ -213,39 +238,58 @@
});
}

// notitifcation renderer
function renderNotification(count, sound) {
// updates notitifcation renderer
function renderUpdatesNotification(count, sound) {
var opt = {
type: "basic",
title: chrome.i18n.getMessage('notificationTitle'),
message: chrome.i18n.getMessage('notificationMessage', [count]),
title: chrome.i18n.getMessage('notificationUpdatesTitle'),
message: chrome.i18n.getMessage('notificationUpdatesMessage', [count]),
iconUrl: "icon-48.png",
buttons: [
{ title: chrome.i18n.getMessage('notificationButton2Title') },
]
};
var optOpera = {
type: "basic",
title: chrome.i18n.getMessage('notificationTitle'),
message: chrome.i18n.getMessage('notificationMessage', [count]),
title: chrome.i18n.getMessage('notificationUpdatesTitle'),
message: chrome.i18n.getMessage('notificationUpdatesMessage', [count]),
iconUrl: "icon-48.png"
};
var notification = chrome.notifications.create('showNotification', opt, function() {
var notification = chrome.notifications.create('showNotificationUpdates', opt, function() {
if (sound) {
var notifAudio = new Audio();
notifAudio.src = 'UI_Bleep_Notification.ogg';
notifAudio.play();
playSound();
}
if (chrome.runtime.lastError) {
console.log(chrome.runtime.lastError.message);
if (chrome.runtime.lastError.message == "Adding buttons to notifications is not supported.") {
var notification = chrome.notifications.create('showNotification', optOpera, function() {});
var notification = chrome.notifications.create('showNotificationUpdates', optOpera, function() {});
}
return;
}
});
}

// friends notitifcation renderer
function renderFriendsNotification(count, status) {
var opt = {
type: "basic",
title: chrome.i18n.getMessage('notificationFriendsTitle'),
message: chrome.i18n.getMessage('notificationFriendsMessage', [count, status]),
iconUrl: "icon-48.png"
};
var notification = chrome.notifications.create('friendsNotification' + status, opt, function(id) {
setTimeout(function() {
chrome.notifications.clear(id);
}, 4000);
});
}

function playSound() {
var notifAudio = new Audio();
notifAudio.src = soundBleep;
notifAudio.play();
}

function isBattlelogUrl(url) {
return url.indexOf(getHomeUrl(localStorage.game)) === 0;
}
Expand Down Expand Up @@ -306,36 +350,36 @@

// alarms
chrome.alarms.create('badge', {periodInMinutes: 1});
if (localStorage.notifIsActivated == 'true' && localStorage.notifFrequency) {
chrome.alarms.create('notification', {periodInMinutes: parseInt(localStorage.notifFrequency)});
if (localStorage.notifUpdatesIsActivated == 'true' && localStorage.notifUpdatesFrequency) {
chrome.alarms.create('notification', {periodInMinutes: parseInt(localStorage.notifUpdatesFrequency)});
}
chrome.alarms.onAlarm.addListener(function (alarm) {
if (alarm.name == 'badge') {
chrome.runtime.sendMessage({do: 'updatebadge'});
chrome.runtime.sendMessage({do: 'update_badge'});
}
if (alarm.name == 'notification') {
showNotification();
showNotificationUpdates();
}
});

// browser action
chrome.browserAction.onClicked.addListener(function () {
chrome.runtime.sendMessage({do: 'updatebadge'});
chrome.runtime.sendMessage({do: 'shownotification'});
chrome.runtime.sendMessage({do: 'update_badge'});
chrome.runtime.sendMessage({do: 'show_updates_notification'});
openBattlelogHomeInTab();
});

if (chrome.notifications) {
// notification action
chrome.notifications.onClicked.addListener(function () {
chrome.notifications.clear('showNotification');
chrome.notifications.clear('showNotificationUpdates');
openBattlelogHomeInTab(true);
});
// notification button(s) action
chrome.notifications.onButtonClicked.addListener(function (notificationId, buttonIndex) {
switch (buttonIndex) {
case 0:
chrome.notifications.clear('showNotification');
chrome.notifications.clear('showNotificationUpdates');
openBattlelogUpdatesInTab();
break;
}
Expand All @@ -358,33 +402,44 @@
localStorage.iconShowOffline = false;
localStorage.iconShowOnline = true;
localStorage.iconShowIngame = true;
localStorage.notifIsActivated = false;
localStorage.notifFrequency = 5;
localStorage.notifIsSound = true;
localStorage.notifUpdatesIsActivated = false;
localStorage.notifUpdatesFrequency = 5;
localStorage.notifUpdatesIsSound = true;
localStorage.notifFriendsIsActivated = false;
} else if (details.reason == 'update') {
console.log(manifest.name + " updated from v" + details.previousVersion + " to v" + manifest.version);
}
chrome.runtime.sendMessage({do: 'updatebadge'});
chrome.runtime.sendMessage({do: 'update_badge'});
});

// on message update badge
chrome.runtime.onMessage.addListener(function (message, sender, response) {
switch (message.do) {
case 'updatebadge':
case 'update_badge':
updateBadge();
break;
case 'shownotification':
showNotification();
case 'show_updates_notification':
showNotificationUpdates();
break;
case 'shownotification_test':
case 'show_updates_notification_test':
var sound = false;
if (!chrome.notifications) {
return;
}
if (localStorage.notifIsSound == 'true') {
if (localStorage.notifUpdatesIsSound == 'true') {
sound = true;
}
renderNotification(Math.floor((Math.random()*10)+1), sound);
renderUpdatesNotification(Math.floor((Math.random()*10)+1), sound);
break;
case 'show_friends_notification_test':
if (!chrome.notifications) {
return;
}
var statusArray = [
chrome.i18n.getMessage('statusIngame'),
chrome.i18n.getMessage('statusOnline')
];
renderFriendsNotification(Math.floor((Math.random()*10)+1), statusArray[Math.floor(Math.random()*statusArray.length)]);
break;
}
});
Expand Down
2 changes: 1 addition & 1 deletion src/manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "__MSG_extName__",
"description": "__MSG_extDescription__",
"version": "0.1.3",
"version": "0.1.4",
"background": {
"persistent": false,
"scripts": [
Expand Down
Loading