webContents是electron的核心,它负责渲染并控制网页,也是BrowserWindow实例的属性

如果是新建的窗口,直接获取

const BrowserWindow = require('electron').BrowserWindow;
let win = new BrowserWindow({width: 1000, height: 500});
win.loadURL("https://www.wumao.org");

let webContents = win.webContents;

获取激活状态下的窗口webConents实例

const { webContents } = require('electron')
let webContent = webContents.getFocusedWebContents()

在渲染进程中获取到当前窗口的webConents实例

const { remote } = require('electron')
let webContent = remote.getCurrentWebContents

通过ID去拿

实际上每个窗口创建出来的时候都有一个只读的id属性,然后只要我们创建的时候记住他,每次通过ID去查就很方便

const { webContents } = require('electron')
let webContent = webContents.fromId(要取的ID)
Last modification:May 26th, 2020 at 09:34 pm
If you think my article is useful to you, please feel free to appreciate