diff --git a/src/_locales/en/messages.json b/src/_locales/en/messages.json index 07e3981..82f7ebc 100644 --- a/src/_locales/en/messages.json +++ b/src/_locales/en/messages.json @@ -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": { @@ -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" }, @@ -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" }, diff --git a/src/_locales/fr/messages.json b/src/_locales/fr/messages.json index da4ef65..19abe4d 100644 --- a/src/_locales/fr/messages.json +++ b/src/_locales/fr/messages.json @@ -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": { @@ -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" @@ -72,17 +87,17 @@ "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" @@ -90,6 +105,18 @@ "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é" }, diff --git a/src/background.js b/src/background.js index 02ef33a..a9d8191 100644 --- a/src/background.js +++ b/src/background.js @@ -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'; @@ -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'; @@ -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); } }); } @@ -213,12 +238,12 @@ }); } - // 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') }, @@ -226,26 +251,45 @@ }; 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; } @@ -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; } @@ -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; } }); diff --git a/src/manifest.json b/src/manifest.json index 8ea2d23..7a9a67e 100644 --- a/src/manifest.json +++ b/src/manifest.json @@ -1,7 +1,7 @@ { "name": "__MSG_extName__", "description": "__MSG_extDescription__", - "version": "0.1.3", + "version": "0.1.4", "background": { "persistent": false, "scripts": [ diff --git a/src/options.html b/src/options.html index 3753b7d..b3c761b 100644 --- a/src/options.html +++ b/src/options.html @@ -76,36 +76,43 @@

Toolbar icon

Notifications

-
-

Updates

+
+

Updates

+

Displays a desktop notification when you get new updates

- - + +
- - - - - - - minute(s).
- - + +
- +
-
+
+
+

Friends

+

Displays a desktop notification with count of friends

+
+ + +
+
+ +
+
diff --git a/src/options.js b/src/options.js index 7392336..e4ebd8a 100644 --- a/src/options.js +++ b/src/options.js @@ -30,9 +30,10 @@ var inputIconShowOffline = document.getElementById('iconShowOffline'); var inputIconShowOnline = document.getElementById('iconShowOnline'); var inputIconShowIngame = document.getElementById('iconShowIngame'); - var inputNotifIsActivated = document.getElementById('notifIsActivated'); - var inputNotifFrequency = document.getElementById('notifFrequency'); - var inputNotifIsSound = document.getElementById('notifIsSound'); + var inputNotifUpdatesIsActivated = document.getElementById('notifUpdatesIsActivated'); + var inputNotifUpdatesFrequency = document.getElementById('notifUpdatesFrequency'); + var inputNotifUpdatesIsSound = document.getElementById('notifUpdatesIsSound'); + var inputNotifFriendsIsActivated = document.getElementById('notifFriendsIsActivated'); var successMessage = document.getElementById('success_message'); var successTimeout = null; @@ -79,23 +80,29 @@ } else { inputIconShowIngame.checked = (localStorage.getItem('iconShowIngame') === 'true'); } - // notifIsActivated - if (localStorage.getItem('notifIsActivated') === null) { - inputNotifIsActivated.checked = false; + // notifUpdatesIsActivated + if (localStorage.getItem('notifUpdatesIsActivated') === null) { + inputNotifUpdatesIsActivated.checked = false; } else { - inputNotifIsActivated.checked = (localStorage.getItem('notifIsActivated') === 'true'); + inputNotifUpdatesIsActivated.checked = (localStorage.getItem('notifUpdatesIsActivated') === 'true'); } - // notifFrequency - if (localStorage.getItem('notifFrequency') === null) { - inputNotifFrequency.value = '1'; + // notifUpdatesFrequency + if (localStorage.getItem('notifUpdatesFrequency') === null) { + inputNotifUpdatesFrequency.value = '1'; } else { - inputNotifFrequency.value = localStorage.getItem('notifFrequency'); + inputNotifUpdatesFrequency.value = localStorage.getItem('notifUpdatesFrequency'); } - // notifIsSound - if (localStorage.getItem('notifIsSound') === null) { - inputNotifIsSound.checked = true; + // notifUpdatesIsSound + if (localStorage.getItem('notifUpdatesIsSound') === null) { + inputNotifUpdatesIsSound.checked = true; } else { - inputNotifIsSound.checked = (localStorage.getItem('notifIsSound') === 'true'); + inputNotifUpdatesIsSound.checked = (localStorage.getItem('notifUpdatesIsSound') === 'true'); + } + // notifFriendsIsActivated + if (localStorage.getItem('notifFriendsIsActivated') === null) { + inputNotifFriendsIsActivated.checked = false; + } else { + inputNotifFriendsIsActivated.checked = (localStorage.getItem('notifFriendsIsActivated') === 'true'); } } loadOptions(); @@ -107,9 +114,10 @@ localStorage.setItem('iconShowOffline', inputIconShowOffline.checked); localStorage.setItem('iconShowOnline', inputIconShowOnline.checked); localStorage.setItem('iconShowIngame', inputIconShowIngame.checked); - localStorage.setItem('notifIsActivated', inputNotifIsActivated.checked); - localStorage.setItem('notifFrequency', inputNotifFrequency.value); - localStorage.setItem('notifIsSound', inputNotifIsSound.checked); + localStorage.setItem('notifUpdatesIsActivated', inputNotifUpdatesIsActivated.checked); + localStorage.setItem('notifUpdatesFrequency', inputNotifUpdatesFrequency.value); + localStorage.setItem('notifUpdatesIsSound', inputNotifUpdatesIsSound.checked); + localStorage.setItem('notifFriendsIsActivated', inputNotifFriendsIsActivated.checked); // success message clearTimeout(successTimeout); successMessage.classList.add('visible'); @@ -119,7 +127,7 @@ } document.getElementById('game').addEventListener('change', function () { saveOptions(); - chrome.runtime.sendMessage({do: 'updatebadge'}); + chrome.runtime.sendMessage({do: 'update_badge'}); }); document.getElementById('bgPermission').addEventListener('change', function () { var permission = {'permissions': ['background']}; @@ -157,33 +165,44 @@ }); document.getElementById('iconShowOffline').addEventListener('change', function () { saveOptions(); - chrome.runtime.sendMessage({do: 'updatebadge'}); + chrome.runtime.sendMessage({do: 'update_badge'}); }); document.getElementById('iconShowOnline').addEventListener('change', function () { saveOptions(); - chrome.runtime.sendMessage({do: 'updatebadge'}); + chrome.runtime.sendMessage({do: 'update_badge'}); }); document.getElementById('iconShowIngame').addEventListener('change', function () { saveOptions(); - chrome.runtime.sendMessage({do: 'updatebadge'}); + chrome.runtime.sendMessage({do: 'update_badge'}); + }); + document.getElementById('notifUpdatesIsActivated').addEventListener('change', function () { + saveOptions(); + chrome.runtime.sendMessage({do: 'show_updates_notification'}); }); - document.getElementById('notifIsActivated').addEventListener('change', function () { + document.getElementById('notifUpdatesFrequency').addEventListener('change', function () { saveOptions(); - chrome.runtime.sendMessage({do: 'shownotification'}); + chrome.runtime.sendMessage({do: 'show_updates_notification'}); }); - document.getElementById('notifFrequency').addEventListener('change', function () { + document.getElementById('notifUpdatesIsSound').addEventListener('change', function () { saveOptions(); - chrome.runtime.sendMessage({do: 'shownotification'}); + chrome.runtime.sendMessage({do: 'show_updates_notification'}); }); - document.getElementById('notifIsSound').addEventListener('change', function () { + document.getElementById('notifFriendsIsActivated').addEventListener('change', function () { saveOptions(); - chrome.runtime.sendMessage({do: 'shownotification'}); + chrome.runtime.sendMessage({do: 'update_badge'}); }); - // Notification test - document.getElementById('notifTest').addEventListener('click', function () { + // Updates notification test + document.getElementById('notifUpdatesTest').addEventListener('click', function () { + if (chrome.notifications) { + chrome.runtime.sendMessage({do: 'show_updates_notification_test'}); + return; + } + }); + // Friends notification test + document.getElementById('notifFriendsTest').addEventListener('click', function () { if (chrome.notifications) { - chrome.runtime.sendMessage({do: 'shownotification_test'}); + chrome.runtime.sendMessage({do: 'show_friends_notification_test'}); return; } }); @@ -197,7 +216,6 @@ // Extension version in about page var manifest = chrome.runtime.getManifest(); - //console.log(manifest.version); $(function() { $('#version').html(manifest.version); });