fix opening A tag (#105)

* fix opening A tag

* remove console.log
This commit is contained in:
Lynn 2017-05-25 09:48:40 -07:00 committed by GitHub
parent 2afce4c10b
commit a9d0d3b0a7
2 changed files with 53 additions and 17 deletions

View File

@ -19,6 +19,8 @@ const apiCmds = apiEnums.cmds;
const apiName = apiEnums.apiName;
const getMediaSources = require('../desktopCapturer/getSources');
const nodeURL = require('url');
// hold ref so doesn't get GC'ed
const local = {
ipcRenderer: ipcRenderer
@ -43,6 +45,25 @@ function createAPI() {
return;
}
// bug in electron is preventing using event 'will-navigate' from working
// in sandboxed environment. https://github.com/electron/electron/issues/8841
// so in the mean time using this code below to block clicking on A tags.
// A tags are allowed if they include href='_blank', this cause 'new-window'
// event to be received which is handled properly in windowMgr.js
window.addEventListener('beforeunload', function(event) {
var newUrl = document.activeElement && document.activeElement.href;
if (newUrl) {
var currHostName = window.location.hostname;
var parsedNewUrl = nodeURL.parse(newUrl);
var parsedNewUrlHostName = parsedNewUrl && parsedNewUrl.hostname;
if (currHostName !== parsedNewUrlHostName) {
/* eslint-disable no-param-reassign */
event.returnValue = 'false';
/* eslint-enable no-param-reassign */
}
}
});
// note: window.open from main window (if in the same domain) will get
// api access. window.open in another domain will be opened in the default
// browser (see: handler for event 'new-window' in windowMgr.js)

View File

@ -178,7 +178,14 @@ function doCreateMainWindow(initialUrl, initialBounds) {
mainWindow.on('closed', destroyAllWindows);
// open external links in default browser - a tag, window.open
// bug in electron is preventing this from working in sandboxed evt...
// https://github.com/electron/electron/issues/8841
mainWindow.webContents.on('will-navigate', function(event, willNavUrl) {
event.preventDefault();
openUrlInDefaultBrower(willNavUrl);
});
// open external links in default browser - a tag with href='_blank' or window.open
mainWindow.webContents.on('new-window', function (event, newWinUrl,
frameName, disposition, newWinOptions) {
let newWinParsedUrl = getParsedUrl(newWinUrl);
@ -187,12 +194,9 @@ function doCreateMainWindow(initialUrl, initialBounds) {
let newWinHost = newWinParsedUrl && newWinParsedUrl.host;
let mainWinHost = mainWinParsedUrl && mainWinParsedUrl.host;
// if host url doesn't match then open in external browser
if (newWinHost !== mainWinHost) {
event.preventDefault();
electron.shell.openExternal(newWinUrl);
} else if (disposition === 'foreground-tab' ||
disposition === 'new-window') {
// only allow window.open to succeed is if coming from same hsot,
// otherwise open in default browser.
if (disposition === 'new-window' && newWinHost === mainWinHost) {
// handle: window.open
if (!frameName) {
@ -266,6 +270,9 @@ function doCreateMainWindow(initialUrl, initialBounds) {
browserWin.on('resize', throttledBoundsChange);
}
});
} else {
event.preventDefault();
openUrlInDefaultBrower(newWinUrl)
}
});
@ -289,17 +296,19 @@ function getMainWindow() {
}
function getWindowSizeAndPosition(window) {
let newPos = window.getPosition();
let newSize = window.getSize();
if (window) {
let newPos = window.getPosition();
let newSize = window.getSize();
if (newPos && newPos.length === 2 &&
newSize && newSize.length === 2) {
return {
x: newPos[0],
y: newPos[1],
width: newSize[0],
height: newSize[1],
};
if (newPos && newPos.length === 2 &&
newSize && newSize.length === 2) {
return {
x: newPos[0],
y: newPos[1],
width: newSize[0],
height: newSize[1],
};
}
}
return null;
@ -369,6 +378,12 @@ function sendChildWinBoundsChange(window) {
}
}
function openUrlInDefaultBrower(urlToOpen) {
if (urlToOpen) {
electron.shell.openExternal(urlToOpen);
}
}
module.exports = {
createMainWindow: createMainWindow,
getMainWindow: getMainWindow,