YouTube Engagement Panel Text Extractor

提取并复制 ytd-engagement-panel-section-list-renderer 标签下的文本

当前为 2024-12-10 提交的版本,查看 最新版本

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

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

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey,才能安装此脚本。

您需要先安装用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         YouTube Engagement Panel Text Extractor
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  提取并复制 ytd-engagement-panel-section-list-renderer 标签下的文本
// @author       您的名字
// @match        https://www.youtube.com/*
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    // 创建一个容器来保持两个按钮在一起
    const buttonContainer = document.createElement('div');
    buttonContainer.style.position = 'fixed';
    buttonContainer.style.top = '50%'; // 设置为页面高度的50%
    buttonContainer.style.right = '10px'; // 距离右侧10像素
    buttonContainer.style.transform = 'translateY(-50%)'; // 垂直居中对齐
    buttonContainer.style.display = 'flex';
    buttonContainer.style.flexDirection = 'column';
    buttonContainer.style.alignItems = 'flex-end'; // 将子元素靠右对齐

    // 创建两个按钮
    const button1 = document.createElement('button');
    button1.innerText = '提取文字';
    button1.style.marginBottom = '10px'; // 给两个按钮之间添加一些间距
    
    const button2 = document.createElement('button');
    button2.innerText = '复制文字';
    button2.disabled = true; // 初始状态下禁用复制按钮

    // 添加到容器
    buttonContainer.appendChild(button1);
    buttonContainer.appendChild(button2);

    // 添加到页面
    document.body.appendChild(buttonContainer);

    let extractedText = '';

    // 按钮1点击事件 - 提取文字
    button1.addEventListener('click', () => {
        // 查找所有ytd-engagement-panel-section-list-renderer元素并提取其文本内容
        const elements = document.querySelectorAll('ytd-engagement-panel-section-list-renderer');
        extractedText = Array.from(elements).map(el => el.textContent.trim()).join('\n');
        
        console.log('已提取的文字:', extractedText); // 输出到控制台供调试
        
        // 启用复制按钮
        button2.disabled = false;
    });

    // 按钮2点击事件 - 复制文字
    button2.addEventListener('click', () => {
        if (extractedText) {
            navigator.clipboard.writeText(extractedText).then(() => {
                console.log('文本已复制到剪贴板');
                alert('文本已复制到剪贴板');
            }).catch(err => {
                console.error('无法复制文本: ', err);
            });
        }
    });
})();