chrome.tabs.captureVisibleTab在chrome.tabs.update后失败为同步回调?

时间:2016-04-26 02:55:34

标签: javascript google-chrome google-chrome-extension callback

我目前正在尝试弄清楚如何通过扩展程序在Chrome浏览器上自动执行屏幕捕获。我已到达chrome.tabs.captureVisibleTab,但遇到了一个奇怪的问题:

chrome.tabs.update(tabId, { active: true }, function() {
      chrome.tabs.captureVisibleTab(null, {}, function(data) {
            // Going to canvas that data so hard...
            // The system's going think it's tainted
          });
    });

以上内容失败并显示以下消息:

Unchecked runtime.lastError while running tabs.captureVisibleTab: Failed to capture tab: unknown error
    at Object.capture [as callback]

但是,如果我将其更改为以下内容:

chrome.tabs.update(tabId, { active: true }, function() {
      setTimeout(function() {
          chrome.tabs.captureVisibleTab(null, {}, function(data) {
                // Going to canvas that data so hard...
                // The system's going think it's tainted
              });
        }, 0);
    });

代码经历没有问题。但是,回调最终是异步的。我的扩展目前可以“并行”自动化多个选项卡。我担心在执行第一个chrome.tabs.update之前可能会有多个chrome.tabs.captureVisibleTab被调用。

思考?我忽略了什么?还是一个潜在的错误?任何见解都非常感谢。

1 个答案:

答案 0 :(得分:0)

在manifest.json中尝试$(document).ready(function() { $("#chatBubble").click(function(){ //console.log("clicked") ('#wrapper').show(); }) }); 。这为我解决了。