How can I access the DOM of a in Electron?

后端 未结 3 1244
盖世英雄少女心
盖世英雄少女心 2020-12-30 10:40

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 ifram

相关标签:
3条回答
  • 2020-12-30 11:05

    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");
    

    use:

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

    And:

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

    See: http://electron.atom.io/blog/2015/11/17/electron-api-changes section on 'Splitting the ipc module'

    0 讨论(0)
  • 2020-12-30 11:08

    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

    0 讨论(0)
  • 2020-12-30 11:09

    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 (e.channel === "window-data") {
        // console.log(e.args[0]);
    
        $(".tab.active .tab-favicon").attr("src", e.args[0].favicon);
        $(".tab.active .tab-title").html(e.args[0].title);
        $("#url-bar").val(e.args[0].url);
    
        $("#aries-titlebar h1").html("Aries | " + e.args[0].title);
      }
    
      // TODO
      // Make this better...cancel out setTimeout?
      var timer;
    
      if (e.channel === "mouseover-href") {
        // console.log(e.args[0]);
        $(".linker").html(e.args[0]).stop().addClass("active");
    
        clearTimeout(timer);
    
        timer = setTimeout(function () {
          $(".linker").stop().removeClass("active");
        }, 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 = e.target;
    
      if (hoveredEl.tagName !== "A") {
        return;
      }
    
      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 hikar.io?
        "favicon": "https://www.google.com/s2/favicons?domain=" + 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).

    0 讨论(0)
提交回复
热议问题