Youtube transcript copy

9/10/2025, 8:56:08 PM

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name        Youtube transcript copy
// @namespace   Violentmonkey Scripts
// @match       https://www.youtube.com/watch?v=*
// @grant       none
// @run-at      document-idle
// @version     1.0.0
// @author      Sue Smith MD
// @license     AGPL-3.0; https://www.gnu.org/licenses/agpl-3.0.html#license-text
// @description 9/10/2025, 8:56:08 PM
// ==/UserScript==

(()=>{let G=()=>{if(document.getElementById("ytb-copy-transcript-style"))return;let k=document.createElement("style");k.id="ytb-copy-transcript-style",k.textContent=`
.ytb-copy-transcript-btn { font-family: "YouTube Sans","Roboto",sans-serif; font-size: 1.5rem; line-height: 2.8rem; font-weight: 700; overflow: hidden; display: inline-flex; align-items: center; justify-content: center; height: 36px; padding: 0 12px; border-radius: 18px; background: transparent; color: currentColor; border: 1px solid rgba(255,255,255,0.2); cursor: pointer; gap: 6px; }
.ytb-copy-transcript-btn:hover { background: rgba(255,255,255,0.06); }
.ytb-copy-transcript-toast { position: fixed; left: 50%; bottom: 24px; transform: translateX(-50%); background: rgba(28,28,28,0.92); color: #fff; padding: 8px 12px; border-radius: 8px; font: 500 12px/16px Roboto, sans-serif; z-index: 2147483647; opacity: 0; transition: opacity 120ms ease-out; }
`,document.head.appendChild(k)},U=(k,z)=>{let A;return(...D)=>{if(A!==void 0)window.clearTimeout(A);A=window.setTimeout(()=>k(...D),z)}},V=(k)=>{if("requestIdleCallback"in window)window.requestIdleCallback(k);else setTimeout(k,0)},X=(k)=>{return Array.from(k.querySelectorAll(".segment-text")).map((D)=>D.textContent?.trim()).filter((D)=>Boolean(D&&D.length>0)).join(`
`)},Y=async(k)=>{try{return await navigator.clipboard.writeText(k),!0}catch(z){let A=document.createElement("textarea");A.value=k,A.style.position="fixed",A.style.top="0",A.style.left="0",A.style.opacity="0",document.body.appendChild(A),A.focus(),A.select();try{return document.execCommand("copy")}finally{A.remove()}}},Z=async()=>{let k=document.querySelector("#segments-container");if(!k){console.warn("No transcript container found; open transcript first.");return}let z=X(k);if(!z){console.warn("No transcript text found in #segments-container");return}let A=await Y(z.replaceAll("[Music]",""));if(j(A?"Copied transcript":"Copy failed"),!A)console.warn("Copy failed. Transcript follows:",z)},J=()=>{let k=["ytd-engagement-panel-title-header-renderer #menu ytd-menu-renderer #top-level-buttons-computed","ytd-engagement-panel-title-header-renderer #top-level-buttons-computed","#panel ytd-engagement-panel-title-header-renderer #top-level-buttons-computed"];for(let z of k){let A=document.querySelector(z);if(A)return A}return null},_=()=>{G();let k=J();if(!k)return;if(k.querySelector('[data-ytb-copy-transcript="true"]'))return;let z=document.createElement("button");z.setAttribute("data-ytb-copy-transcript","true"),z.type="button",z.title="Copy transcript",z.textContent="Copy",z.className="ytb-copy-transcript-btn",z.addEventListener("click",()=>{Z()}),k.appendChild(z)},$=()=>{let k=document.querySelector('ytd-engagement-panel-section-list-renderer[target-id="engagement-panel-searchable-transcript"]');if(k)return k;return document.querySelector("ytd-engagement-panel-section-list-renderer")},F=U(()=>V(_),50),B=()=>{let z=document.querySelector("#primary-button > ytd-button-renderer > yt-button-shape > button");if(z)z.addEventListener("click",()=>{requestAnimationFrame(()=>{F()})});let A=()=>{let N=$();if(!N)return!1;let Q=new MutationObserver(()=>{F()});Q.observe(N,{childList:!0,subtree:!0});let q=()=>{if(J()?.querySelector('[data-ytb-copy-transcript="true"]'))return Q.disconnect(),!0;return!1},R=setInterval(()=>{if(q())clearInterval(R)},300);return!0};F();let D=0,K=setInterval(()=>{if(D+=1,A())clearInterval(K);if(D>=10)clearInterval(K)},400),L=()=>{F(),setTimeout(()=>void A(),600)};window.addEventListener("yt-navigate-finish",L),window.addEventListener("yt-page-data-updated",L)},j=(k)=>{G();let z=document.createElement("div");z.className="ytb-copy-transcript-toast",z.textContent=k,document.body.appendChild(z),requestAnimationFrame(()=>{z.style.opacity="1"}),setTimeout(()=>{z.style.opacity="0",setTimeout(()=>z.remove(),150)},1200)};if(!window.__ytb_copy_transcript_bound)window.__ytb_copy_transcript_bound=!0,document.querySelector("#expand")?.click(),document.querySelector("#primary-button > ytd-button-renderer > yt-button-shape > button")?.click(),B()})();