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.