1 year ago

#387232

test-img

Aiden Pearce

Manifest v3 Chrome Extension Screen Recording with Background Script

I am trying to create an extension to record screen. In manifest version 2 I was using a persistent background script and calling the start function with chrome.extension.getBackgroundPage() and everything was working perfectly. Now I am trying to migrate to v3 and having problems. Basically what I want to do is start screen record with chrome.desktopCapture in my background script for the active tab.

When I try to run chrome.desktopCapture in background.js chrome shut itself immediately. Since I cannot use getBackgroundPage anymore, I am passing message between the popup and background.

The Code:

// popup.html
chrome.runtime.sendMessage({
    message: "start", 
    tabId: <active-tab-id-here>
    }, (response) => {
           console.log(response.answer);
});


// background.js

chrome.runtime.onMessage.addListener( (request, _, sendResponse) => {
    switch(request.message) {
        case 'start':
               chrome.desktopCapture.chooseDesktopMedia(['tab', 'screen'], onAccessApproved);
    }
    
    sendResponse({answer: "Message Received Successfully!"});
}
);

javascript

google-chrome-extension

0 Answers

Your Answer

Accepted video resources