Skip to content

Commit 12e2ae5

Browse files
committed
experimental popup support
1 parent 3dfc06b commit 12e2ae5

File tree

4 files changed

+66
-21
lines changed

4 files changed

+66
-21
lines changed

js/browserUI.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,8 +167,8 @@ function switchToTab (id, options) {
167167
})
168168
}
169169

170+
/*
170171
webviews.bindEvent('new-window', function (tabId, url, frameName, disposition) {
171-
/* disabled in focus mode */
172172
if (focusMode.enabled()) {
173173
focusMode.warn()
174174
return
@@ -183,6 +183,14 @@ webviews.bindEvent('new-window', function (tabId, url, frameName, disposition) {
183183
openInBackground: disposition === 'background-tab' && !settings.get('openTabsInForeground')
184184
})
185185
})
186+
*/
187+
188+
webviews.bindEvent('did-create-popup', function (tabId, popupId) {
189+
var popupTab = tabs.add()
190+
tabBar.addTab(popupTab)
191+
webviews.add(popupTab, popupId)
192+
switchToTab(popupTab)
193+
})
186194

187195
webviews.bindIPC('close-window', function (tabId, args) {
188196
closeTab(tabId)

js/preload/default.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ function cloneEvent (e) {
1818
setTimeout(function () {
1919
/* Used for swipe gestures */
2020
window.addEventListener('wheel', function (e) {
21+
console.log(e)
2122
ipc.send('wheel-event', cloneEvent(e))
2223
})
2324

js/webviews.js

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ const webviews = {
169169
}
170170
}
171171
},
172-
add: function (tabId) {
172+
add: function (tabId, existingViewId) {
173173
var tabData = tabs.get(tabId)
174174

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

190190
ipc.send('createView', {
191+
existingViewId,
191192
id: tabId,
192193
webPreferencesString: JSON.stringify({
193-
webPreferences: {
194-
nodeIntegration: false,
195-
nodeIntegrationInSubFrames: true,
196-
scrollBounce: true,
197-
safeDialogs: true,
198-
safeDialogsMessage: 'Prevent this page from creating additional dialogs',
199-
preload: __dirname + '/dist/preload.js',
200-
contextIsolation: true,
201-
sandbox: true,
202-
enableRemoteModule: false,
203-
allowPopups: false,
204-
partition: partition || 'persist:webcontent',
205-
enableWebSQL: false,
206-
autoplayPolicy: (settings.get('enableAutoplay') ? 'no-user-gesture-required' : 'user-gesture-required')
207-
}
194+
partition: partition || 'persist:webcontent'
208195
}),
209196
boundsString: JSON.stringify(webviews.getViewBounds()),
210197
events: webviews.events.map(e => e.event).filter((i, idx, arr) => arr.indexOf(i) === idx)

main/viewManager.js

Lines changed: 53 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,34 @@
1+
const BrowserView = electron.BrowserView
2+
13
var viewMap = {} // id: view
24
var viewStateMap = {} // id: view state
35

4-
const BrowserView = electron.BrowserView
6+
var temporaryPopupViews = {} // id: view
7+
8+
const defaultViewWebPreferences = {
9+
nodeIntegration: false,
10+
nodeIntegrationInSubFrames: true,
11+
scrollBounce: true,
12+
safeDialogs: true,
13+
safeDialogsMessage: 'Prevent this page from creating additional dialogs',
14+
preload: __dirname + '/dist/preload.js',
15+
contextIsolation: true,
16+
sandbox: true,
17+
enableRemoteModule: false,
18+
allowPopups: false,
19+
// partition: partition || 'persist:webcontent',
20+
enableWebSQL: false,
21+
autoplayPolicy: (settings.get('enableAutoplay') ? 'no-user-gesture-required' : 'user-gesture-required')
22+
}
523

6-
function createView (id, webPreferencesString, boundsString, events) {
7-
const view = new BrowserView(JSON.parse(webPreferencesString))
24+
function createView (existingViewId, id, webPreferencesString, boundsString, events) {
25+
console.log(existingViewId)
26+
let view
27+
if (existingViewId) {
28+
view = temporaryPopupViews[existingViewId]
29+
} else {
30+
view = new BrowserView({ webPreferences: Object.assign({}, defaultViewWebPreferences, JSON.parse(webPreferencesString)) })
31+
}
832

933
events.forEach(function (event) {
1034
view.webContents.on(event, function (e) {
@@ -29,6 +53,7 @@ function createView (id, webPreferencesString, boundsString, events) {
2953
Workaround for crashes when calling preventDefault() on the new-window event (https://github.com/electron/electron/issues/23859#issuecomment-650270680)
3054
Calling preventDefault also prevents the new-window event from occurring, so create a new event here instead
3155
*/
56+
/*
3257
view.webContents.on('-will-add-new-contents', function (e, url) {
3358
e.preventDefault()
3459
mainWindow.webContents.send('view-event', {
@@ -37,6 +62,30 @@ function createView (id, webPreferencesString, boundsString, events) {
3762
args: [url, '', 'new-window']
3863
})
3964
})
65+
*/
66+
67+
view.webContents.removeAllListeners('-add-new-contents')
68+
69+
view.webContents.on('-add-new-contents', function (e, webContents, disposition, _userGesture, _left, _top, _width, _height, url, frameName, referrer, rawFeatures, postData) {
70+
console.log(arguments)
71+
var view = new BrowserView({ webPreferences: defaultViewWebPreferences, webContents: webContents })
72+
73+
view.setBounds({
74+
x: 0,
75+
y: 0,
76+
width: 500,
77+
height: 500
78+
})
79+
80+
var popupId = Math.random().toString()
81+
temporaryPopupViews[popupId] = view
82+
83+
mainWindow.webContents.send('view-event', {
84+
viewId: id,
85+
event: 'did-create-popup',
86+
args: [popupId]
87+
})
88+
})
4089

4190
view.webContents.on('ipc-message', function (e, channel, data) {
4291
mainWindow.webContents.send('view-ipc', {
@@ -161,7 +210,7 @@ function getViewIDFromWebContents (contents) {
161210
}
162211

163212
ipc.on('createView', function (e, args) {
164-
createView(args.id, args.webPreferencesString, args.boundsString, args.events)
213+
createView(args.existingViewId, args.id, args.webPreferencesString, args.boundsString, args.events)
165214
})
166215

167216
ipc.on('destroyView', function (e, id) {

0 commit comments

Comments
 (0)