Capture chrome tab without browse action

2019-08-14 07:09发布

I have a simple method which captures current tab, and it works only when this method triggered by click on extension button (i.e. browserAction).

But when I use this method in setTimeout event it fails with following error:

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

My simple capture method is:

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

And it works when it triggered as follows:

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

But doesn't work when it triggered as follows:

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

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.

3条回答
Explosion°爆炸
2楼-- · 2019-08-14 07:17

The method that works is a user action invoking your extension (someone clicks something that tells Chrome it's okay because the user wanted it).

This temporarily grants "activeTab" permissions to the extension and is why it is working. (https://developer.chrome.com/extensions/activeTab)

To make it work like you intend (without an action bringing the activeTab permissions in automatically), you would need to declare it in your extension's permissions section, for example:

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

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

查看更多
叛逆
3楼-- · 2019-08-14 07:36

Please be aware chrome.tabCapture.capture can only be started on the currently active tab after the extension has been invoked.

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.

And when you declare activeTab permissions, only the following user gestures enable it

  • 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

So when you are using setTimeOut, you can not ensure current tab is active.

查看更多
Root(大扎)
4楼-- · 2019-08-14 07:37

I know this is weird I'm answering my own question after I accepted a different answer, but...

I have a solution for that which can be applied only if you can inject switch to chrome which in my case is possible as I noted above:

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.

So the trick is to use the follwoing switch with your extension id:

--whitelisted-extension-id="abcdefghijklmnopqrstuvwxyz"

Hope this will help someone someday.

查看更多
登录 后发表回答