Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add experimental popup window support #1608

Merged
merged 7 commits into from Jun 10, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 11 additions & 7 deletions js/browserUI.js
Expand Up @@ -167,20 +167,24 @@ function switchToTab (id, options) {
})
}

webviews.bindEvent('new-window', function (tabId, url, frameName, disposition) {
/* disabled in focus mode */
if (focusMode.enabled()) {
focusMode.warn()
return
}
webviews.bindEvent('did-create-popup', function (tabId, popupId) {
var popupTab = tabs.add({
private: tabs.get(tabId).private
})
tabBar.addTab(popupTab)
webviews.add(popupTab, popupId)
switchToTab(popupTab)
})

webviews.bindEvent('new-tab', function (tabId, url) {
var newTab = tabs.add({
url: url,
private: tabs.get(tabId).private // inherit private status from the current tab
})

addTab(newTab, {
enterEditMode: false,
openInBackground: disposition === 'background-tab' && !settings.get('openTabsInForeground')
openInBackground: !settings.get('openTabsInForeground')
})
})

Expand Down
19 changes: 3 additions & 16 deletions js/webviews.js
Expand Up @@ -169,7 +169,7 @@ const webviews = {
}
}
},
add: function (tabId) {
add: function (tabId, existingViewId) {
var tabData = tabs.get(tabId)

// needs to be called before the view is created to that its listeners can be registered
Expand All @@ -188,23 +188,10 @@ const webviews = {
}

ipc.send('createView', {
existingViewId,
id: tabId,
webPreferencesString: JSON.stringify({
webPreferences: {
nodeIntegration: false,
nodeIntegrationInSubFrames: true,
scrollBounce: true,
safeDialogs: true,
safeDialogsMessage: 'Prevent this page from creating additional dialogs',
preload: __dirname + '/dist/preload.js',
contextIsolation: true,
sandbox: true,
enableRemoteModule: false,
allowPopups: false,
partition: partition || 'persist:webcontent',
enableWebSQL: false,
autoplayPolicy: (settings.get('enableAutoplay') ? 'no-user-gesture-required' : 'user-gesture-required')
}
partition: partition || 'persist:webcontent'
}),
boundsString: JSON.stringify(webviews.getViewBounds()),
events: webviews.events.map(e => e.event).filter((i, idx, arr) => arr.indexOf(i) === idx)
Expand Down
81 changes: 61 additions & 20 deletions main/viewManager.js
@@ -1,21 +1,44 @@
const BrowserView = electron.BrowserView

var viewMap = {} // id: view
var viewStateMap = {} // id: view state

const BrowserView = electron.BrowserView
var temporaryPopupViews = {} // id: view

const defaultViewWebPreferences = {
nodeIntegration: false,
nodeIntegrationInSubFrames: true,
scrollBounce: true,
safeDialogs: true,
safeDialogsMessage: 'Prevent this page from creating additional dialogs',
preload: __dirname + '/dist/preload.js',
contextIsolation: true,
sandbox: true,
enableRemoteModule: false,
allowPopups: false,
// partition: partition || 'persist:webcontent',
enableWebSQL: false,
autoplayPolicy: (settings.get('enableAutoplay') ? 'no-user-gesture-required' : 'user-gesture-required')
}

function createView (existingViewId, id, webPreferencesString, boundsString, events) {
viewStateMap[id] = { loadedInitialURL: false }

function createView (id, webPreferencesString, boundsString, events) {
const view = new BrowserView(JSON.parse(webPreferencesString))
let view
if (existingViewId) {
view = temporaryPopupViews[existingViewId]
delete temporaryPopupViews[existingViewId]

// the initial URL has already been loaded, so set the background color
view.setBackgroundColor('#fff')
viewStateMap[id].loadedInitialURL = true
} else {
view = new BrowserView({ webPreferences: Object.assign({}, defaultViewWebPreferences, JSON.parse(webPreferencesString)) })
}

events.forEach(function (event) {
view.webContents.on(event, function (e) {
/*
new-window is special because its arguments contain a webContents object that can't be serialized and needs to be removed.
*/
var args = Array.prototype.slice.call(arguments).slice(1)
if (event === 'new-window') {
e.preventDefault()
args = args.slice(0, 3)
}

mainWindow.webContents.send('view-event', {
viewId: id,
Expand All @@ -25,16 +48,35 @@ function createView (id, webPreferencesString, boundsString, events) {
})
})

/*
Workaround for crashes when calling preventDefault() on the new-window event (https://github.com/electron/electron/issues/23859#issuecomment-650270680)
Calling preventDefault also prevents the new-window event from occurring, so create a new event here instead
*/
view.webContents.on('-will-add-new-contents', function (e, url) {
e.preventDefault()
view.webContents.setWindowOpenHandler(function (details) {
if (details.disposition === 'background-tab') {
mainWindow.webContents.send('view-event', {
viewId: id,
event: 'new-tab',
args: [details.url]
})
return {
action: 'deny'
}
}

return {
action: 'allow'
}
})

view.webContents.removeAllListeners('-add-new-contents')

view.webContents.on('-add-new-contents', function (e, webContents, disposition, _userGesture, _left, _top, _width, _height, url, frameName, referrer, rawFeatures, postData) {
var view = new BrowserView({ webPreferences: defaultViewWebPreferences, webContents: webContents })

var popupId = Math.random().toString()
temporaryPopupViews[popupId] = view

mainWindow.webContents.send('view-event', {
viewId: id,
event: 'new-window',
args: [url, '', 'new-window']
event: 'did-create-popup',
args: [popupId]
})
})

Expand Down Expand Up @@ -112,7 +154,6 @@ function createView (id, webPreferencesString, boundsString, events) {
view.setBounds(JSON.parse(boundsString))

viewMap[id] = view
viewStateMap[id] = { loadedInitialURL: false }

return view
}
Expand Down Expand Up @@ -175,7 +216,7 @@ function getViewIDFromWebContents (contents) {
}

ipc.on('createView', function (e, args) {
createView(args.id, args.webPreferencesString, args.boundsString, args.events)
createView(args.existingViewId, args.id, args.webPreferencesString, args.boundsString, args.events)
})

ipc.on('destroyView', function (e, id) {
Expand Down
4 changes: 2 additions & 2 deletions package.json
Expand Up @@ -4,7 +4,7 @@
"author": "PalmerAL",
"version": "1.19.2",
"description": "A fast, minimal browser that protects your privacy",
"electronVersion": "13.0.0-beta.27",
"electronVersion": "13.1.1",
"main": "main.build.js",
"standard": {
"globals": [
Expand Down Expand Up @@ -44,7 +44,7 @@
"chokidar": "^3.4.0",
"concurrently": "^5.2.0",
"decomment": "^0.9.0",
"electron": "^13.0.0-beta.27",
"electron": "^13.1.1",
"electron-installer-windows": "^3.0.0",
"electron-packager": "^15.1.0",
"electron-rebuild": "^2.3.2",
Expand Down