捕获 chrome 选项卡而不进行浏览操作

Capture chrome tab without browse action

我有一个捕获当前选项卡的简单方法,它仅在通过单击扩展按钮(即 browserAction)触发此方法时有效。

但是当我在 setTimeout 事件中使用此方法时,它失败并出现以下错误:

Unchecked runtime.lastError while running tabCapture.capture: Extension has not been invoked for the current page (see activeTab permission). Chrome pages cannot be captured.

我的简单抓包方法是:

function captureTab() {
    var constraints = ...
    chrome.tabCapture.capture(constraints, function (stream) {
        if (!stream) {
            console.error("couldn't record tab");
            return;
        }
        // recording succeeded
    });
} 

并且在触发时有效,如下所示:

chrome.browserAction.onClicked.addListener(function () {
    chrome.tabs.getSelected(null, function (tab) {
        captureTab();
    });
});

但是触发如下:

setTimeout(function () {
    chrome.tabs.getSelected(1, function (tab) {
        captureTab();            
    });
}, 1 * 30 * 1000);

重要说明:我的扩展程序不是 public 扩展程序,它只在我的浏览器上运行,因此我可以在启动时添加任何命令行开关(按顺序禁用此限制),如果需要的话。

有效的方法是调用您的扩展程序的用户操作(有人点击某些内容会告诉 Chrome 它没问题,因为用户想要它)。

这会暂时授予扩展程序 "activeTab" 权限,这就是它正常工作的原因。 (https://developer.chrome.com/extensions/activeTab)

要使其按您的预期工作(无需执行自动引入 activeTab 权限的操作),您需要在扩展程序的权限部分声明它,例如:

"permissions": [
  "tabs",
  "bookmarks",
  "http://www.blogger.com/",
  "http://*.google.com/",
  "unlimitedStorage",
  "activeTab"
],

https://developer.chrome.com/extensions/declare_permissions

请注意 chrome.tabCapture.capture 只能在调用扩展程序后在当前活动的选项卡上启动。

Captures the visible area of the currently active tab. Capture can only be started on the currently active tab after the extension has been invoked.

并且当您声明 activeTab 权限时,只有以下用户手势才能启用它

  • Executing a browser action
  • Executing a page action
  • Executing a context menu item
  • Executing a keyboard shortcut from the commands API
  • Accepting a suggestion from the omnibox API

因此,当您使用 setTimeOut 时,您无法确保当前选项卡处于活动状态。

我知道这很奇怪我在接受了不同的答案后回答了我自己的问题,但是...

我有一个解决方案,只有当你可以将开关注入 chrome 时才能应用,正如我上面提到的,在我的情况下这是可能的:

Important note: My extension isn't a public extension and it runs only on my browser so I can add any command line switch on start (in order to disable this limitation), if needed.

所以诀窍是将以下开关与您的扩展 ID 一起使用:

--whitelisted-extension-id="abcdefghijklmnopqrstuvwxyz"

希望有一天这会对某人有所帮助。