使用 chrome.tabs.captureVisibleTab 的屏幕截图

Screenshot using chrome.tabs.captureVisibleTab

本文关键字:屏幕截图 captureVisibleTab tabs chrome 使用      更新时间:2023-09-26

我正在尝试使用chrome.tabs.captureVisibleTab捕获页面的可见区域。下面是进行调用的代码:

chrome.extension.onMessage.addListener(function(request, sender, sendResponse) {
    if (request.name == 'screenshot') {
        chrome.tabs.captureVisibleTab(null, null, function(dataUrl) {
            sendResponse({ screenshotUrl: dataUrl });
        });
    }
});

但是当我尝试捕获选项卡时,出现此错误:

运行 tabs.captureVisibleTab 时未选中的 runtime.lastError:"activeTab"权限无效,因为尚未调用此扩展。

这是我的清单文件:

   {
  "manifest_version": 2,
  "name": "Empathy",
  "version": "0.1",
  "description": "Simulate accessibility issues for websites.",
  "browser_action": {
    "default_icon": "empathy19.png",
    "default_title": "Empathy!"
  },
  "permissions": [
    "activeTab",
    "contextMenus",
    "desktopCapture",
    "tabCapture",
    "tts" // Text-to-speech
  ],
  "background":   {
    "scripts": [
      "boot.js"
    ],
    "persistent": false
  },
  "content_scripts": [
    {
      "matches": ["<all_urls>"],
      "js": [
        "src/helpers.js",
        "src/colorblindness.js",
        "lib/colorvision.js",
        "lib/html2canvas.js"
      ]
    }
  ]
}
  • 我有活动选项卡权限
  • 正在从后台脚本进行调用
  • 我正在匹配<all_urls>

为什么我会收到该错误?

有些事情会说<all_urls>是匹配的东西,但我缺少的是<all_urls>许可。添加权限后,它起作用了。