Copy Transcript Text Button for Coursera

Adds a button to copy transcript text to clipboard on Coursera course video pages

You will need to install an extension such as Tampermonkey, Greasemonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey or Userscripts to install this script.

You will need to install an extension such as Tampermonkey to install this script.

You will need to install a user script manager extension to install this script.

(У мене вже є менеджер скриптів, дайте мені встановити його!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(I already have a user style manager, let me install it!)

// ==UserScript==
// @name         Copy Transcript Text Button for Coursera
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  Adds a button to copy transcript text to clipboard on Coursera course video pages
// @author       Your Name
// @match        https://*.coursera.org/learn/*/lecture/*
// @grant        none
// @run-at       document-end
// @license     MIT
// ==/UserScript==

(function() {
    'use strict';

    // Function to copy text to clipboard
    function copyToClipboard(text) {
       text = text.slice(203);
        const textArea = document.createElement('textarea');
        textArea.value = text;
        document.body.appendChild(textArea);
        textArea.select();
        document.execCommand('copy');
        document.body.removeChild(textArea);
    }

    // Function to add the copy button
    function addButton() {
        // Ensure the transcript lighter element exists


        // Create button element
        const button = document.createElement('button');
        button.textContent = 'Copy Transcript Text';
        button.style.position = 'absolute';
        button.style.top = '10px';
        button.style.right = '150px';
        button.style.padding = '10px';
        button.style.backgroundColor = '#007bff';
        button.style.color = '#fff';
        button.style.border = 'none';
        button.style.borderRadius = '5px';
        button.style.cursor = 'pointer';
        button.style.zIndex = '1000';

        // Add button to the page
        document.body.appendChild(button);

        var transcriptLighter
        // Add click event listener to the button
        button.addEventListener('click', () => {
        if (!transcriptLighter) {
            transcriptLighter = document.querySelector('div[id$="-panel-TRANSCRIPT"]');
        }
            const text = transcriptLighter.textContent.trim();
            if (text) {
                copyToClipboard(text);
            } 
        });
    }

    // Wait until the page is fully loaded
    window.addEventListener('load', () => {
        setTimeout(addButton,2000); // Delay to ensure the transcript is loaded
    });

})();