node.js - puppeteer overridePermissions 剪贴板读取不适用于 createIncognitoBrowserContext()

标签 node.js puppeteer

以下代码可用于 headless /有头读取剪贴板:

var context = await client.defaultBrowserContext();
await context.overridePermissions('http://localhost', ['clipboard-read']);
page = await browser.newPage();
await page.goto( 'http://localhost/test/', {waitUntil: 'load', timeout: 35000});
// click button for clipboard..
let clipboard = await page.evaluate(`(async () => await navigator.clipboard.readText())()`);

但是当您稍后开始隐身时,它就不再起作用了:

const incognito = await client.createIncognitoBrowserContext();
page = await incognito.newPage();

你得到:

  • DOMException:读取权限被拒绝。

我目前正在尝试将两者结合起来。有人知道如何在新的隐身窗口中设置 overridePermissions 吗?

请注意,我不想在开始时使用隐身 chrome arg。我想使用正确的 overridePermissions 在我的脚本中手动创建新的隐身页面。

最佳答案

我遇到了同样的问题。这是一个最小的可重现示例。 Node 版本:v16.13.1 puppeteer 版本:puppeteer@14.4.1

  'use strict';
  const puppeteer = require('puppeteer');
  const URL = 'https://google.com';
  (async () => {
      const browser = await puppeteer.launch();
      const context = browser.defaultBrowserContext();
      context.overridePermissions(URL, ['clipboard-read', 'clipboard-write'])
      const page = await browser.newPage();
      await page.goto(URL, {
        waitUntil: 'networkidle2',
      });
  
      await page.evaluate(() => navigator.clipboard.writeText("Injected"));
  
      const value = await page.evaluate(() => navigator.clipboard.readText());
      console.log(value);
  })();

关于node.js - puppeteer overridePermissions 剪贴板读取不适用于 createIncognitoBrowserContext(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63531924/

相关文章:

jestjs - Puppeteer - checkbox.checked 未定义 - 为什么?

puppeteer - 表情符号在 Chrome Headless 的 PDF 输出中转换为灰色

node.js - Node java typescript 导入

node.js - 无法使用 Vue + Express 保存 session

node.js - 从模块调用的nodejs

javascript - 如何将 userDataDir 配置文件文件夹传递给 Puppeteer

pdf-generation - puppeteer 空白 pdf 生成

javascript - 将带有resolve和reject的promise转换为异步函数的正确方法(puppeteer)

javascript - 使用 Karma : "module is not defined" 进行 Angular Testing

javascript - 将 Dynamo DB 流数据解码为 Json 格式