fix: move window.open handlers from BrowserWindow to WebContents
This commit is contained in:
parent
23de301955
commit
9ff7807be8
2 changed files with 40 additions and 38 deletions
|
@ -3,7 +3,6 @@
|
||||||
const electron = require('electron')
|
const electron = require('electron')
|
||||||
const { WebContentsView, TopLevelWindow } = electron
|
const { WebContentsView, TopLevelWindow } = electron
|
||||||
const { BrowserWindow } = process.atomBinding('window')
|
const { BrowserWindow } = process.atomBinding('window')
|
||||||
const ipcMain = require('@electron/internal/browser/ipc-main-internal')
|
|
||||||
|
|
||||||
Object.setPrototypeOf(BrowserWindow.prototype, TopLevelWindow.prototype)
|
Object.setPrototypeOf(BrowserWindow.prototype, TopLevelWindow.prototype)
|
||||||
|
|
||||||
|
@ -26,43 +25,6 @@ BrowserWindow.prototype._init = function () {
|
||||||
nativeSetBounds.call(this, bounds, ...opts)
|
nativeSetBounds.call(this, bounds, ...opts)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Make new windows requested by links behave like "window.open"
|
|
||||||
this.webContents.on('-new-window', (event, url, frameName, disposition,
|
|
||||||
additionalFeatures, postData,
|
|
||||||
referrer) => {
|
|
||||||
const options = {
|
|
||||||
show: true,
|
|
||||||
width: 800,
|
|
||||||
height: 600
|
|
||||||
}
|
|
||||||
ipcMain.emit('ELECTRON_GUEST_WINDOW_MANAGER_INTERNAL_WINDOW_OPEN',
|
|
||||||
event, url, referrer, frameName, disposition,
|
|
||||||
options, additionalFeatures, postData)
|
|
||||||
})
|
|
||||||
|
|
||||||
// Create a new browser window for the native implementation of
|
|
||||||
// "window.open", used in sandbox and nativeWindowOpen mode
|
|
||||||
this.webContents.on('-add-new-contents', (event, webContents, disposition,
|
|
||||||
userGesture, left, top, width, height, url, frameName) => {
|
|
||||||
if ((disposition !== 'foreground-tab' && disposition !== 'new-window' &&
|
|
||||||
disposition !== 'background-tab')) {
|
|
||||||
event.preventDefault()
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
const options = {
|
|
||||||
show: true,
|
|
||||||
x: left,
|
|
||||||
y: top,
|
|
||||||
width: width || 800,
|
|
||||||
height: height || 600,
|
|
||||||
webContents: webContents
|
|
||||||
}
|
|
||||||
const referrer = { url: '', policy: 'default' }
|
|
||||||
ipcMain.emit('ELECTRON_GUEST_WINDOW_MANAGER_INTERNAL_WINDOW_OPEN',
|
|
||||||
event, url, referrer, frameName, disposition, options)
|
|
||||||
})
|
|
||||||
|
|
||||||
// window.resizeTo(...)
|
// window.resizeTo(...)
|
||||||
// window.moveTo(...)
|
// window.moveTo(...)
|
||||||
this.webContents.on('move', (event, size) => {
|
this.webContents.on('move', (event, size) => {
|
||||||
|
|
|
@ -376,6 +376,46 @@ WebContents.prototype._init = function () {
|
||||||
this.reload()
|
this.reload()
|
||||||
})
|
})
|
||||||
|
|
||||||
|
// Handle window.open for BrowserWindow and BrowserView.
|
||||||
|
if (['browserview', 'window'].includes(this.getType())) {
|
||||||
|
// Make new windows requested by links behave like "window.open".
|
||||||
|
this.on('-new-window', (event, url, frameName, disposition,
|
||||||
|
additionalFeatures, postData,
|
||||||
|
referrer) => {
|
||||||
|
const options = {
|
||||||
|
show: true,
|
||||||
|
width: 800,
|
||||||
|
height: 600
|
||||||
|
}
|
||||||
|
ipcMainInternal.emit('ELECTRON_GUEST_WINDOW_MANAGER_INTERNAL_WINDOW_OPEN',
|
||||||
|
event, url, referrer, frameName, disposition,
|
||||||
|
options, additionalFeatures, postData)
|
||||||
|
})
|
||||||
|
|
||||||
|
// Create a new browser window for the native implementation of
|
||||||
|
// "window.open", used in sandbox and nativeWindowOpen mode.
|
||||||
|
this.on('-add-new-contents', (event, webContents, disposition,
|
||||||
|
userGesture, left, top, width, height, url, frameName) => {
|
||||||
|
if ((disposition !== 'foreground-tab' && disposition !== 'new-window' &&
|
||||||
|
disposition !== 'background-tab')) {
|
||||||
|
event.preventDefault()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
const options = {
|
||||||
|
show: true,
|
||||||
|
x: left,
|
||||||
|
y: top,
|
||||||
|
width: width || 800,
|
||||||
|
height: height || 600,
|
||||||
|
webContents
|
||||||
|
}
|
||||||
|
const referrer = { url: '', policy: 'default' }
|
||||||
|
ipcMainInternal.emit('ELECTRON_GUEST_WINDOW_MANAGER_INTERNAL_WINDOW_OPEN',
|
||||||
|
event, url, referrer, frameName, disposition, options)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
app.emit('web-contents-created', {}, this)
|
app.emit('web-contents-created', {}, this)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue