Udemy Transcript Copier

Adds a button to copy the transcript from Udemy video lectures

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

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

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         Udemy Transcript Copier
// @namespace    https://greasyfork.org/users/KosherKale
// @version      1.0.0
// @description  Adds a button to copy the transcript from Udemy video lectures
// @author       kosherkale
// @license      GPL-3.0
// @match        https://*.udemy.com/course/*
// @grant        GM_setClipboard
// @run-at       document-idle
// ==/UserScript==

(function () {
    'use strict';

    const BUTTON_ID = 'ute-export-btn';
    const PANEL_SELECTOR = '[data-purpose="transcript-panel"]';
    const CUE_SELECTOR = '[data-purpose="cue-text"]';

    function getTranscriptText() {
        const cues = document.querySelectorAll(CUE_SELECTOR);
        return Array.from(cues)
            .map(el => el.textContent.trim())
            .filter(Boolean)
            .join(' ');
    }

    function getVideoTitle() {
        // Try the lesson heading first, then fall back to page title
        const heading = document.querySelector('[data-purpose="lead-title"], h1[class*="curriculum-item-link--title"]');
        if (heading) return heading.textContent.trim();
        return document.title.replace(' | Udemy', '').trim();
    }

    function downloadText(text, filename) {
        const blob = new Blob([text], { type: 'text/plain;charset=utf-8' });
        const url = URL.createObjectURL(blob);
        const a = document.createElement('a');
        a.href = url;
        a.download = filename;
        a.click();
        URL.revokeObjectURL(url);
    }

    function createButton() {
        const btn = document.createElement('button');
        btn.id = BUTTON_ID;
        btn.textContent = 'Copy Transcript';
        btn.title = 'Copy transcript to clipboard or download as .txt';
        Object.assign(btn.style, {
            display: 'inline-flex',
            alignItems: 'center',
            gap: '6px',
            margin: '8px 0 4px 0',
            padding: '6px 14px',
            background: '#a435f0',
            color: '#fff',
            border: 'none',
            borderRadius: '4px',
            fontSize: '13px',
            fontWeight: '700',
            cursor: 'pointer',
            width: '100%',
            justifyContent: 'center',
            boxSizing: 'border-box',
        });

        // Right-click or Ctrl+click → download; left-click → clipboard
        btn.addEventListener('click', (e) => {
            const text = getTranscriptText();
            if (!text) {
                btn.textContent = 'No transcript found';
                setTimeout(() => { btn.textContent = 'Copy Transcript'; }, 2000);
                return;
            }
            if (e.ctrlKey || e.metaKey) {
                // Download as file
                const title = getVideoTitle().replace(/[\\/:*?"<>|]/g, '_').slice(0, 80);
                downloadText(text, `${title}_transcript.txt`);
                btn.textContent = 'Downloaded!';
            } else {
                // Copy to clipboard
                if (typeof GM_setClipboard === 'function') {
                    GM_setClipboard(text);
                } else {
                    navigator.clipboard.writeText(text).catch(() => {
                        // Fallback for older browsers
                        const ta = document.createElement('textarea');
                        ta.value = text;
                        document.body.appendChild(ta);
                        ta.select();
                        document.execCommand('copy');
                        document.body.removeChild(ta);
                    });
                }
                btn.textContent = 'Copied!';
            }
            setTimeout(() => { btn.textContent = 'Copy Transcript'; }, 2000);
        });

        btn.addEventListener('mouseenter', () => {
            btn.style.background = '#8710d8';
        });
        btn.addEventListener('mouseleave', () => {
            btn.style.background = '#a435f0';
        });

        return btn;
    }

    function injectButton(panel) {
        if (document.getElementById(BUTTON_ID)) return;
        const btn = createButton();
        panel.insertAdjacentElement('beforebegin', btn);
    }

    function tryInject() {
        const panel = document.querySelector(PANEL_SELECTOR);
        if (panel) injectButton(panel);
    }

    // Watch for the transcript panel being added to the DOM (SPA navigation)
    const observer = new MutationObserver(() => tryInject());
    observer.observe(document.body, { childList: true, subtree: true });

    // Also try immediately in case panel is already present
    tryInject();
})();