Skip to content

Commit

Permalink
docs: add <webview> new-window event removal to breaking-changes.md
Browse files Browse the repository at this point in the history
  • Loading branch information
Milan Burda authored and miniak committed May 25, 2023
1 parent 185180a commit 39cf439
Show file tree
Hide file tree
Showing 6 changed files with 101 additions and 0 deletions.
33 changes: 33 additions & 0 deletions docs/breaking-changes.md
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,39 @@ webContents.setWindowOpenHandler((details) => {
})
```

### Removed: `<webview>` `new-window` event

The `new-window` event of `<webview>` has been removed. There is no direct replacement.

```js
// Removed in Electron 22
webview.addEventListener('new-window', (event) => {})
```

```javascript fiddle='docs/fiddles/ipc/webview-new-window'
// Replace with

// main.js
mainWindow.webContents.on('did-attach-webview', (event, wc) => {
wc.setWindowOpenHandler((details) => {
mainWindow.webContents.send('webview-new-window', wc.id, details)
return { action: 'deny' }
})
})

// preload.js
const { ipcRenderer } = require('electron')
ipcRenderer.on('webview-new-window', (e, webContentsId, details) => {
console.log('webview-new-window', webContentsId, details)
document.getElementById('webview').dispatchEvent(new Event('new-window'))
})

// renderer.js
document.getElementById('webview').addEventListener('new-window', () => {
console.log('got new-window event')
})
```

### Deprecated: BrowserWindow `scroll-touch-*` events

The `scroll-touch-begin`, `scroll-touch-end` and `scroll-touch-edge` events on
Expand Down
3 changes: 3 additions & 0 deletions docs/fiddles/ipc/webview-new-window/child.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<body>
<a href="child.html" target="_blank">new window</a>
</body>
4 changes: 4 additions & 0 deletions docs/fiddles/ipc/webview-new-window/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<body>
<webview id=webview src="child.html" allowpopups></webview>
<script src="renderer.js"></script>
</body>
51 changes: 51 additions & 0 deletions docs/fiddles/ipc/webview-new-window/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// Modules to control application life and create native browser window
const { app, BrowserWindow } = require('electron')
const path = require('path')

function createWindow () {
// Create the browser window.
const mainWindow = new BrowserWindow({
width: 800,
height: 600,
webPreferences: {
preload: path.join(__dirname, 'preload.js'),
webviewTag: true
}
})

mainWindow.webContents.on('did-attach-webview', (event, wc) => {
wc.setWindowOpenHandler((details) => {
mainWindow.webContents.send('webview-new-window', wc.id, details)
return { action: 'deny' }
})
})

// and load the index.html of the app.
mainWindow.loadFile('index.html')

// Open the DevTools.
// mainWindow.webContents.openDevTools()
}

// This method will be called when Electron has finished
// initialization and is ready to create browser windows.
// Some APIs can only be used after this event occurs.
app.whenReady().then(() => {
createWindow()

app.on('activate', function () {
// On macOS it's common to re-create a window in the app when the
// dock icon is clicked and there are no other windows open.
if (BrowserWindow.getAllWindows().length === 0) createWindow()
})
})

// Quit when all windows are closed, except on macOS. There, it's common
// for applications and their menu bar to stay active until the user quits
// explicitly with Cmd + Q.
app.on('window-all-closed', function () {
if (process.platform !== 'darwin') app.quit()
})

// In this file you can include the rest of your app's specific main process
// code. You can also put them in separate files and require them here.
6 changes: 6 additions & 0 deletions docs/fiddles/ipc/webview-new-window/preload.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
const { ipcRenderer } = require('electron')
const webview = document.getElementById('webview')
ipcRenderer.on('webview-new-window', (e, webContentsId, details) => {
console.log('webview-new-window', webContentsId, details)
webview.dispatchEvent(new Event('new-window'))
})
4 changes: 4 additions & 0 deletions docs/fiddles/ipc/webview-new-window/renderer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
const webview = document.getElementById('webview')
webview.addEventListener('new-window', () => {
console.log('got new-window event')
})

0 comments on commit 39cf439

Please sign in to comment.