From 2c25c7ed55caf1c3f92520cbff220a11aa92f98c Mon Sep 17 00:00:00 2001 From: john dev Date: Fri, 6 Apr 2018 14:33:02 +0200 Subject: [PATCH 1/6] added bound object as payload to tray events added bound object as payload to tray events this commit is tested in contrast to the last one and works as expected --- main.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/main.js b/main.js index 92f887f..f30f0a3 100644 --- a/main.js +++ b/main.js @@ -352,9 +352,9 @@ function trayCreate(json) { if (typeof json.trayOptions.tooltip !== "undefined") { elements[json.targetID].setToolTip(json.trayOptions.tooltip); } - elements[json.targetID].on('click', () => { client.write(json.targetID, consts.eventNames.trayEventClicked) }) - elements[json.targetID].on('double-click', () => { client.write(json.targetID, consts.eventNames.trayEventDoubleClicked) }) - elements[json.targetID].on('right-click', () => { client.write(json.targetID, consts.eventNames.trayEventRightClicked) }) + elements[json.targetID].on('click', (index, event) => { client.write(json.targetID, consts.eventNames.trayEventClicked, {"bounds":{x:event.x, y:event.y,width:event.width,height:event.height}})}) + elements[json.targetID].on('double-click', (index, event) => { client.write(json.targetID, consts.eventNames.trayEventDoubleClicked, {"bounds":{x:event.x, y:event.y,width:event.width,height:event.height}})}) + elements[json.targetID].on('right-click', (index, event) => { client.write(json.targetID, consts.eventNames.trayEventRightClicked, {"bounds":{x:event.x, y:event.y,width:event.width,height:event.height}})}) client.write(json.targetID, consts.eventNames.trayEventCreated) } From 34ad957edf4cf05c2b3046df520e98bc5c56f977 Mon Sep 17 00:00:00 2001 From: Quentin Renard Date: Sat, 21 Apr 2018 11:46:35 +0200 Subject: [PATCH 2/6] Added webContents login callback system --- main.js | 35 ++++++++++++++++++++++++++++++++--- src/consts.js | 5 +++++ 2 files changed, 37 insertions(+), 3 deletions(-) diff --git a/main.js b/main.js index f30f0a3..37027f3 100644 --- a/main.js +++ b/main.js @@ -6,9 +6,11 @@ const consts = require('./src/consts.js') const client = require('./src/client.js').init() const rl = require('readline').createInterface({input: client.socket}) -let elements = {} -let menus = {} -let quittingApp = false +let callbacks = {}; +let counters = {}; +let elements = {}; +let menus = {}; +let quittingApp = false; // Command line switches let idx = 3; @@ -206,6 +208,11 @@ app.on('ready',() => { client.write(json.targetID, consts.eventNames.trayEventImageSet) break; + // Web contents + case consts.eventNames.webContentsEventLoginCallback: + executeCallback(consts.callbackNames.webContentsLogin, json, [json.username, json.password]); + break; + // Window case consts.eventNames.windowCmdBlur: elements[json.targetID].blur() @@ -452,6 +459,10 @@ function windowCreate(json) { oldUrl: oldUrl }) }) + elements[json.targetID].webContents.on('login', (event, request, authInfo, callback) => { + event.preventDefault(); + registerCallback(json, consts.callbackNames.webContentsLogin, {authInfo: authInfo, request: request}, consts.eventNames.webContentsEventLogin, callback); + }) elements[json.targetID].webContents.on('will-navigate', (event, url) => { client.write(json.targetID, consts.eventNames.windowEventWillNavigate, { url: url @@ -459,6 +470,24 @@ function windowCreate(json) { }) } +function registerCallback(json, k, e, n, c) { + if (typeof counters[k] === "undefined") { + counters[k] = 1; + } + e.callbackId = String(counters[k]++); + if (typeof callbacks[k] === "undefined") { + callbacks[k] = {}; + } + callbacks[k][e.callbackId] = c; + client.write(json.targetID, n, e); +} + +function executeCallback(k, json, args) { + if (typeof callbacks[k][json.callbackId] !== "undefined") { + callbacks[k][json.callbackId].apply(null, args); + } +} + function sessionCreate(webContents, sessionId) { elements[sessionId] = webContents.session elements[sessionId].on('will-download', () => { client.write(sessionId, consts.eventNames.sessionEventWillDownload) }) diff --git a/src/consts.js b/src/consts.js index 30ead32..290827b 100644 --- a/src/consts.js +++ b/src/consts.js @@ -1,6 +1,9 @@ 'use strict' module.exports = { + callbackNames: { + webContentsLogin: "web.contents.login" + }, eventNames: { appCmdQuit: "app.cmd.quit", appEventReady: "app.event.ready", @@ -67,6 +70,8 @@ module.exports = { trayEventDoubleClicked: "tray.event.double.clicked", trayEventImageSet: "tray.event.image.set", trayEventRightClicked: "tray.event.right.clicked", + webContentsEventLogin: "web.contents.event.login", + webContentsEventLoginCallback: "web.contents.event.login.callback", windowCmdBlur: "window.cmd.blur", windowCmdCenter: "window.cmd.center", windowCmdClose: "window.cmd.close", From 24156e11c6efa26cb9464baf15e8d3d1003ac2f4 Mon Sep 17 00:00:00 2001 From: Quentin Renard Date: Sat, 21 Apr 2018 10:36:16 +0100 Subject: [PATCH 3/6] Fix MaxListenersExceededWarning --- main.js | 48 ++++++++++++++++++++++++++++++------------------ src/consts.js | 3 ++- 2 files changed, 32 insertions(+), 19 deletions(-) diff --git a/main.js b/main.js index 37027f3..5c8d59a 100644 --- a/main.js +++ b/main.js @@ -403,13 +403,10 @@ function windowCreate(json) { elements[json.targetID].webContents.executeJavaScript( `const {ipcRenderer} = require('electron') const {dialog} = require('electron').remote - ipcRenderer.on('`+ consts.eventNames.ipcCmdLog+`', function(event, message) { - console.log(message) - }) - var onMessageOnce = false; var astilectron = { + onMessageOnce: false, onMessage: function(callback) { - if (onMessageOnce) { + if (astilectron.onMessageOnce) { return } ipcRenderer.on('`+ consts.eventNames.ipcCmdMessage +`', function(event, message) { @@ -420,20 +417,29 @@ function windowCreate(json) { ipcRenderer.send('`+ consts.eventNames.ipcEventMessageCallback +`', e) } }) - onMessageOnce = true + astilectron.onMessageOnce = true }, - callbackIdCounter: 1, - sendMessage: function(message, callback) { - let e = {message: message, targetID: '`+ json.targetID +`'} - if (typeof callback !== "undefined") { - e.callbackId = String(astilectron.callbackIdCounter++) - ipcRenderer.on('`+ consts.eventNames.ipcCmdMessageCallback +`', function(event, message) { - if (message.callbackId === e.callbackId) { - callback(message.message) - } - }); + callbacks: {}, + counters: {}, + registerCallback: function(k, e, c, n) { + e.targetID = '`+ json.targetID +`'; + if (typeof astilectron.counters[k] === "undefined") { + astilectron.counters[k] = 1; + } + e.callbackId = String(astilectron.counters[k]++); + if (typeof astilectron.callbacks[k] === "undefined") { + astilectron.callbacks[k] = {}; + } + astilectron.callbacks[k][e.callbackId] = c; + ipcRenderer.send(n, e); + }, + executeCallback: function(k, message, args) { + if (typeof astilectron.callbacks[k][message.callbackId] !== "undefined") { + astilectron.callbacks[k][message.callbackId].apply(null, args); } - ipcRenderer.send('`+ consts.eventNames.ipcEventMessage +`', e) + }, + sendMessage: function(message, callback) { + astilectron.registerCallback('` + consts.callbackNames.webContentsMessage + `', {message: message}, callback, '`+ consts.eventNames.ipcEventMessage +`'); }, showErrorBox: function(title, content) { dialog.showErrorBox(title, content) @@ -447,7 +453,13 @@ function windowCreate(json) { showSaveDialog: function(options, callback) { dialog.showSaveDialog(null, options, callback) } - } + }; + ipcRenderer.on('`+ consts.eventNames.ipcCmdMessageCallback +`', function(event, message) { + astilectron.executeCallback('` + consts.callbackNames.webContentsMessage + `', message, [message.message]); + }); + ipcRenderer.on('`+ consts.eventNames.ipcCmdLog+`', function(event, message) { + console.log(message) + }) document.dispatchEvent(new Event('astilectron-ready'))` ) sessionCreate(elements[json.targetID].webContents, json.sessionId) diff --git a/src/consts.js b/src/consts.js index 290827b..a3cd3be 100644 --- a/src/consts.js +++ b/src/consts.js @@ -2,7 +2,8 @@ module.exports = { callbackNames: { - webContentsLogin: "web.contents.login" + webContentsLogin: "web.contents.login", + webContentsMessage: "web.contents.message", }, eventNames: { appCmdQuit: "app.cmd.quit", From 014232dd90658f76f34420eaab041ddf275b6dce Mon Sep 17 00:00:00 2001 From: Quentin Renard Date: Sat, 16 Jun 2018 16:31:49 +0200 Subject: [PATCH 4/6] Added hideOnClose window attribute --- main.js | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/main.js b/main.js index 5c8d59a..c937f84 100644 --- a/main.js +++ b/main.js @@ -379,9 +379,14 @@ function windowCreate(json) { return } } - if (json.windowOptions.minimizeOnClose && !quittingApp) { - e.preventDefault(); - elements[json.targetID].minimize(); + if (!quittingApp) { + if (json.windowOptions.minimizeOnClose) { + e.preventDefault(); + elements[json.targetID].minimize(); + } else if (json.windowOptions.hideOnClose) { + e.preventDefault(); + elements[json.targetID].hide(); + } } }) elements[json.targetID].on('closed', () => { From 70ba8eba3412d9beeab0161677177783d3ff82b9 Mon Sep 17 00:00:00 2001 From: liasica Date: Wed, 27 Jun 2018 16:30:00 +0800 Subject: [PATCH 5/6] When page load failed, retry reload page. --- main.js | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/main.js b/main.js index c937f84..54ea840 100644 --- a/main.js +++ b/main.js @@ -11,6 +11,7 @@ let counters = {}; let elements = {}; let menus = {}; let quittingApp = false; +let reload = null; // Command line switches let idx = 3; @@ -404,6 +405,11 @@ function windowCreate(json) { elements[json.targetID].on('show', () => { client.write(json.targetID, consts.eventNames.windowEventShow) }) elements[json.targetID].on('unmaximize', () => { client.write(json.targetID, consts.eventNames.windowEventUnmaximize) }) elements[json.targetID].on('unresponsive', () => { client.write(json.targetID, consts.eventNames.windowEventUnresponsive) }) + elements[json.targetID].webContents.on('did-fail-load', () => { + reload = setInterval(() => { + elements[json.targetID].reload(); + }, 3000) + }) elements[json.targetID].webContents.on('did-finish-load', () => { elements[json.targetID].webContents.executeJavaScript( `const {ipcRenderer} = require('electron') From 5c25d8a7ba2c48ba85060d7a262660eb8b908143 Mon Sep 17 00:00:00 2001 From: liasica Date: Wed, 27 Jun 2018 16:38:18 +0800 Subject: [PATCH 6/6] When page load sucessed, clear reload interval. --- main.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/main.js b/main.js index 54ea840..bb106ae 100644 --- a/main.js +++ b/main.js @@ -411,6 +411,9 @@ function windowCreate(json) { }, 3000) }) elements[json.targetID].webContents.on('did-finish-load', () => { + if (reload !== null) { + clearInterval(reload); + } elements[json.targetID].webContents.executeJavaScript( `const {ipcRenderer} = require('electron') const {dialog} = require('electron').remote