From 9eb234445fbe2b51c3c3d10fea4cfc2510632283 Mon Sep 17 00:00:00 2001 From: Michael Weimann Date: Thu, 15 Dec 2016 10:33:49 +0100 Subject: [PATCH] Adds an API for rambox services. --- app/store/ServicesList.js | 3 +- app/ux/WebView.js | 51 +++++++++++++++++++++++++++--- resources/js/rambox-service-api.js | 28 ++++++++++++++++ 3 files changed, 76 insertions(+), 6 deletions(-) create mode 100644 resources/js/rambox-service-api.js diff --git a/app/store/ServicesList.js b/app/store/ServicesList.js index 2d9ae17c..4120d9f5 100644 --- a/app/store/ServicesList.js +++ b/app/store/ServicesList.js @@ -668,7 +668,8 @@ Ext.define('Rambox.store.ServicesList', { description: 'Career-oriented social networking', url: 'https://www.xing.com/messages/conversations', type: 'messaging', - js_unread: '(function() { let originalTitle = document.title; function checkUnread() { let count = null; let notificationElement = document.querySelector(\'[data-update="unread_conversations"]\'); if (notificationElement && notificationElement.style.display !== \'none\') { count = parseInt(notificationElement.textContent.trim(), 10); } updateBadge(count); } function updateBadge(count) { let newTitle = originalTitle; if (count && count >= 1) { newTitle += ` (${count})`; } document.title = newTitle; } setInterval(checkUnread, 3000); checkUnread(); })();' + js_unread: '(function() { let originalTitle = document.title; function checkUnread() { let count = null; let notificationElement = document.querySelector(\'[data-update="unread_conversations"]\'); if (notificationElement && notificationElement.style.display !== \'none\') { count = parseInt(notificationElement.textContent.trim(), 10); } updateBadge(count); } function updateBadge(count) { if (count && count >= 1) { rambox.setUnreadCount(count); } else { rambox.clearUnreadCount(); } } setInterval(checkUnread, 3000); checkUnread(); })();', + dont_update_unread_from_title: true } ] }); diff --git a/app/ux/WebView.js b/app/ux/WebView.js index e681b331..11df8302 100644 --- a/app/ux/WebView.js +++ b/app/ux/WebView.js @@ -158,7 +158,8 @@ Ext.define('Rambox.ux.WebView',{ ,autosize: 'on' ,disablewebsecurity: 'on' ,blinkfeatures: 'ApplicationCache,GlobalCacheStorage' - ,useragent: Ext.getStore('ServicesList').getById(me.record.get('type')).get('userAgent') + ,useragent: Ext.getStore('ServicesList').getById(me.record.get('type')).get('userAgent'), + preload: './resources/js/rambox-service-api.js' } }; @@ -296,14 +297,54 @@ Ext.define('Rambox.ux.WebView',{ webview.executeJavaScript('document.body.scrollTop=0;'); }); - webview.addEventListener("page-title-updated", function(e) { - var count = e.title.match(/\(([^)]+)\)/); // Get text between (...) + webview.addEventListener('ipc-message', function(event) { + var channel = event.channel; + switch (channel) { + case 'rambox.setUnreadCount': + handleSetUnreadCount(event); + break; + case 'rambox.clearUnreadCount': + handleClearUnreadCount(event); + break; + } + + /** + * Handles 'rambox.clearUnreadCount' messages. + * Clears the unread count. + */ + function handleClearUnreadCount() { + me.tab.setBadgeText(''); + } + + /** + * Handles 'rambox.setUnreadCount' messages. + * Sets the badge text if the event contains an integer as first argument. + * + * @param event + */ + function handleSetUnreadCount(event) { + if (Array.isArray(event.args) === true && event.args.length > 0) { + var count = event.args[0]; + if (count === parseInt(count, 10)) { + me.tab.setBadgeText(Rambox.util.Format.formatNumber(count)); + } + } + } + }); + + /** + * Register page title update event listener only for services that don't prevent it by setting 'dont_update_unread_from_title' to true. + */ + if (Ext.getStore('ServicesList').getById(me.record.get('type')).get('dont_update_unread_from_title') !== true) { + webview.addEventListener("page-title-updated", function(e) { + var count = e.title.match(/\(([^)]+)\)/); // Get text between (...) count = count ? count[1] : '0'; count = count === '•' ? count : Ext.isArray(count.match(/\d+/g)) ? count.match(/\d+/g).join("") : count.match(/\d+/g); // Some services have special characters. Example: (•) count = count === null ? '0' : count; - me.tab.setBadgeText(Rambox.util.Format.formatNumber(count)); - }); + me.tab.setBadgeText(Rambox.util.Format.formatNumber(count)); + }); + } webview.addEventListener('did-get-redirect-request', function( e ) { if ( e.isMainFrame ) webview.loadURL(e.newURL); diff --git a/resources/js/rambox-service-api.js b/resources/js/rambox-service-api.js new file mode 100644 index 00000000..c37517dd --- /dev/null +++ b/resources/js/rambox-service-api.js @@ -0,0 +1,28 @@ +/** + * This file is loaded in the service web views to provide a Rambox API. + */ + +const { ipcRenderer } = require('electron'); + +/** + * Make the Rambox API available via a global "rambox" variable. + * + * @type {{}} + */ +window.rambox = {}; + +/** + * Sets the unraed count of the tab. + * + * @param {*} count The unread count + */ +window.rambox.setUnreadCount = function(count) { + ipcRenderer.sendToHost('rambox.setUnreadCount', count); +}; + +/** + * Clears the unread count. + */ +window.rambox.clearUnreadCount = function() { + ipcRenderer.sendToHost('rambox.clearUnreadCount'); +}