From 8f5a6d718506d1dc0de8e88abbaacb9c99074a2a Mon Sep 17 00:00:00 2001 From: Alexey Skobkin Date: Mon, 24 Nov 2014 18:57:34 +0400 Subject: [PATCH] Clickable notifications (#38) --- chrome_point_plus/js/background.js | 28 +++++++++++++++++++++++++--- chrome_point_plus/js/point-plus.js | 23 ++++++++++++++++++----- chrome_point_plus/manifest.json | 2 +- 3 files changed, 44 insertions(+), 9 deletions(-) diff --git a/chrome_point_plus/js/background.js b/chrome_point_plus/js/background.js index f8b3902..3fcce1a 100644 --- a/chrome_point_plus/js/background.js +++ b/chrome_point_plus/js/background.js @@ -10,16 +10,38 @@ chrome.extension.onMessage.addListener(function(message, sender) { break; case 'showNotification': - console.log(chrome.notifications.create( + chrome.notifications.create( message.notificationId, { type: 'basic', iconUrl: message.avatarUrl, title: message.title, message: message.text, - priority: 0 + priority: 0, + isClickable: true }, function() { /* Error checking goes here */} - )); + ); + + console.log('Showing notification %s', message.notificationId); + break; + + case 'listenNotificationClicks': + // Adding notification click event listener + chrome.notifications.onClicked.addListener(function(notificationId) { + // Detecting notification type + if (notificationId.indexOf('comment_') === 0) { + tab_url = message.protocol + '//' + 'point.im/' + notificationId.replace(/comment_/g, ''); + } else if (notificationId.indexOf('post_') === 0) { + tab_url = message.protocol + '//' + 'point.im/' + notificationId.replace(/post_/g, ''); + } + console.log('Notification %s clicked! Opening new tab: %s', notificationId, tab_url); + + if (tab_url !== undefined) { + chrome.tabs.create({ + url: tab_url + }); + } + }); break; case 'injectJSFile': diff --git a/chrome_point_plus/js/point-plus.js b/chrome_point_plus/js/point-plus.js index fe57b71..c0a91c2 100644 --- a/chrome_point_plus/js/point-plus.js +++ b/chrome_point_plus/js/point-plus.js @@ -281,6 +281,13 @@ $(document).ready(function() { // SSL or plain ws = new WebSocket(((location.protocol == 'https:') ? 'wss' : 'ws') + '://point.im/ws'); console.log('WebSocket created: %O', ws); + + // @todo: унести в опцию + // Adding event listener for notification click + chrome.extension.sendMessage({ + type: 'listenNotificationClicks', + protocol: getProtocol() + }); // Detecting post id if presented var postId = $('#top-post').attr('data-id'); @@ -341,6 +348,7 @@ $(document).ready(function() { 'data-to-comment-id': (wsMessage.to_comment_id != null) ? wsMessage.to_comment_id : '' }); + // @todo: Вынести в отдельную функцию // Loading HTML template $commentTemplate.load(chrome.extension.getURL('includes/comment.html'), function() { // Load complete @@ -348,7 +356,8 @@ $(document).ready(function() { // Date and time of comment var date = new Date(); - + + // @todo: унести наверх // Data for template var userLink = '//' + wsMessage.author + '.point.im/'; var postAuthorLink = $('#top-post .info a').attr('href'); @@ -356,7 +365,7 @@ $(document).ready(function() { var userAvatar = '//point.im/avatar/' + wsMessage.author; var commentLink = '//point.im/' + wsMessage.post_id + '#' + wsMessage.comment_id; var csRfToken = $('.reply-form input[name="csrf_token"').val(); - + // Filling template console.info('Changing data in the comment element'); // Date and time @@ -433,9 +442,9 @@ $(document).ready(function() { console.log('Showing desktop notification'); chrome.extension.sendMessage({ type: 'showNotification', - notificationId: wsMessage.post_id + '_' + wsMessage.comment_id, - avatarUrl: ((location.protocol == 'http:') ? 'http:' : 'https:') + userAvatar + '/80', - title: '@' + wsMessage.author + ' → #' + wsMessage.post_id + '(/' + wsMessage.comment_id + ')', + notificationId: 'comment_' + wsMessage.post_id + '#' + wsMessage.comment_id, + avatarUrl: getProtocol() + userAvatar + '/80', + title: '@' + wsMessage.author + ' #' + wsMessage.post_id + '(/' + wsMessage.comment_id + ')', text: wsMessage.text }); } @@ -518,6 +527,10 @@ $(document).ready(function() { }); }); +function getProtocol() { + return ((location.protocol == 'http:') ? 'http:' : 'https:') +} + function escapeHtml(text) { return text .replace(/&/g, "&") diff --git a/chrome_point_plus/manifest.json b/chrome_point_plus/manifest.json index 58c27d9..9f31a87 100644 --- a/chrome_point_plus/manifest.json +++ b/chrome_point_plus/manifest.json @@ -1,7 +1,7 @@ { "manifest_version": 2, "name": "Point+", - "version": "1.15.1", + "version": "1.17.0", "default_locale": "ru", "author": "__MSG_ext_author__", "homepage_url": "https://bitbucket.org/skobkin/chrome_point_plus",