🐛 Internal links open in app

This commit is contained in:
akashnimare
2016-06-21 06:14:43 +05:30
parent 24789dc374
commit 33fb06c5c5
4 changed files with 38 additions and 10 deletions

3
.gitignore vendored
View File

@@ -1 +1,2 @@
node_modules
node_modules
npm-debug.log

View File

@@ -3,10 +3,16 @@ const path = require('path');
const electron = require('electron');
const {app, shell} = require('electron');
const tray = require('./tray');
const link = require ('./link_helper');
const {linkIsInternal} = link;
// adds debug features like hotkeys for triggering dev tools and reload
require('electron-debug')();
// Load this url in main window
const targetUrl = "https://zulip.com/login"
// prevent window being garbage collected
let mainWindow;
@@ -27,9 +33,10 @@ function createMainWindow() {
minHeight: 600
});
win.loadURL('https://zulip.com/login');
win.loadURL(targetUrl);
win.on('closed', onClosed);
win.setTitle('Zulip');
// This will stop page to update it's title
win.on('page-title-updated',(e) => {
e.preventDefault();
@@ -55,11 +62,17 @@ app.on('ready', () => {
tray.create(mainWindow);
const page = mainWindow.webContents;
page.on('new-window', (e, url) => {
e.preventDefault();
electron.shell.openExternal(url);
});
page.on('new-window', (event, url) => {
if (mainWindow.useDefaultWindowBehaviour) {
mainWindow.useDefaultWindowBehaviour = false;
return;
}
if (linkIsInternal(targetUrl, url)) {
event.preventDefault();
return mainWindow.loadURL(url);;
}
event.preventDefault();
electron.shell.openExternal(url);
});
});

12
app/link_helper.js Normal file
View File

@@ -0,0 +1,12 @@
const wurl = require('wurl');
// Check the link if it's inetrnal
function linkIsInternal(currentUrl, newUrl) {
var currentDomain = wurl('domain', currentUrl);
var newDomain = wurl('domain', newUrl);
return currentDomain === newDomain;
}
exports = module.exports = {
linkIsInternal
};

View File

@@ -25,12 +25,14 @@
],
"dependencies": {
"electron-debug": "^1.0.0",
"electron-dl": "^0.2.0"
"electron-dl": "^0.2.0",
"wurl": "^2.1.0"
},
"devDependencies": {
"devtron": "^1.1.0",
"electron-packager": "^7.0.0",
"electron-prebuilt": "^1.2.2",
"devtron": "^1.1.0",
"wurl": "^2.1.0",
"xo": "*"
},
"xo": {