I'm building a chrome extension for catching screen records and editing them, I'm trying to use the chrome tab capture API for getting video stream in the content script or background script and I'm getting an undefined error. I am using manifest version 3 and I set in the manifest the tab capture permission here is a sample trying to run in the content script. I also tried to send the message to the popup.js, but as soon as the popup close, the recording stops and return error :-
content-script.js
var recordButton = document.createElement("button");
recordButton.innerHTML = "Record";
chrome.runtime.sendMessage({message: "access_tabCapture_API"});
// Add an event listener to the record button to start recording
recordButton.addEventListener("click", function() {
chrome.runtime.onMessage.addListener(function(request, sender, sendResponse) {
if(request.message === "stream_object") {
// Use the stream object received from the background script
//var stream = request.stream;
if(request.stream != undefined || request.stream != null) {
console.log('yes, its working')
} else {
console.log('nothing there')
}
}
});
background.js
chrome.runtime.onMessage.addListener(function(request, sender, sendResponse) {
if(request.message === "access_tabCapture_API") {
chrome.tabCapture.capture({audio: true, video: true}, function(stream) {
// Once the tabCapture API is accessed, send the stream object back to the content script
chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
chrome.tabs.sendMessage(tabs[0].id, {message: "stream_object", stream: stream});
});
});
}
});
I am getting error :- Error in event handler: TypeError: Cannot read properties of undefined (reading 'capture') at chrome-......