上面是 Electron 中渲染过程可用的模块:

模块形容
desktopCapturer用来获取可用资源,这个资源可通过 getUserMedia 捕捉失去
ipcRenderer是一个 EventEmitter 类的实例,它提供了无限的办法,能够从渲染过程向主过程发送同步或异步音讯,也能够收到主过程的相应
remote提供了一个简略的跨过程之间通信的办法
webFrame用来定制以后网页的渲染

desktopCapturer模块

desktopCapturer 模块用来获取可用的资源,能够用 getUserMedia 来获取。

示例:
// 在渲染过程中var desktopCapturer = require('electron').desktopCapturer;desktopCapturer.getSources({types: ['window', 'screen']}, function(error, sources) {  if (error) throw error;  for (var i = 0; i < sources.length; ++i) {    if (sources[i].name == "Electron") {      navigator.webkitGetUserMedia({        audio: false,        video: {          mandatory: {            chromeMediaSource: 'desktop',            chromeMediaSourceId: sources[i].id,            minWidth: 1280,            maxWidth: 1280,            minHeight: 720,            maxHeight: 720          }        }      }, gotStream, getUserMediaError);      return;    }  }});function gotStream(stream) {  document.querySelector('video').src = URL.createObjectURL(stream);}function getUserMediaError(e) {  console.log('getUserMediaError');}

上述代码中,当调用 navigator.webkitGetUserMedia 时创立一个束缚对象,如果应用 desktopCapturer 的资源,必须设置 chromeMediaSource"desktop" ,并且 audiofalse

如果咱们想捕捉整个桌面的 audiovideo,能够设置 chromeMediaSource"screen" ,和 audiotrue。 当应用这个办法的时候,不能够指定一个 chromeMediaSourceId

ipcRenderer模块

ipcRenderer 模块是一个 EventEmitter 对象的实例,它提供了一些办法能够用来向主过程发送同步或异步音讯,也能够收到主过程的响应。

ipcRenderer 模块有下列办法来监听事件:

办法形容
on监听 channel,当有新音讯达到,应用 listener(event, args...) 调用 listener
once为这个事件增加一个一次性 listener 函数
removeListener从指定的 channel 中的监听者数组删除指定的 listener
removeAllListeners删除所有的监听者,或者删除指定 channel 中的全副

ipcRenderer 模块有如下办法来发送音讯:

办法形容
send通过 channel 向主过程发送异步音讯,也能够发送任意参数,参数会被 JSON 序列化,之后就不会蕴含函数或原型链
sendSync通过 channel 向主过程发送同步音讯,也能够发送任意参数,参数会被 JSON 序列化,之后就不会蕴含函数或原型链
sendToHost相似 ipcRenderer.send ,然而它的事件将发往 host page<webview> 元素,而不是主过程

remote模块

remote 模块提供了一种在渲染过程(网页)和主过程之间进行过程间通信(IPC)的简便路径。

示例:

上面是从渲染过程创立一个浏览器窗口的例子:

const remote = require('electron').remote;const BrowserWindow = remote.BrowserWindow;var win = new BrowserWindow({ width: 800, height: 600 });win.loadURL('https://github.com');

remote 模块的办法有:

办法形容
require返回在主过程中执行 require(module) 所返回的对象
getCurrentWindow返回该网页所属的 BrowserWindow 对象
getCurrentWebContents返回该网页的 WebContents 对象
getGlobal返回在主过程中名为 name 的全局变量(即 global[name])
process返回主过程中的 process 对象

webFrame模块

webFrame 模块用来定制以后网页的渲染。

示例:

例如将页面放大到 150%

var webFrame = require('electron').webFrame;webFrame.setZoomFactor(2)

web-frame 模块有如下办法:

办法形容
setZoomFactor将缩放参数批改为指定的参数值,缩放参数是百分制的,所以 300% = 3.0
getZoomFactor返回以后缩放参数值
setZoomLevel将缩放程度批改为指定的程度值,原始 size 为 0,并且每次增长都示意放大 20% 或放大 20%
getZoomLevel返回以后缩放程度值
setZoomLevelLimits设置缩放程度的最大值和最小值
setSpellCheckProvider为输入框或文本域设置一个拼写查看 provider