From 12e2ae5499e385929e9c6e534218e3896006e75c Mon Sep 17 00:00:00 2001 From: PalmerAL Date: Mon, 10 May 2021 17:39:53 -0500 Subject: [PATCH 1/6] experimental popup support --- js/browserUI.js | 10 +++++++- js/preload/default.js | 1 + js/webviews.js | 19 +++------------ main/viewManager.js | 57 ++++++++++++++++++++++++++++++++++++++++--- 4 files changed, 66 insertions(+), 21 deletions(-) diff --git a/js/browserUI.js b/js/browserUI.js index bc44e478b..1b0bccb9d 100644 --- a/js/browserUI.js +++ b/js/browserUI.js @@ -167,8 +167,8 @@ function switchToTab (id, options) { }) } +/* webviews.bindEvent('new-window', function (tabId, url, frameName, disposition) { - /* disabled in focus mode */ if (focusMode.enabled()) { focusMode.warn() return @@ -183,6 +183,14 @@ webviews.bindEvent('new-window', function (tabId, url, frameName, disposition) { openInBackground: disposition === 'background-tab' && !settings.get('openTabsInForeground') }) }) +*/ + +webviews.bindEvent('did-create-popup', function (tabId, popupId) { + var popupTab = tabs.add() + tabBar.addTab(popupTab) + webviews.add(popupTab, popupId) + switchToTab(popupTab) +}) webviews.bindIPC('close-window', function (tabId, args) { closeTab(tabId) diff --git a/js/preload/default.js b/js/preload/default.js index d502cbe29..fd09e3096 100644 --- a/js/preload/default.js +++ b/js/preload/default.js @@ -18,6 +18,7 @@ function cloneEvent (e) { setTimeout(function () { /* Used for swipe gestures */ window.addEventListener('wheel', function (e) { + console.log(e) ipc.send('wheel-event', cloneEvent(e)) }) diff --git a/js/webviews.js b/js/webviews.js index 0199b2f40..75b29c026 100644 --- a/js/webviews.js +++ b/js/webviews.js @@ -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 @@ -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) diff --git a/main/viewManager.js b/main/viewManager.js index e78a6116b..26b46493a 100644 --- a/main/viewManager.js +++ b/main/viewManager.js @@ -1,10 +1,34 @@ +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 (id, webPreferencesString, boundsString, events) { - const view = new BrowserView(JSON.parse(webPreferencesString)) +function createView (existingViewId, id, webPreferencesString, boundsString, events) { + console.log(existingViewId) + let view + if (existingViewId) { + view = temporaryPopupViews[existingViewId] + } else { + view = new BrowserView({ webPreferences: Object.assign({}, defaultViewWebPreferences, JSON.parse(webPreferencesString)) }) + } events.forEach(function (event) { view.webContents.on(event, function (e) { @@ -29,6 +53,7 @@ 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() mainWindow.webContents.send('view-event', { @@ -37,6 +62,30 @@ function createView (id, webPreferencesString, boundsString, events) { args: [url, '', 'new-window'] }) }) + */ + + 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) { + console.log(arguments) + var view = new BrowserView({ webPreferences: defaultViewWebPreferences, webContents: webContents }) + + view.setBounds({ + x: 0, + y: 0, + width: 500, + height: 500 + }) + + var popupId = Math.random().toString() + temporaryPopupViews[popupId] = view + + mainWindow.webContents.send('view-event', { + viewId: id, + event: 'did-create-popup', + args: [popupId] + }) + }) view.webContents.on('ipc-message', function (e, channel, data) { mainWindow.webContents.send('view-ipc', { @@ -161,7 +210,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) { From f25b51b43023509c80e94ec964f170c8cf230e3a Mon Sep 17 00:00:00 2001 From: PalmerAL Date: Fri, 14 May 2021 16:48:45 -0500 Subject: [PATCH 2/6] fix opening popups from private tabs --- js/browserUI.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/js/browserUI.js b/js/browserUI.js index 1b0bccb9d..b1668fd22 100644 --- a/js/browserUI.js +++ b/js/browserUI.js @@ -186,7 +186,9 @@ webviews.bindEvent('new-window', function (tabId, url, frameName, disposition) { */ webviews.bindEvent('did-create-popup', function (tabId, popupId) { - var popupTab = tabs.add() + var popupTab = tabs.add({ + private: tabs.get(tabId).private + }) tabBar.addTab(popupTab) webviews.add(popupTab, popupId) switchToTab(popupTab) From 00c9182de75dbc488d92e22e69bdf47fffbda816 Mon Sep 17 00:00:00 2001 From: PalmerAL Date: Sun, 6 Jun 2021 17:57:02 -0500 Subject: [PATCH 3/6] fix ctrl+click tab open behavior --- js/browserUI.js | 12 ++++++++++++ main/viewManager.js | 21 +++++++++++++++++---- package.json | 4 ++-- 3 files changed, 31 insertions(+), 6 deletions(-) diff --git a/js/browserUI.js b/js/browserUI.js index b1668fd22..73db8d1dd 100644 --- a/js/browserUI.js +++ b/js/browserUI.js @@ -194,6 +194,18 @@ webviews.bindEvent('did-create-popup', function (tabId, 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: !settings.get('openTabsInForeground') + }) +}) + webviews.bindIPC('close-window', function (tabId, args) { closeTab(tabId) }) diff --git a/main/viewManager.js b/main/viewManager.js index dc7d2060e..3575e7479 100644 --- a/main/viewManager.js +++ b/main/viewManager.js @@ -36,10 +36,6 @@ function createView (existingViewId, id, webPreferencesString, boundsString, eve 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, @@ -64,6 +60,23 @@ function createView (existingViewId, id, webPreferencesString, boundsString, eve }) */ + 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) { diff --git a/package.json b/package.json index 933e0edd6..9b01cf09f 100644 --- a/package.json +++ b/package.json @@ -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": [ @@ -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", From 1ded9888c719f4d81ad78ae2e55396c3f95b838d Mon Sep 17 00:00:00 2001 From: PalmerAL Date: Sun, 6 Jun 2021 18:01:22 -0500 Subject: [PATCH 4/6] fix popup background color --- main/viewManager.js | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/main/viewManager.js b/main/viewManager.js index 3575e7479..ff329ddfb 100644 --- a/main/viewManager.js +++ b/main/viewManager.js @@ -23,9 +23,16 @@ const defaultViewWebPreferences = { function createView (existingViewId, id, webPreferencesString, boundsString, events) { console.log(existingViewId) + + viewStateMap[id] = { loadedInitialURL: false } + let view if (existingViewId) { view = 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)) }) } @@ -174,7 +181,6 @@ function createView (existingViewId, id, webPreferencesString, boundsString, eve view.setBounds(JSON.parse(boundsString)) viewMap[id] = view - viewStateMap[id] = { loadedInitialURL: false } return view } From f308e1d8992d91304aeb53f1c8a8000fc0d5eb63 Mon Sep 17 00:00:00 2001 From: PalmerAL Date: Sun, 6 Jun 2021 18:07:03 -0500 Subject: [PATCH 5/6] cleanup --- js/preload/default.js | 1 - main/viewManager.js | 22 +--------------------- 2 files changed, 1 insertion(+), 22 deletions(-) diff --git a/js/preload/default.js b/js/preload/default.js index fd09e3096..d502cbe29 100644 --- a/js/preload/default.js +++ b/js/preload/default.js @@ -18,7 +18,6 @@ function cloneEvent (e) { setTimeout(function () { /* Used for swipe gestures */ window.addEventListener('wheel', function (e) { - console.log(e) ipc.send('wheel-event', cloneEvent(e)) }) diff --git a/main/viewManager.js b/main/viewManager.js index ff329ddfb..744453f32 100644 --- a/main/viewManager.js +++ b/main/viewManager.js @@ -22,13 +22,12 @@ const defaultViewWebPreferences = { } function createView (existingViewId, id, webPreferencesString, boundsString, events) { - console.log(existingViewId) - viewStateMap[id] = { loadedInitialURL: false } 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') @@ -39,9 +38,6 @@ function createView (existingViewId, id, webPreferencesString, boundsString, eve 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) mainWindow.webContents.send('view-event', { @@ -52,21 +48,6 @@ function createView (existingViewId, id, webPreferencesString, boundsString, eve }) }) - /* - 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() - mainWindow.webContents.send('view-event', { - viewId: id, - event: 'new-window', - args: [url, '', 'new-window'] - }) - }) - */ - view.webContents.setWindowOpenHandler(function (details) { if (details.disposition === 'background-tab') { mainWindow.webContents.send('view-event', { @@ -87,7 +68,6 @@ function createView (existingViewId, id, webPreferencesString, boundsString, eve 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) { - console.log(arguments) var view = new BrowserView({ webPreferences: defaultViewWebPreferences, webContents: webContents }) view.setBounds({ From ba952d456b4804f8d5952a4fd6d6547b75900e6d Mon Sep 17 00:00:00 2001 From: PalmerAL Date: Thu, 10 Jun 2021 14:30:29 -0500 Subject: [PATCH 6/6] cleanup --- js/browserUI.js | 18 ------------------ main/viewManager.js | 7 ------- 2 files changed, 25 deletions(-) diff --git a/js/browserUI.js b/js/browserUI.js index 73db8d1dd..b151943df 100644 --- a/js/browserUI.js +++ b/js/browserUI.js @@ -167,24 +167,6 @@ function switchToTab (id, options) { }) } -/* -webviews.bindEvent('new-window', function (tabId, url, frameName, disposition) { - if (focusMode.enabled()) { - focusMode.warn() - return - } - 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') - }) -}) -*/ - webviews.bindEvent('did-create-popup', function (tabId, popupId) { var popupTab = tabs.add({ private: tabs.get(tabId).private diff --git a/main/viewManager.js b/main/viewManager.js index 744453f32..757020be3 100644 --- a/main/viewManager.js +++ b/main/viewManager.js @@ -70,13 +70,6 @@ function createView (existingViewId, id, webPreferencesString, boundsString, eve 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 }) - view.setBounds({ - x: 0, - y: 0, - width: 500, - height: 500 - }) - var popupId = Math.random().toString() temporaryPopupViews[popupId] = view