1

I have a listener defined in my content script in chrome extension:

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

and have a function defined in my 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:

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. 2) Unchecked runtime.lastError while running tabCapture.capture: Extension has not been invoked for the current page (see activeTab permission). Chrome pages cannot be captured.

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.

Xan
  • 74,770
  • 16
  • 179
  • 206
  • What causes the `startRecording` event in the document? If it's not an explicit user gesture, then it's not considered "invoked" and will fail. – Xan Jan 22 '15 at 11:32
  • I have a button in my application. I have this code on my button click event: var event = document.createEvent('Event'); event.initEvent('startRecording'); document.dispatchEvent(event); – user3332731 Jan 22 '15 at 14:01
  • You are new here; hint: you can and should **edit** your question with additional info. – Xan Jan 22 '15 at 14:01

1 Answers1

0

Problem here is that Chrome loses track of the fact that this event was initiated with a click. 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. I think it should be enough for Chrome to accept that as invocation by the user.


Alternatively, direct sending of a message to the extension bypassing the content script may work. This is achievable with "externally_connectable" method (docs).

Community
  • 1
  • 1
Xan
  • 74,770
  • 16
  • 179
  • 206
  • This is not working :( same exceptions occurred when accessed my internal method directly from my application . – user3332731 Jan 26 '15 at 06:07
  • Do you mean the first or the second method I mentioned? – Xan Jan 26 '15 at 09:33
  • Strange. Can you update your question with code from attempting the first method (click listener in content script)? – Xan Jan 27 '15 at 13:37