简体   繁体   English

以编程方式调用选项卡捕获会引发异常

[英]Programmatically invoke tab capture throws exception

I have a listener defined in my content script in chrome extension: 我在chrome扩展程序的内容脚本中定义了一个侦听器:

document.addEventListener("startRecording", function(data) {
    chrome.runtime.sendMessage({'action' : 'captureCurrentTab'});
});

and have a function defined in my extension.js: 并在我的extension.js中定义了一个函数:

chrome.runtime.onMessage.addListener(

function(request, sender, sendResponse) {
       if (request.action == "captureCurrentTab"){

        captureCurrentTab();
     }
 });

function captureCurrentTab() {

    chrome.tabs.getSelected(null, function(tab) {
        chrome.tabCapture.capture(MediaStreamConstraint, handleCapture);
    });
}

var MediaStreamConstraint = {
            //audio: true,
            video: true,
            videoConstraints: {
                mandatory: {
                    chromeMediaSource: 'tab',
                    minWidth: 1920,
                    maxWidth: 1920,
                    minHeight: 1080,
                    maxHeight: 1080
                }
            }
        };

function handleCapture(stream) {

    console.log('content captured');
    console.log("Adding Stream: ", stream);

}

but when i send message to start recording from my web application like this: 但是当我发送消息以从我的Web应用程序开始记录时,像这样:

var event = document.createEvent('Event');
            event.initEvent('startRecording');
            document.dispatchEvent(event);

then extension throws exceptions: 然后扩展抛出异常:

1) Error in response to tabCapture.capture: MediaStream is mandatory. 1)响应tabCapture.capture时出错:MediaStream是必需的。 2) Unchecked runtime.lastError while running tabCapture.capture: Extension has not been invoked for the current page (see activeTab permission). 2)运行tabCapture.capture时未选中runtime.lastError:当前页面未调用扩展名(请参见activeTab权限)。 Chrome pages cannot be captured. Chrome页面无法捕获。

Here are the permissions i have supplied: 这是我提供的权限:

"permissions": [
    "tabCapture",
    "tabs",
    "activeTab",
    "http://*/*",
    "https://*/*" ,
    "http://localhost:1615/*"
  ]

But when I click on my extension button and repeat the same process (send message for recording) then everything works fine. 但是,当我单击扩展名按钮并重复相同的过程(发送消息以进行记录)时,一切正常。 I don't know why I have to click extension button every time to start capturing screen. 我不知道为什么每次都必须单击扩展按钮才能开始捕获屏幕。 How can I start it automatically? 如何自动启动?

I have also defined shortcut keys for my extension. 我还为扩展程序定义了快捷键。 When I press them before sending message for recording then everything works fine. 当我在发送录制消息之前按它们时,一切正常。 but when I triggered/simulate them from my application then again end up with same exception. 但是当我从应用程序中触发/模拟它们时,再次以相同的异常结束。

Please help. 请帮忙。

Problem here is that Chrome loses track of the fact that this event was initiated with a click. 这里的问题是Chrome无法跟踪该事件是通过单击启动的。 It is therefore not "invoked". 因此,它不是“调用”的。

You should instead bind a click event listener in your content script code/context, and send the message from there directly. 您应该在内容脚本代码/上下文中绑定click事件侦听器,然后直接从那里发送消息。 I think it should be enough for Chrome to accept that as invocation by the user. 我认为Chrome足以接受用户的调用。


Alternatively, direct sending of a message to the extension bypassing the content script may work. 或者,可以绕过内容脚本直接向扩展名发送消息。 This is achievable with "externally_connectable" method ( docs ). 这可以通过"externally_connectable"方法docs )实现。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM