将电子 NativeImage 从 <webview>.capturePage() 转换为 base64 或 dataURL

Transform electron NativeImage from <webview>.capturePage() to base64 or dataURL

所以我试图在 electron 中截取一个 webview,然后在 webview 之外的 <img> 上显示它。但是,当我拥有 NativeImage 时,我无法将其转换为 dataUrl。 这是我试过的:

const electron = require("electron");
const {remote, nativeImage} = electron;
...
var wv = document.getElementById("wv");
var ph = wv.capturePage();
var pr = ph.toDataURL();

我得到:

ph.toDataURL() is not a function

我做错了什么?谢谢!

capturePage()要么returns一个承诺,要么收到回调,所以你必须等待它解决(awaitthen()):

var ph = await wv.capturePage();
var pr = ph.toDataURL();

或者在回调中获取你的数据url:

var ph = wv.capturePage(function (ph) {
  var pr = ph.toDataURL();
});

编辑:

here 中发布,这似乎是一个已知问题,其中函数 <webview>.capturePage() returns 是电子 4.0 的空对象。解决方法是:

<webview>.getWebContents().capturePage()