在 window 的活动面板中捕获屏幕 从 chrome 扩展程序打开

capture screen in active panel from window opened from chrome extension

我创建了一个页面事件 google-chrome-extension,当按下扩展程序弹出窗口上的按钮时,它会创建一个新的 window。我不想通过按新 window 中的按钮从活动面板截取屏幕截图。我通过向后台页面发送消息以在那里进行捕获来做到这一点,但我总是收到此错误:

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

如果我理解正确,该捕获应该由扩展弹出窗口或活动面板中的用户操作直接启动。有一些方法可以设置一些东西,以便在当前页面中生成接受扩展内容页面中的点击?

----清单------

"background" : {
    "page": "background.html",
    "persistent": false
  },
  "page_action" :{
    "default_popup": "popup.html" },
  "permissions": ["activeTab",  "declarativeContent",  "tabCapture",  "background"], 

------------background.js--------

chrome.runtime.onMessage.addListener(
  function(request, sender, sendResponse) {
    console.log(sender.tab ?
                "from a content script:" + sender.tab.url :
                "from the extension");
    if (request.command === "init_capture"){
      locate_tab(function(){
        init_capture();
      });

function init_capture(e){

    video = document.querySelector('video');
      canvas = document.querySelector('canvas');
      ctx = canvas.getContext('2d');

      var MediaStreamConstraint = {
            //audio: true,
            video: true,
            videoConstraints: {
                mandatory: {
                    chromeMediaSource: 'tab',
                    minWidth: 640,
                    maxWidth: 800,
                    minHeight: 420,
                    maxHeight: 600
                }
            }
      };

           chrome.tabCapture.capture(MediaStreamConstraint, function(stream){ 
console.log(stream); //always null.. activeTab error
});
    }

嗯..我得到了一个简单的解决方案,我想分享这个,希望其他用户分享解决这个问题的不同方法:

  1. 在我的例子中,我的弹出窗口中有一个 "launch window" 按钮,在新的 window 中有一个 "init capture" 和 "capture frame" 按钮。 因此,我没有搞乱权限,而是从新的 window 中删除了 "init capture" 按钮,并将该代码放在弹出 "launch window" 按钮的同一处理程序中,以这种方式绕过安全限制。

  2. 我在后台页面中保留了隐藏的 canvas 和视频标签,这才是真正的捕获发生的地方。当用户在新的 window 上按下 "capture frame" 时,它只是通过回调 canvas 的内容向后台页面发送消息 returns。