How can I access the DOM of a <webview> in Electron?

I'm just getting started with Electron, with prior experience with node-webkit (nw.js).

In nw.js, I was able to create iframes and then access the DOM of said iframe in order to grab things like the title, favicon, &c. When I picked up Electron a few days ago to port my nw.js app to it, I saw advice to use webviews instead of iframes, simply because they were better. Now, the functionality I mentioned above was relatively easy to do in nw.js, but I don't know how to do it in Electron (and examples are slim to none). Can anyone help?

Also, I have back/forward buttons for my webview (and I intend on having more than one). I saw in the documentation that I could call functions for doing so on a webview, but nothing I have tried worked either (and, I haven't found examples of them being used in the wild).



Besides guest to host IPC calls as NetOperatorWibby, it is also very useful to go from host to guest. The only way to do this at present is to use the <webview>.executeJavaScript(code, userGesture). This api is a bit crude but it works.

If you are working with a remote guest, like "extending" a third party web page, you can also utilize webview preload attribute which executes your custom script before any other scripts are run on the page. Just note that the preload api, for security reasons, will nuke any functions that are created in the root namespace of your custom JS file when your custom script finishes, however this custodial process will not nuke any objects you declare in the root. So if you want your custom functions to persist, bundle them into a singleton object and your custom APIs will persist after the page fully loads.

[update] Here is a simple example that I just finished writing: Electron-Webview-Host-to-Guest-RPC-Sample


I dunno who voted to close my question, but I'm glad it didn't go through. Other people have this question elsewhere online too. I also explained what I wanted to achieve, but w/e.

I ended up using ipc-message. The documentation could use more examples/explanations for the layperson, but hey, I figured it out. My code is here and here, but I will also post examples below should my code disappear for whatever reason.

This code is in aries.js, and this file is included in the main renderer page, which is index.html.

var ipc = require("ipc");
var webview = document.getElementsByClassName("tabs-pane active")[0];

webview.addEventListener("ipc-message", function (e) {
  if ( === "window-data") {
    // console.log(e.args[0]);

    $(" .tab-favicon").attr("src", e.args[0].favicon);
    $(" .tab-title").html(e.args[0].title);

    $("#aries-titlebar h1").html("Aries | " + e.args[0].title);

  // TODO
  // Make this better...cancel out setTimeout?
  var timer;

  if ( === "mouseover-href") {
    // console.log(e.args[0]);


    timer = setTimeout(function () {
    }, 1500);

This next bit of code is in browser.js, and this file gets injected into my <webview>.

var ipc = require("ipc");

document.addEventListener("mouseover", function (e) {
  var hoveredEl =;

  if (hoveredEl.tagName !== "A") {

  ipc.sendToHost("mouseover-href", hoveredEl.href);

document.addEventListener("DOMContentLoaded", function () {
  var data = {
    "title": document.title,
    "url": window.location.href,
    // need to make my own version, can't rely on Google forever
    // maybe have this URL fetcher hosted on
    "favicon": "" + window.location.href

  ipc.sendToHost("window-data", data);

I haven't found a reliable way to inject jQuery into <webview>s, and I probably shouldn't because the page I would be injecting might already have it (in case you're wondering why my main code is jQuery, but there's also regular JavaScript).


This relates to previous answer (I am not allowed to comment): Important info regarding ipc module for users of Electron 1.x:

The ipc module was split into two separate modules:

  • ipcMain for the main process
  • ipcRenderer for the renderer process

So, the above examples need to be corrected, instead of

// Outdated - doesn't work in 1.x    
var ipc = require("ipc");


// In main process.
var ipcMain = require('electron').ipcMain


// In renderer process.
var ipcRenderer = require('electron').ipcRenderer

See: section on 'Splitting the ipc module'


Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us

©2020 All rights reserved.