YouTube 字幕快速複製

快速複製 YouTube 字幕並將其寫入剪貼簿,以便在GPT上進行分析。

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name               Quick Copy YouTube Subtitles
// @name:zh-TW         YouTube 字幕快速複製
// @namespace          wellstsai.com
// @version            v20241128.2
// @license            BSD
// @description        Quickly copy subtitles from YouTube and write them to the clipboard for easy analysis on GPT.
// @description:zh-TW  快速複製 YouTube 字幕並將其寫入剪貼簿,以便在GPT上進行分析。
// @author             WellsTsai
// @match              https://*.youtube.com/*
// @icon               data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==
// @grant              none
// ==/UserScript==

(function() {
    'use strict';

    const GPT_PROMPT = '使用"正體中文" 與 "臺灣詞彙",幫我消化時間軸後,我不需要知道詳細的時間,其中,如果影片有比較,請幫我列為表格,如果有方法,請幫我以條列式列出方法,請將影片的字幕檔轉換為詳細描述影片的重點與內容:';
    const COPY_NOTIFICATION_TEXT = '已複製';

    const copyToClipboard = text => {
        navigator.clipboard.writeText(text + "\n" + GPT_PROMPT).then(showCopyNotification);
    };

    const showCopyNotification = () => {
        const notification = document.createElement('div');
        notification.innerText = COPY_NOTIFICATION_TEXT;
        Object.assign(notification.style, {
            'font-size': '2em',
            position: 'fixed',
            bottom: '20px', right: '20px', padding: '10px',
            backgroundColor: 'rgba(0, 0, 0, 0.7)',
            color: 'white',
            borderRadius: '5px',
            zIndex: '1000'
        });
        document.body.appendChild(notification);
        setTimeout(() => document.body.removeChild(notification), 1500);
    };

    document.addEventListener('keydown', e => {
        if (!(e.ctrlKey && e.key === 'c') || window.getSelection().toString()) return; // Detect if Ctrl + C is pressed and no text is selected.
        e.preventDefault();

        const segmentsContainer = document.querySelector('#segments-container');
        const transcriptButton = document.querySelector('ytd-video-description-transcript-section-renderer button');

        if (segmentsContainer) {
            copyToClipboard(segmentsContainer.innerText);
        } else if (transcriptButton) {
            transcriptButton.click();
            setTimeout(() => {
                const newSegmentsContainer = document.querySelector('#segments-container');
                if (newSegmentsContainer) copyToClipboard(newSegmentsContainer.innerText);
            }, 1000);
        }
    });
})();