最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Capture chrome tab without browse action - Stack Overflow

programmeradmin0浏览0评论

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 mand line switch on start (in order to disable this limitation), if needed.

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 mand line switch on start (in order to disable this limitation), if needed.

Share Improve this question asked Mar 14, 2016 at 10:39 urieluriel 1,2481 gold badge16 silver badges26 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 5

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 mands API
  • Accepting a suggestion from the omnibox API

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

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 mand 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.

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./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./",
  "http://*.google./",
  "unlimitedStorage",
  "activeTab"
],

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

发布评论

评论列表(0)

  1. 暂无评论