diff --git a/src/argv-files-handler.js b/src/argv-files-handler.js index 82c0894a2..ad2bd1b72 100644 --- a/src/argv-files-handler.js +++ b/src/argv-files-handler.js @@ -1,27 +1,26 @@ -import { app } from 'electron' import fs from 'fs-extra' import addToIpfs from './add-to-ipfs' -export default async function (ctx) { - const handleArgv = async argv => { - for (const arg of argv.slice(1)) { - if (!arg.startsWith('--add')) { - continue - } +export async function argvHandler (argv, ctx) { + let handled = false + + for (const arg of argv.slice(1)) { + if (!arg.startsWith('--add')) { + continue + } - const filename = arg.slice(6) + const filename = arg.slice(6) - if (await fs.pathExists(filename)) { - await addToIpfs(ctx, filename) - } + if (await fs.pathExists(filename)) { + await addToIpfs(ctx, filename) + handled = true } } - // Works for Windows context menu - app.on('second-instance', (_, argv) => { - handleArgv(argv) - }) + return handled +} +export default async function (ctx) { // Checks current proccess - await handleArgv(process.argv) + await argvHandler(process.argv, ctx) } diff --git a/src/index.js b/src/index.js index dc7b0f232..fc477892d 100644 --- a/src/index.js +++ b/src/index.js @@ -16,6 +16,7 @@ import setupAutoUpdater from './auto-updater' import setupTray from './tray' import setupIpfsOnPath from './ipfs-on-path' import setupAnalytics from './analytics' +import setupSecondInstance from './second-instance' // Hide Dock if (app.dock) app.dock.hide() @@ -66,6 +67,7 @@ async function run () { await Promise.all([ setupArgvFilesHandler(ctx), setupAutoLaunch(ctx), + setupSecondInstance(ctx), // Setup global shortcuts setupDownloadHash(ctx), setupTakeScreenshot(ctx), diff --git a/src/protocol-handlers.js b/src/protocol-handlers.js index acf0d25e7..c2b2e1be2 100644 --- a/src/protocol-handlers.js +++ b/src/protocol-handlers.js @@ -3,6 +3,7 @@ import toUri from 'multiaddr-to-uri' function openLink (protocol, part, base) { shell.openExternal(`${base}/${protocol}/${part}`) + return true } function parseAddr (addr) { @@ -18,33 +19,38 @@ async function parseUrl (url, ctx) { } if (url.startsWith('ipfs://')) { - openLink('ipfs', url.slice(7), base) + return openLink('ipfs', url.slice(7), base) } else if (url.startsWith('ipns://')) { - openLink('ipns', url.slice(7), base) + return openLink('ipns', url.slice(7), base) } else if (url.startsWith('dweb:/ipfs/')) { - openLink('ipfs', url.slice(11), base) + return openLink('ipfs', url.slice(11), base) } else if (url.startsWith('dweb:/ipns/')) { - openLink('ipns', url.slice(11), base) + return openLink('ipns', url.slice(11), base) } + + return false +} + +export async function argvHandler (argv, ctx) { + let handled = false + + for (const arg of argv) { + if (await parseUrl(arg, ctx)) { + handled = true + } + } + + return handled } export default function (ctx) { // Handle if the app started running now, and a link // was sent to be handled. - for (const arg of process.argv) { - parseUrl(arg, ctx) - } + argvHandler(process.argv, ctx) // Handle URLs in macOS app.on('open-url', (event, url) => { event.preventDefault() parseUrl(url, ctx) }) - - // Handle URLs on Windows (hopefully on Linux too) - app.on('second-instance', (_, argv) => { - for (const arg of argv) { - parseUrl(arg, ctx) - } - }) } diff --git a/src/second-instance.js b/src/second-instance.js new file mode 100644 index 000000000..e37aa53c0 --- /dev/null +++ b/src/second-instance.js @@ -0,0 +1,17 @@ +import { app } from 'electron' +import { argvHandler as protocolHandler } from './protocol-handlers' +import { argvHandler as filesHandler } from './argv-files-handler' + +export default async function (ctx) { + app.on('second-instance', async (_, argv) => { + if (await protocolHandler(argv, ctx)) { + return + } + + if (await filesHandler(argv, ctx)) { + return + } + + ctx.launchWebUI() + }) +} diff --git a/src/webui/index.js b/src/webui/index.js index 7c2e2d9ad..df4fbafbd 100644 --- a/src/webui/index.js +++ b/src/webui/index.js @@ -67,8 +67,12 @@ export default async function (ctx) { ctx.webui = window ctx.launchWebUI = (url, { focus = true } = {}) => { - logger.info(`[web ui] navigate to ${url}`) - window.webContents.send('updatedPage', url) + if (!url) { + logger.info('[web ui] launching web ui') + } else { + logger.info(`[web ui] navigate to ${url}`) + window.webContents.send('updatedPage', url) + } if (focus) { window.show()