Udemy Transcript Copy Button

Show copy button next to AI Assistant after transcript is available

Bu betiği kurabilmeniz için Tampermonkey, Greasemonkey ya da Violentmonkey gibi bir kullanıcı betiği eklentisini kurmanız gerekmektedir.

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

Bu betiği kurabilmeniz için Tampermonkey ya da Violentmonkey gibi bir kullanıcı betiği eklentisini kurmanız gerekmektedir.

Bu betiği kurabilmeniz için Tampermonkey ya da Userscripts gibi bir kullanıcı betiği eklentisini kurmanız gerekmektedir.

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

Bu komut dosyasını yüklemek için bir kullanıcı komut dosyası yöneticisi uzantısı yüklemeniz gerekecek.

(Zaten bir kullanıcı komut dosyası yöneticim var, kurmama izin verin!)

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.

(Zateb bir user-style yöneticim var, yükleyeyim!)

// ==UserScript==
// @name         Udemy Transcript Copy Button
// @namespace    http://tampermonkey.net/
// @version      1.1
// @description  Show copy button next to AI Assistant after transcript is available
// @author       Imran Pollob
// @license      MIT
// @match        https://www.udemy.com/course/*/learn/lecture/*
// @grant        GM_setClipboard
// ==/UserScript==

(function() {
    'use strict';

    let lastUrl = location.href;

    function tryGetTranscript() {
        let startTime = Date.now();

        function check() {
            const elapsed = (Date.now() - startTime) / 1000;
            if (elapsed > 10) {
                console.log("⏱️ Stopped trying to find transcript panel after 10 seconds.");
                return;
            }

            const panel = document.querySelector('[data-purpose="transcript-panel"]');
            const spans = panel ? panel.querySelectorAll('span[data-purpose="cue-text"]') : null;

            if (panel && spans && spans.length > 0) {
                insertCopyButton(panel);
            } else {
                setTimeout(check, 1000);
            }
        }

        check();
    }

    function insertCopyButton(panel) {
        if (document.getElementById('copy-transcript-btn')) return; // avoid duplicates
        const btn = document.createElement('button');
        btn.id = 'copy-transcript-btn';
        btn.innerText = '📋 Copy';
        btn.style.marginLeft = '10px';
        btn.style.padding = '5px 10px';
        btn.style.backgroundColor = 'white';
        btn.style.borderColor = '#007791';
        btn.style.borderRadius = '5px';
        btn.style.cursor = 'pointer';

        btn.onclick = function() {
            const spans = panel.querySelectorAll('span[data-purpose="cue-text"]');
            const text = Array.from(spans).map(span => span.textContent.trim()).join(' ');
            GM_setClipboard(text);
            console.log("✅ Transcript copied to clipboard!");
        };

        const aiAssistantSpan = Array.from(document.querySelectorAll('span')).find(span => span.textContent.trim() === 'Transcript');
        if (aiAssistantSpan && aiAssistantSpan.parentNode) {
            aiAssistantSpan.parentNode.insertBefore(btn, aiAssistantSpan.nextSibling);
        } else {
            // fallback if AI Assistant not found
            document.body.appendChild(btn);
        }
    }

    function observeUrlChange() {
        const observer = new MutationObserver(() => {
            if (location.href !== lastUrl) {
                lastUrl = location.href;
                console.log("🔄 URL changed, checking for transcript.");
                const btn = document.getElementById('copy-transcript-btn');
                if (btn) btn.remove();
                tryGetTranscript();
            }
        });
        observer.observe(document.body, { childList: true, subtree: true });
    }

    window.addEventListener('load', () => {
        tryGetTranscript();
        observeUrlChange();
    });
})();