From f01e735abc154b5263cb607adc7137c973ea9659 Mon Sep 17 00:00:00 2001 From: Charlie Blevins Date: Sat, 27 Apr 2019 18:30:27 -0400 Subject: [PATCH 1/5] add register protocol ability --- main.js | 39 ++++++++++++++++++++++++++++++++++++++- src/consts.js | 3 +++ 2 files changed, 41 insertions(+), 1 deletion(-) diff --git a/main.js b/main.js index 02aebc9..9670764 100644 --- a/main.js +++ b/main.js @@ -1,7 +1,8 @@ 'use strict' const electron = require('electron') -const {app, BrowserWindow, ipcMain, Menu, MenuItem, Tray, dialog, Notification} = electron +const path = require('path') +const {app, BrowserWindow, ipcMain, Menu, MenuItem, Tray, dialog, Notification, protocol} = electron const consts = require('./src/consts.js') const client = require('./src/client.js').init() const rl = require('readline').createInterface({input: client.socket}) @@ -232,6 +233,12 @@ app.on('ready',() => { executeCallback(consts.callbackNames.webContentsLogin, json, [json.username, json.password]); break; + // Protocol + case consts.eventNames.protocolCmdRegisterAppProtocol: + registerAppProtocol(client, json) + break; + + // Window case consts.eventNames.windowCmdBlur: elements[json.targetID].blur() @@ -553,3 +560,33 @@ function sessionCreate(webContents, sessionId) { elements[sessionId] = webContents.session elements[sessionId].on('will-download', () => { client.write(sessionId, consts.eventNames.sessionEventWillDownload) }) } + +function registerAppProtocol(client, json) { + + protocol.registerFileProtocol('app', (request, callback) => { + + // string after "app:///"" + const url = request.url.substr(7) + const finalPath = path.normalize(`${json.filePath}/${url}`) + + client.write( + consts.targetIds.app, + consts.eventNames.registerAppProtocolCallback, + { finalPath: finalPath } + ); + + callback({ path: finalPath }) + + }, (error) => { + + client.write( + consts.targetIds.app, + consts.eventNames.registerAppProtocolCompletion, + { + error: (error) ? error.message : '', + workingDir: json.filePath + } + ); + //if (error) console.error('Failed to register app:// protocol') + }); +} \ No newline at end of file diff --git a/src/consts.js b/src/consts.js index 6b00bbb..583c48b 100644 --- a/src/consts.js +++ b/src/consts.js @@ -51,6 +51,9 @@ module.exports = { notificationEventCreated: "notification.event.created", notificationEventReplied: "notification.event.replied", notificationEventShown: "notification.event.shown", + protocolCmdRegisterAppProtocol: "protocol.cmd.register.app", + registerAppProtocolCallback: 'protocol.register.callback', + registerAppProtocolCompletion: 'protocol.event.register.completion', sessionCmdClearCache: "session.cmd.clear.cache", sessionEventClearedCache: "session.event.cleared.cache", sessionEventWillDownload: "session.event.will.download", From f27d5c89dd301047b7e57191ee9b2938df714898 Mon Sep 17 00:00:00 2001 From: Charlie Blevins Date: Sun, 28 Apr 2019 12:56:12 -0400 Subject: [PATCH 2/5] throw off hash value - electron cant find file otherwise --- main.js | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/main.js b/main.js index 9670764..e2b626a 100644 --- a/main.js +++ b/main.js @@ -565,17 +565,26 @@ function registerAppProtocol(client, json) { protocol.registerFileProtocol('app', (request, callback) => { + // get value before "#" - for some reason electron cant + // find the file if hash value is passed as part of the path. + const parts = request.url.split('#'); + // string after "app:///"" - const url = request.url.substr(7) - const finalPath = path.normalize(`${json.filePath}/${url}`) + const url = parts[0].substr(7); + + const finalPath = path.normalize(`${json.filePath}/${url}`); + + const navinfo = { + path: finalPath + }; client.write( consts.targetIds.app, consts.eventNames.registerAppProtocolCallback, - { finalPath: finalPath } + navinfo ); - callback({ path: finalPath }) + callback(navinfo); }, (error) => { @@ -587,6 +596,5 @@ function registerAppProtocol(client, json) { workingDir: json.filePath } ); - //if (error) console.error('Failed to register app:// protocol') }); } \ No newline at end of file From 081a49839c294c53d42615ec4c60e00238c2083d Mon Sep 17 00:00:00 2001 From: Charlie Blevins Date: Sun, 28 Apr 2019 18:21:10 -0400 Subject: [PATCH 3/5] get appData path --- main.js | 9 +++++++++ src/consts.js | 2 ++ 2 files changed, 11 insertions(+) diff --git a/main.js b/main.js index e2b626a..9d2a483 100644 --- a/main.js +++ b/main.js @@ -87,6 +87,15 @@ app.on('ready',() => { // App case consts.eventNames.appCmdQuit: app.quit(); + + case consts.eventNames.appCmdGetPath: + client.write( + consts.targetIds.app, + consts.eventNames.appEventGetPathResult, + { + pathName: (json.pathName) ? app.getPath(json.pathName) : '' + } + ); break; // Dock diff --git a/src/consts.js b/src/consts.js index 583c48b..d0d3a82 100644 --- a/src/consts.js +++ b/src/consts.js @@ -7,6 +7,8 @@ module.exports = { }, eventNames: { appCmdQuit: "app.cmd.quit", + appCmdGetPath: "app.cmd.get.path", + appEventGetPathResult: "app.event.get.path.result", appEventReady: "app.event.ready", displayEventAdded: "display.event.added", displayEventMetricsChanged: "display.event.metrics.changed", From 95b18ca22e2f746a5cf4ec2c30f7cd1fea90bf90 Mon Sep 17 00:00:00 2001 From: Charlie Blevins Date: Fri, 17 May 2019 19:05:33 -0400 Subject: [PATCH 4/5] use ? query param rather than hash value --- main.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/main.js b/main.js index 9d2a483..9e8ee9e 100644 --- a/main.js +++ b/main.js @@ -574,9 +574,9 @@ function registerAppProtocol(client, json) { protocol.registerFileProtocol('app', (request, callback) => { - // get value before "#" - for some reason electron cant - // find the file if hash value is passed as part of the path. - const parts = request.url.split('#'); + // get value before "?" - for some reason electron cant + // find the file if ?search value is passed as part of the path. + const parts = request.url.split('?'); // string after "app:///"" const url = parts[0].substr(7); From e73220e5f91be529352e38b3581bb109186ad927 Mon Sep 17 00:00:00 2001 From: Charlie Blevins Date: Thu, 30 May 2019 22:24:30 -0400 Subject: [PATCH 5/5] Unstable. in the middle of adding custom url scheme handling --- main.js | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/main.js b/main.js index 9e8ee9e..33135d0 100644 --- a/main.js +++ b/main.js @@ -325,6 +325,11 @@ app.on('ready',() => { }) }); +app.on('open-url', function (event, url) { + event.preventDefault(); + console.log('open-url event: ', url); +}); + // menuCreate creates a new menu function menuCreate(menu) { if (typeof menu !== "undefined") { @@ -572,14 +577,24 @@ function sessionCreate(webContents, sessionId) { function registerAppProtocol(client, json) { - protocol.registerFileProtocol('app', (request, callback) => { + const scheme = 'com.logtransformer.app'; + + console.log('Registering app protocol "' + scheme +'"...'); + + const ret = app.setAsDefaultProtocolClient(scheme); + console.log('setAsDefaultProtocolClient() returned:: ', JSON.stringify(ret)); + + protocol.registerFileProtocol(scheme, (request, callback) => { + console.log('Received request to ' + scheme + ':: ', request.url); // get value before "?" - for some reason electron cant // find the file if ?search value is passed as part of the path. const parts = request.url.split('?'); - // string after "app:///"" - const url = parts[0].substr(7); + // string after ":///"" + // - 1 to account for 0-index + // + 3 to account for "://" + const url = parts[0].substr((scheme.length - 1) + 3); const finalPath = path.normalize(`${json.filePath}/${url}`); @@ -606,4 +621,4 @@ function registerAppProtocol(client, json) { } ); }); -} \ No newline at end of file +}