On the Electron <webview>
documentation there is a list of methods that you can use with the object. When I try to run any of the methods, none of them work. When I looked in inpect the properties of the <webview>
element in the inspector, it says that its prototype is webview
. (__proto__ : webview
)
It is in that prototype where all of the methods are stored. So my element should basically just inherit those methods from its prototype when I use those methods (e.g. myWebview.openDevTools()
).
However! when i use Object.getProptotypeOf(myWebview)
I get HTMLElement
, NOT webview
like it shows in the inspector.
Here is an example of my code:
<webview id="myWebview" src="path/to.file"></webview>
<script>
var myWebview = document.getElementById('myWebview');
console.log("myWebview: ",myWebview);
console.log("prototype: ",Object.getPrototypeOf(myWebview)); //=> HTMLElement
myWebview.openDevTools();
</script>
I discovered the issue and added an example to the Electron Documentation
The bottom line is that you need to add a listener to the webview that listens for when the webview element is ready:
According to @Shwany, the webview's methods will be available when the
did-start-loading
event is fired, however it may be better practice to wait until the webview element is completely ready usingdom-ready
For a more detailed explanation:
When the window is initially rendering the DOM, the webview methods are not available. Initially, the prototype for the
<webview>
element is still the genericHTMLElement
.It as after the page renders that the
<webview>
element begins loading and then its prototype is changed to the webview prototype (same name as the element). And when it gains access to the webview prototype, it gains access to all of the webview prototype methods.