Skip to content

Instantly share code, notes, and snippets.

@zhuochun
Last active December 22, 2024 07:12
Show Gist options
  • Select an option

  • Save zhuochun/f06e9fa18d9b7fab843e5dd8116312ea to your computer and use it in GitHub Desktop.

Select an option

Save zhuochun/f06e9fa18d9b7fab843e5dd8116312ea to your computer and use it in GitHub Desktop.

Revisions

  1. zhuochun revised this gist Dec 22, 2024. 1 changed file with 3 additions and 3 deletions.
    6 changes: 3 additions & 3 deletions copy-youtube-transcript.js
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,7 @@
    // ==UserScript==
    // @name YouTube Transcript to Clipboard
    // @namespace http://tampermonkey.net/
    // @version 1.2
    // @version 1.3
    // @description Adds a button to copy the transcript to the clipboard.
    // @author You
    // @match https://www.youtube.com/*
    @@ -56,11 +56,11 @@
    }

    // Extract title
    const titleElement = document.querySelector('#title');
    const titleElement = document.querySelector('h1.ytd-watch-metadata');
    const title = titleElement ? titleElement.textContent.trim() : 'Untitled';

    // Extract text from the transcript panel
    let text = transcriptPanel.querySelector('#content')?.textContent || '';
    let text = transcriptPanel.querySelector('#segments-container')?.textContent || '';

    // Split by newline, trim lines, remove empty lines
    const lines = text
  2. zhuochun revised this gist Dec 22, 2024. 1 changed file with 8 additions and 3 deletions.
    11 changes: 8 additions & 3 deletions copy-youtube-transcript.js
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,7 @@
    // ==UserScript==
    // @name YouTube Transcript to Clipboard
    // @namespace http://tampermonkey.net/
    // @version 1.1
    // @version 1.2
    // @description Adds a button to copy the transcript to the clipboard.
    // @author You
    // @match https://www.youtube.com/*
    @@ -55,8 +55,12 @@
    return;
    }

    // Extract title
    const titleElement = document.querySelector('#title');
    const title = titleElement ? titleElement.textContent.trim() : 'Untitled';

    // Extract text from the transcript panel
    let text = transcriptPanel.textContent || '';
    let text = transcriptPanel.querySelector('#content')?.textContent || '';

    // Split by newline, trim lines, remove empty lines
    const lines = text
    @@ -84,7 +88,8 @@
    combinedLines.push(currentEntry.trim());
    }

    const finalText = combinedLines.join('\n');
    const finalText = `Title: ${title}\nTranscript:\n${combinedLines.join('\n')}`;

    GM_setClipboard(finalText);

    // Provide some feedback to the user
  3. zhuochun revised this gist Dec 22, 2024. 2 changed files with 49 additions and 1 deletion.
    49 changes: 49 additions & 0 deletions copy-hackernews-comments.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,49 @@
    // ==UserScript==
    // @name Hacker News Comment Extractor
    // @namespace http://tampermonkey.net/
    // @version 1.0
    // @description Extract and copy comments from Hacker News threads
    // @author YourName
    // @match https://news.ycombinator.com/item*
    // @grant GM_setClipboard
    // ==/UserScript==

    (function() {
    'use strict';

    // Create and add clipboard button
    const addClipboardButton = () => {
    const submissionRow = document.querySelector('tr.athing.submission');
    if (!submissionRow) return;

    const button = document.createElement('button');
    button.textContent = '📋 Copy Comments';
    button.style.marginLeft = '2px';
    button.style.cursor = 'pointer';
    button.title = 'Copy processed comments to clipboard';

    button.addEventListener('click', () => {
    const comments = Array.from(
    document.querySelectorAll('table.comment-tree .commtext.c00')
    ).map(div => div.textContent.trim())
    .filter(text => text.split(/\s+/).length >= 20)
    .map(text => text.replace(/\n+/g, '\n').trim());

    if (comments.length > 100) {
    comments.length = 100; // Limit to 100 comments
    }

    const finalText = `Summarize these comments by groups:\n\n${comments.join('\n\n')}`;
    GM_setClipboard(finalText);

    // Provide some feedback to the user
    button.textContent = '✅';
    setTimeout(() => { button.textContent = '📋'; }, 2000);
    });

    submissionRow.appendChild(button);
    };

    // Add button after page load
    window.addEventListener('load', addClipboardButton);
    })();
    1 change: 0 additions & 1 deletion copy-youtube-transcript.js
    Original file line number Diff line number Diff line change
    @@ -85,7 +85,6 @@
    }

    const finalText = combinedLines.join('\n');

    GM_setClipboard(finalText);

    // Provide some feedback to the user
  4. zhuochun revised this gist Dec 22, 2024. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions copy-youtube-transcript.js
    Original file line number Diff line number Diff line change
    @@ -5,7 +5,7 @@
    // @description Adds a button to copy the transcript to the clipboard.
    // @author You
    // @match https://www.youtube.com/*
    // @grant none
    // @grant GM_setClipboard
    // ==/UserScript==

    (function() {
    @@ -86,7 +86,7 @@

    const finalText = combinedLines.join('\n');

    await navigator.clipboard.writeText(finalText);
    GM_setClipboard(finalText);

    // Provide some feedback to the user
    btn.textContent = '✅';
  5. zhuochun revised this gist Dec 22, 2024. 1 changed file with 23 additions and 3 deletions.
    26 changes: 23 additions & 3 deletions copy-youtube-transcript.js
    Original file line number Diff line number Diff line change
    @@ -1,9 +1,9 @@
    // ==UserScript==
    // @name YouTube Transcript to Clipboard
    // @namespace http://tampermonkey.net/
    // @version 1.0
    // @version 1.1
    // @description Adds a button to copy the transcript to the clipboard.
    // @author ChatGPT
    // @author You
    // @match https://www.youtube.com/*
    // @grant none
    // ==/UserScript==
    @@ -64,7 +64,27 @@
    .map(line => line.trim())
    .filter(line => line !== '');

    const finalText = lines.join('\n');
    // Combine lines based on timestamps
    const timestampRegex = /^\d{1,2}:\d{2}(?::\d{2})?$/;
    const combinedLines = [];
    let currentEntry = '';

    for (const line of lines) {
    if (timestampRegex.test(line)) {
    if (currentEntry) {
    combinedLines.push(currentEntry.trim());
    }
    currentEntry = line; // Start a new entry with the timestamp
    } else {
    currentEntry += ` ${line}`; // Append line to the current entry
    }
    }

    if (currentEntry) {
    combinedLines.push(currentEntry.trim());
    }

    const finalText = combinedLines.join('\n');

    await navigator.clipboard.writeText(finalText);

  6. zhuochun created this gist Dec 20, 2024.
    94 changes: 94 additions & 0 deletions copy-youtube-transcript.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,94 @@
    // ==UserScript==
    // @name YouTube Transcript to Clipboard
    // @namespace http://tampermonkey.net/
    // @version 1.0
    // @description Adds a button to copy the transcript to the clipboard.
    // @author ChatGPT
    // @match https://www.youtube.com/*
    // @grant none
    // ==/UserScript==

    (function() {
    'use strict';

    // A helper function to wait for an element to appear in the DOM.
    function waitForElement(selector, timeout = 10000) {
    return new Promise((resolve, reject) => {
    const start = Date.now();

    (function check() {
    const element = document.querySelector(selector);
    if (element) {
    resolve(element);
    } else if (Date.now() - start >= timeout) {
    reject(new Error(`Element ${selector} not found within ${timeout}ms`));
    } else {
    requestAnimationFrame(check);
    }
    })();
    });
    }

    // Create the clipboard button
    function createClipboardButton() {
    const btn = document.createElement('button');
    btn.style.display = 'inline-flex';
    btn.style.alignItems = 'center';
    btn.style.justifyContent = 'center';
    btn.style.marginLeft = '8px';
    btn.style.padding = '4px';
    btn.style.border = 'none';
    btn.style.borderRadius = '2px';
    btn.style.cursor = 'pointer';
    btn.style.background = 'transparent';
    btn.style.color = 'var(--yt-spec-text-primary)';
    btn.title = 'Copy transcript';

    // Adding a clipboard icon (using an emoji for simplicity)
    btn.textContent = '📋';

    btn.addEventListener('click', async () => {
    try {
    const transcriptPanel = document.querySelector('ytd-engagement-panel-section-list-renderer[target-id="engagement-panel-searchable-transcript"]');
    if (!transcriptPanel) {
    alert("Transcript panel not found!");
    return;
    }

    // Extract text from the transcript panel
    let text = transcriptPanel.textContent || '';

    // Split by newline, trim lines, remove empty lines
    const lines = text
    .split('\n')
    .map(line => line.trim())
    .filter(line => line !== '');

    const finalText = lines.join('\n');

    await navigator.clipboard.writeText(finalText);

    // Provide some feedback to the user
    btn.textContent = '✅';
    setTimeout(() => { btn.textContent = '📋'; }, 2000);
    } catch (err) {
    console.error("Failed to copy text: ", err);
    }
    });

    return btn;
    }

    // Wait for the owner container and add the clipboard button
    waitForElement('#owner').then(ownerContainer => {
    // Ensure we only add one button if the script re-runs
    if (!ownerContainer.querySelector('.clipboard-button')) {
    const clipboardButton = createClipboardButton();
    clipboardButton.classList.add('clipboard-button');
    ownerContainer.appendChild(clipboardButton);
    }
    }).catch((error) => {
    console.warn(error);
    });

    })();