From 9243f6689fe1cacc2311ee009bd96b705b32d8ad Mon Sep 17 00:00:00 2001 From: Lex Neva Date: Sat, 6 Aug 2016 14:03:53 -0400 Subject: [PATCH] add "copy link location" and "--internal-urls " features (#230) * add arg to specify pattern for urls to be considered internal * add 'copy link location' to context menu * fix lint --- app/src/components/contextMenu/contextMenu.js | 14 +++++++++++++- app/src/components/mainWindow/mainWindow.js | 2 +- app/src/helpers/helpers.js | 7 ++++++- src/build/buildApp.js | 3 ++- src/cli.js | 1 + src/options/optionsMain.js | 3 ++- 6 files changed, 25 insertions(+), 5 deletions(-) diff --git a/app/src/components/contextMenu/contextMenu.js b/app/src/components/contextMenu/contextMenu.js index c555493..c3c91b1 100644 --- a/app/src/components/contextMenu/contextMenu.js +++ b/app/src/components/contextMenu/contextMenu.js @@ -1,4 +1,4 @@ -import {Menu, ipcMain, shell, BrowserWindow} from 'electron'; +import {Menu, ipcMain, shell, clipboard, BrowserWindow} from 'electron'; function initContextMenu(mainWindow) { ipcMain.on('contextMenuOpened', (event, targetHref) => { @@ -20,6 +20,18 @@ function initContextMenu(mainWindow) { return; } + mainWindow.useDefaultWindowBehaviour = true; + mainWindow.webContents.send('contextMenuClosed'); + } + }, + { + label: 'Copy link location', + click: () => { + if (targetHref) { + clipboard.writeText(targetHref); + return; + } + mainWindow.useDefaultWindowBehaviour = true; mainWindow.webContents.send('contextMenuClosed'); } diff --git a/app/src/components/mainWindow/mainWindow.js b/app/src/components/mainWindow/mainWindow.js index c931776..fcfac02 100644 --- a/app/src/components/mainWindow/mainWindow.js +++ b/app/src/components/mainWindow/mainWindow.js @@ -159,7 +159,7 @@ function createMainWindow(options, onAppQuit, setDockBadge) { return; } - if (linkIsInternal(options.targetUrl, urlToGo)) { + if (linkIsInternal(options.targetUrl, urlToGo, options.internalUrls)) { return; } event.preventDefault(); diff --git a/app/src/helpers/helpers.js b/app/src/helpers/helpers.js index c5ff517..aed6acf 100644 --- a/app/src/helpers/helpers.js +++ b/app/src/helpers/helpers.js @@ -17,7 +17,12 @@ function isWindows() { return os.platform() === 'win32'; } -function linkIsInternal(currentUrl, newUrl) { +function linkIsInternal(currentUrl, newUrl, internalUrlRegex) { + if (internalUrlRegex) { + var regex = RegExp(internalUrlRegex); + return regex.test(newUrl); + } + var currentDomain = wurl('domain', currentUrl); var newDomain = wurl('domain', newUrl); return currentDomain === newDomain; diff --git a/src/build/buildApp.js b/src/build/buildApp.js index b73d11d..6fd0dd7 100644 --- a/src/build/buildApp.js +++ b/src/build/buildApp.js @@ -115,7 +115,8 @@ function selectAppArgs(options) { maximize: options.maximize, disableContextMenu: options.disableContextMenu, disableDevTools: options.disableDevTools, - zoom: options.zoom + zoom: options.zoom, + internalUrls: options.internalUrls }; } diff --git a/src/cli.js b/src/cli.js index e4fe86c..8e3cc77 100755 --- a/src/cli.js +++ b/src/cli.js @@ -51,6 +51,7 @@ if (require.main === module) { .option('--disable-context-menu', 'disable the context menu') .option('--disable-dev-tools', 'disable developer tools') .option('--zoom ', 'default zoom factor to use when the app is opened, defaults to 1.0', parseFloat) + .option('--internal-urls ', 'regular expression of URLs to consider "internal"; all other URLs will be opened in an external browser. (default: URLs on same second-level domain as app)') .parse(process.argv); if (!process.argv.slice(2).length) { diff --git a/src/options/optionsMain.js b/src/options/optionsMain.js index 59436af..d0132c8 100644 --- a/src/options/optionsMain.js +++ b/src/options/optionsMain.js @@ -66,7 +66,8 @@ function optionsFactory(inpOptions, callback) { disableDevTools: inpOptions.disableDevTools, // workaround for electron-packager#375 tmpdir: false, - zoom: inpOptions.zoom || 1.0 + zoom: inpOptions.zoom || 1.0, + internalUrls: inpOptions.internalUrls || null }; if (options.verbose) {