0

I am building a chrome extension that performs screen/tab recording.

manifest.json

{
  "name": "My Recorer",
  "version": "1.0",
  "manifest_version": 3,
  "background": {
    "service_worker": "background.js"
  },
  "action": {
    "default_popup": "popup.html"
  },
  "permissions": ["activeTab", "declarativeContent", "storage","tabs", "desktopCapture"]
}

background.js


chrome.runtime.onInstalled.addListener(function (tab) {
  console.log(tab)
})

popup.html

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Document</title>
</head>
<body>
  <div>
    <button style="width: 100%; margin: 5%;" id="start">Record</button>
    <button style="width: 100%; margin: 5%" id="stop">stop</button>
  </div>
  <script src="popup.js"></script>
</body>
</html>

It used popup.js which handles screen recording.

let start = document.getElementById('start'),
    stop  = document.getElementById('stop'),
    mediaRecorder;

start.addEventListener('click', async function(){
    console.log("Start")
    let stream = await recordScreen();
    let mimeType = 'video/webm';
    mediaRecorder = createRecorder(stream, mimeType);
  let node = document.createElement("p");
    node.textContent = "Started recording";
    document.body.appendChild(node);
})

stop.addEventListener('click', function(){
    mediaRecorder.stop();
    let node = document.createElement("p");
    node.textContent = "Stopped recording";
    document.body.appendChild(node);
})

async function recordScreen() {
    return await navigator.mediaDevices.getDisplayMedia({
        audio: true, 
        video: { mediaSource: "screen"}
    });
}

The current application does the recording with no issues.

But, when I close the extension (I go out of the extension), the application ends completely the recording is also lost. Am I looking for how to get the recording running behind the scenes?

Want to achieve below

enter image description here

GMAC
  • 788
  • 4
  • 23
  • 2
    Are you sure this is even allowed? Surely it would be a breach of privacy? – evolutionxbox Aug 25 '21 at 12:27
  • 1
    Yeah exactly, if the user can't stop the recording by themselves it's a big privacy issue. I'm 100% sure chrome webstore won't approve a app like that – Utkarsh Dixit Aug 25 '21 at 12:28
  • By close I mean, keep recording until users stop. and extension have not to be visible all the time – GMAC Aug 25 '21 at 12:31
  • 1
    Ahh if you just mean that it should work even after the tab is closed, this is possible. Loom also does this – Utkarsh Dixit Aug 25 '21 at 12:35
  • 1
    Yeah exactly, I wasn't sure how to rephrase my question. – GMAC Aug 25 '21 at 12:36
  • @UtkarshDixit, do you have any idea how to achieve that? – GMAC Aug 25 '21 at 15:54
  • @GauravGupta AFAIK simply using desktopCapture API in your background.js would do https://developer.chrome.com/docs/extensions/reference/desktopCapture/ – Utkarsh Dixit Aug 25 '21 at 18:28
  • Unlike popup.js background scripts stick even after the tab is closed (i.e persistent background scripts) – Utkarsh Dixit Aug 25 '21 at 18:30
  • Even without the above API I think you can also use the `navigator.mediaDevices.getDisplayMedia` in the background.js. Background stript are run in a different context, but they should still have access to navigator object – Utkarsh Dixit Aug 25 '21 at 18:32
  • @UtkarshDixit, how I will have control of the button in the background.js. Can you please post an answer to this or can you add a script in https://stackblitz.com/ and share the link. – GMAC Aug 26 '21 at 07:55
  • You'll have to communicate between background script and your popup script , see the answer here https://stackoverflow.com/questions/13546778/how-to-communicate-between-popup-js-and-background-js-in-chrome-extension – Utkarsh Dixit Aug 26 '21 at 08:32
  • Thanks, @UtkarshDixit, I called desktopCapture API in the background but somehow that is closing the chrome. I think I might be calling in the wrong way. Can share the workflow from your side? – GMAC Aug 26 '21 at 13:03
  • 1
    @GauravGupta I'll give it a shot, later in the day and will post an answer here if all goes well. – Utkarsh Dixit Aug 26 '21 at 16:55
  • @GauravGupta it was closing bcoz of some bug in chrome which is fixed in v. 102 – coure2011 Jun 02 '22 at 04:54

0 Answers0