javascript - 使用Promises的BrowserWindow.capturePage()示例?

标签 javascript electron

我正在尝试将Electron v 4.x.x项目更新为Electron的当前版本,并且脑子冻结了BrowserWindow.capturePage语法。

我捕获页面的方式如下。谁能给我一些关于如何更改它以使用capturePage promise语法(或仅作为语法示例)的意见吗?

更新:,经常提出问题以澄清问题。这似乎可行:

var img = await win.capturePage();

现有代码
var win = BrowserWindow.fromId(test.windowID);
var img = await saveScreen(win, needNewPage)   
// do some stuff with the returned image
function saveScreen(win, needNewPage) {
  return new Promise(resolve => {
    win.capturePage((img) => {
      resolve(img)
    });
  });
}

最佳答案

这是来自的capturePage的声明

Electron v3.1.0


    /**
     * Captures a snapshot of the webview's page. Same as
     * webContents.capturePage([rect, ]callback).
     */
    capturePage(callback: (image: NativeImage) => void): void;
    /**
     * Captures a snapshot of the webview's page. Same as
     * webContents.capturePage([rect, ]callback).
     */
    capturePage(rect: Rectangle, callback: (image: NativeImage) => void): void;

这是从

Electron@latest(v7.1.8)


    /**
     * Resolves with a NativeImage
     *
     * Captures a snapshot of the page within `rect`. Omitting `rect` will capture the
     * whole visible page.
     */
    capturePage(rect?: Rectangle): Promise<Electron.NativeImage>;

正如您在此处看到的,从某种程度上讲是 Electron 版本。我们不能在capturePage函数中使用回调。因此,我们能够将结果图像检索为回调参数。但是从新版本开始,我们只能使用Promise

关于javascript - 使用Promises的BrowserWindow.capturePage()示例?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59849601/

相关文章:

javascript - 绝对定位导航箭头问题

javascript - 在不影响可读性的情况下同时从 'lodash' 、 'rxjs' 、 'ramda' 导入 { map }

javascript - AngularJS:如何使用 ui-router 指定备用启动路径?

javascript - 使用 webkit-tap-highlight 查找元素

node.js - 如何使 pnpm 对全局目录使用符号链接(symbolic link)?

Electron 浏览器窗口无法打开

javascript - 向 IDE 提供使用 Electron 的 remote.require 导入的模块的 typescript 定义

typescript - 为什么我必须使用 "new BrowserWindow"而不是 "new Electron.BrowserWindow"实例化 Electron.BrowserWindow ?

javascript - 是否可以更改 webview 内容的样式?

javascript - 用于 Restful api 的 Google 驱动器