diff --git a/main.js b/main.js index 02aebc9..33135d0 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}) @@ -86,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 @@ -232,6 +242,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() @@ -309,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") { @@ -553,3 +574,51 @@ function sessionCreate(webContents, sessionId) { elements[sessionId] = webContents.session elements[sessionId].on('will-download', () => { client.write(sessionId, consts.eventNames.sessionEventWillDownload) }) } + +function registerAppProtocol(client, json) { + + 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 ":///"" + // - 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}`); + + const navinfo = { + path: finalPath + }; + + client.write( + consts.targetIds.app, + consts.eventNames.registerAppProtocolCallback, + navinfo + ); + + callback(navinfo); + + }, (error) => { + + client.write( + consts.targetIds.app, + consts.eventNames.registerAppProtocolCompletion, + { + error: (error) ? error.message : '', + workingDir: json.filePath + } + ); + }); +} diff --git a/src/consts.js b/src/consts.js index 6b00bbb..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", @@ -51,6 +53,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",