Browse Source

Adds an API for rambox services.

pull/523/head
Michael Weimann 8 years ago
parent
commit
9eb234445f
  1. 3
      app/store/ServicesList.js
  2. 51
      app/ux/WebView.js
  3. 28
      resources/js/rambox-service-api.js

3
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
}
]
});

51
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);

28
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');
}
Loading…
Cancel
Save