Skip to content
TheChatArchiveThe Chat Archive
o1-preview-2024-09-12· OpenAIAi2 WildChat-4.8M (AI2)

Help me make me a chrome extension that will record my screen in great quality. It must…

  1. User

    Help me make me a chrome extension that will record my screen in great quality. It must have a Record Button to start recording and a finish button to stop the recording and save it on my computer. Add also a timer that go along with the recording and stop when it's over.

  2. Assistant

    Sure! I'd be happy to help you create a Chrome extension that records your screen with great quality. We'll build an extension with a simple user interface that includes a "Record" button to start recording, a "Finish" button to stop recording and save the video to your computer, and a timer that displays the duration of the recording.

    Here's a step-by-step guide to creating this Chrome extension.


    Overview

    We'll create a Chrome extension consisting of the following files:

    • manifest.json — Configures the extension's properties and permissions.
    • popup.html — The HTML for the extension's popup UI.
    • popup.js — The JavaScript that handles the recording logic and UI interactions.
    • icon.png — An icon for the extension (you can use any 128x128 PNG image).

    Step 1: Set Up the Extension Directory

    1. Create a new folder on your computer. You can name it ScreenRecorderExtension (or any name you prefer).

    2. Inside this folder, create the following files:

      • manifest.json
      • popup.html
      • popup.js
      • icon.png (Place your icon image here)

    Step 2: Define the Extension Manifest

    The manifest.json file tells Chrome about your extension, what permissions it needs, and how it's structured.

    Create manifest.json with the following content:

    {
      "manifest_version": 3,
      "name": "Screen Recorder",
      "description": "Record your screen with great quality.",
      "version": "1.0",
      "action": {
        "default_popup": "popup.html",
        "default_icon": "icon.png"
      },
      "permissions": [
        "tabCapture",
        "downloads"
      ],
      "host_permissions": ["*://*/*"]
    }
    

    Explanation:

    • manifest_version: Should be 3 for the latest Chrome Extension manifest version.

    • name, description, version: Basic information about your extension.

    • action: Specifies the extension's toolbar icon and popup UI.

    • permissions:

      • "tabCapture": Allows the extension to capture the content of a tab.
      • "downloads": Allows the extension to save files to the user's computer.
    • host_permissions: Needed for tabCapture API in Manifest V3.


    Step 3: Create the Popup UI

    The popup.html file defines the user interface that appears when you click the extension's icon.

    Create popup.html with the following content:

    <!DOCTYPE html>
    <html>
    <head>
      <title>Screen Recorder</title>
      <style>
        body {
          font-family: Arial, sans-serif;
          width: 200px;
          margin: 0;
          padding: 10px;
        }
        #timer {
          font-size: 18px;
          margin-bottom: 10px;
        }
        button {
          width: 100%;
          padding: 10px;
          font-size: 16px;
          margin-bottom: 5px;
        }
      </style>
    </head>
    <body>
      <div id="timer">00:00:00</div>
      <button id="startBtn">Record</button>
      <button id="stopBtn" disabled>Finish</button>
    
      <script src="popup.js"></script>
    </body>
    </html>
    

    Explanation:

    • #timer: Displays the elapsed recording time.
    • #startBtn: The "Record" button to start recording.
    • #stopBtn: The "Finish" button to stop recording and save the video. Disabled by default.

    Step 4: Implement the Recording Logic

    The popup.js file contains the logic to handle screen recording, the timer, and saving the video file.

    Create popup.js with the following content:

    let mediaRecorder;
    let recordedChunks = [];
    let startTime;
    let timerInterval;
    let stream;
    
    const startBtn = document.getElementById('startBtn');
    const stopBtn = document.getElementById('stopBtn');
    const timerDisplay = document.getElementById('timer');
    
    startBtn.addEventListener('click', async () => {
      startBtn.disabled = true;
      stopBtn.disabled = false;
    
      // Start the timer
      startTime = Date.now();
      timerInterval = setInterval(updateTimer, 1000);
    
      // Start capturing the screen
      try {
        stream = await chrome.tabCapture.capture({
          audio: true,
          video: true
        });
    
        if (stream) {
          mediaRecorder = new MediaRecorder(stream, {
            mimeType: 'video/webm;codecs=vp9'
          });
    
          mediaRecorder.ondataavailable = handleDataAvailable;
          mediaRecorder.start();
        } else {
          console.error('Error: Unable to capture the screen.');
        }
      } catch (err) {
        console.error('Error starting screen capture:', err);
      }
    });
    
    stopBtn.addEventListener('click', () => {
      stopBtn.disabled = true;
      startBtn.disabled = false;
    
      // Stop the timer
      clearInterval(timerInterval);
      timerDisplay.textContent = '00:00:00';
    
      // Stop recording
      mediaRecorder.stop();
      stream.getTracks().forEach(track => track.stop());
    });
    
    function handleDataAvailable(event) {
      if (event.data.size > 0) {
        recordedChunks.push(event.data);
    
        // Save the video file
        const blob = new Blob(recordedChunks, {
          type: 'video/webm'
        });
    
        const url = URL.createObjectURL(blob);
        const filename = `ScreenRecording_${new Date().toISOString().replace(/[:.]/g, '-')}.webm`;
    
        chrome.downloads.download({
          url: url,
          filename: filename,
          saveAs: true
        }, () => {
          // Cleanup
          URL.revokeObjectURL(url);
          recordedChunks = [];
        });
      }
    }
    
    function updateTimer() {
      const elapsed = Date.now() - startTime;
      const hrs = Math.floor(elapsed / 3600000).toString().padStart(2, '0');
      const mins = Math.floor((elapsed % 3600000) / 60000).toString().padStart(2, '0');
      const secs = Math.floor((elapsed % 60000) / 1000).toString().padStart(2, '0');
    
      timerDisplay.textContent = `${hrs}:${mins}:${secs}`;
    }
    

    Explanation:

    • Variables:
      • mediaRecorder: Controls the recording process.
      • recordedChunks: Stores the recorded data.
      • startTime, timerInterval: Used for the timer display.
      • stream: The captured media stream.
    • Event Listeners:
      • startBtn: Initializes screen capture and recording.
      • stopBtn: Stops recording and saves the video.
    • handleDataAvailable(event):
      • Called when recorded data is available.
      • Saves the recorded video using the Chrome Downloads API.
    • updateTimer():
      • Updates the timer display every second.

    Note:

    • The recorded video is saved in WebM format with VP9 codec for high quality.
    • The filename includes a timestamp to prevent overwriting files.

    Step 5: Add an Icon for the Extension

    Place an image file named icon.png in your extension's directory. This will be used as the extension's icon displayed in the Chrome toolbar.

    • The recommended size is 128x128 pixels.
    • You can create a simple icon or download one from free icon sites.

    Step 6: Load the Extension into Chrome

    1. Open Google Chrome.

    2. Navigate to chrome://extensions/

    3. Enable "Developer mode" by toggling the switch in the top right corner.

    4. Click on "Load unpacked" and select your extension's directory (ScreenRecorderExtension).

    5. The extension should now appear in your list of extensions and its icon should show up in the toolbar.


    Step 7: Test the Extension

    1. Click on the extension's icon in the Chrome toolbar to open the popup.

    2. Click "Record" to start recording your current tab.

      • The timer should start counting.
      • A red circle may appear on the tab to indicate it's being recorded.
    3. Perform any actions you want to record on the tab.

    4. Click "Finish" when you're done recording.

      • The timer resets.
      • A save dialog should appear to save the recorded video.
    5. Choose a save location and save the video file.

    6. Play the saved video file to verify that the recording was successful and in good quality.


    Additional Considerations

    • Permissions Prompt: The first time you use the extension, Chrome may prompt you to allow screen capture permissions.

    • Audio Recording: The tabCapture API can capture audio from the tab. If you don't need audio, you can set audio: false in the capture options.

    • Recording Quality: You can adjust the recording quality by changing the mimeType and codecs in the MediaRecorder options. Be aware that higher quality may result in larger file sizes.

    • Error Handling: The provided code has basic error handling via console.error. For a production extension, consider adding user-friendly error messages in the UI.

    • Cross-Origin Content: Some content may not be captured due to cross-origin restrictions.


    Conclusion

    You've now created a Chrome extension that records your screen with great quality, includes a timer, and allows you to save the recording to your computer. Feel free to customize and enhance the extension to suit your needs.


    Disclaimer: This sample extension is for personal use and educational purposes. Before distributing or publishing a Chrome extension, ensure it complies with the Chrome Web Store policies and consider security implications.