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

feat: deprecate <webview>.getWebContents() #21039

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
41 changes: 41 additions & 0 deletions docs/api/breaking-changes.md
Expand Up @@ -59,6 +59,47 @@ these kinds of objects will throw a 'could not be cloned' error.

[SCA]: https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/Structured_clone_algorithm

### `<webview>.getWebContents()`

This API is implemented using the `remote` module, which has both performance
and security implications. Therefore its usage should be explicit.

```js
// Deprecated
webview.getWebContents()
// Replace with
const { remote } = require('electron')
remote.webContents.fromId(webview.getWebContentsId())
```

However, it is recommended to avoid using the `remote` module altogether.

```js
// main
const { ipcMain, webContents } = require('electron')

const getGuestForWebContents = function (webContentsId, contents) {
const guest = webContents.fromId(webContentsId)
if (!guest) {
throw new Error(`Invalid webContentsId: ${webContentsId}`)
}
if (guest.hostWebContents !== contents) {
throw new Error(`Access denied to webContents`)
}
return guest
}

ipcMain.handle('openDevTools', (event, webContentsId) => {
const guest = getGuestForWebContents(webContentsId, event.sender)
guest.openDevTools()
})

// renderer
const { ipcRenderer } = require('electron')

ipcRenderer.invoke('openDevTools', webview.getWebContentsId())
```

## Planned Breaking API Changes (7.0)

### Node Headers URL
Expand Down
2 changes: 1 addition & 1 deletion docs/api/webview-tag.md
Expand Up @@ -648,7 +648,7 @@ Sets the maximum and minimum layout-based (i.e. non-visual) zoom level.

Shows pop-up dictionary that searches the selected word on the page.

### `<webview>.getWebContents()`
### `<webview>.getWebContents()` _Deprecated_

Returns [`WebContents`](web-contents.md) - The web contents associated with
this `webview`.
Expand Down
6 changes: 6 additions & 0 deletions lib/renderer/web-view/web-view-impl.ts
Expand Up @@ -238,6 +238,12 @@ export const setupMethods = (WebViewElement: typeof ElectronInternal.WebViewElem
return remote.getGuestWebContents(internal.guestInstanceId!)
}

WebViewElement.prototype.getWebContents = electron.deprecate.moveAPI(
WebViewElement.prototype.getWebContents,
'webview.getWebContents()',
'remote.webContents.fromId(webview.getWebContentsId())'
) as any

// Focusing the webview should move page focus to the underlying iframe.
WebViewElement.prototype.focus = function () {
this.contentWindow.focus()
Expand Down
3 changes: 2 additions & 1 deletion spec/fixtures/pages/webview-devtools.html
Expand Up @@ -8,7 +8,8 @@
<script>
var wv = document.querySelector('webview')
wv.addEventListener('dom-ready', () => {
const webContents = wv.getWebContents()
const { remote } = require('electron')
const webContents = remote.webContents.fromId(wv.getWebContentsId())
webContents.on('devtools-opened', function () {
var showPanelIntevalId = setInterval(function () {
if (webContents.devToolsWebContents) {
Expand Down