YouTube Engagement Panel Text Extractor

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

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

  1. // ==UserScript==
  2. // @name YouTube Engagement Panel Text Extractor
  3. // @namespace http://tampermonkey.net/
  4. // @version 0.7
  5. // @description 提取并复制 ytd-engagement-panel-section-list-renderer 标签下的文本
  6. // @author 微信:civilpy
  7. // @match https://www.youtube.com/*
  8. // @grant none
  9. // ==/UserScript==
  10.  
  11. (function() {
  12. 'use strict';
  13.  
  14. // 创建一个容器来保持两个按钮在一起
  15. const buttonContainer = document.createElement('div');
  16. buttonContainer.style.position = 'fixed';
  17. buttonContainer.style.top = '50%'; // 设置为页面高度的50%
  18. buttonContainer.style.right = '10px'; // 距离右侧10像素
  19. buttonContainer.style.transform = 'translateY(-50%)'; // 垂直居中对齐
  20. buttonContainer.style.display = 'flex';
  21. buttonContainer.style.flexDirection = 'column';
  22. buttonContainer.style.alignItems = 'flex-end'; // 将子元素靠右对齐
  23.  
  24. // 创建两个按钮
  25. const button1 = document.createElement('button');
  26. button1.innerText = '提取文字';
  27. button1.style.marginBottom = '10px'; // 给两个按钮之间添加一些间距
  28. const button2 = document.createElement('button');
  29. button2.innerText = '下载文案';
  30. button2.disabled = true; // 初始状态下禁用复制按钮
  31. button2.style.marginBottom = '10px'; // 给两个按钮之间添加一些间距
  32.  
  33. // 创建导航到网站的按钮
  34. const button3 = document.createElement('button');
  35. button3.innerText = '我的网站';
  36.  
  37. // 设置按钮点击事件,当点击时导航到指定的URL
  38. button3.addEventListener('click', function() {
  39. // 替换为你的实际网址
  40. window.location.href = 'https://intumu.com';
  41. });
  42.  
  43.  
  44. // 添加到容器
  45. buttonContainer.appendChild(button1);
  46. buttonContainer.appendChild(button2);
  47. buttonContainer.appendChild(button3);
  48.  
  49. // 添加到页面
  50. document.body.appendChild(buttonContainer);
  51.  
  52. let extractedText = '';
  53.  
  54. button1.addEventListener('click', () => {
  55. // 查找所有ytd-engagement-panel-section-list-renderer元素并提取其文本内容
  56. const elements = document.querySelectorAll('ytd-engagement-panel-section-list-renderer');
  57. // 提取文本并去除前后空白,过滤掉空行或仅有空白字符的行
  58. extractedText = Array.from(elements)
  59. .map(el => el.textContent.trim()) // 去除每段文字前后的空白字符
  60. .filter(line => line.replace(/\s+/g, '').length > 0) // 使用正则表达式替换所有空白字符,并检查是否为空字符串
  61. .join('\n'); // 将所有非空行用换行符连接起来
  62.  
  63. console.log('已提取的文字:', extractedText); // 输出到控制台供调试
  64.  
  65. // 启用复制按钮
  66. button2.disabled = false;
  67. });
  68.  
  69. button2.addEventListener('click', () => {
  70. if (extractedText) {
  71. // 清洗文本,去掉空行
  72. const cleanedText = extractedText.split('\n').filter(line => line.trim() !== '').join('\n');
  73. // 获取当前时间戳
  74. const timestamp = new Date().toISOString().replace(/[:.]/g, '-'); // 替换非法文件名字符
  75.  
  76. // 创建文件内容
  77. const fileContent = "分析总结以下文本:\n" + extractedText;
  78.  
  79. // 创建隐藏的<a>元素用于下载
  80. const element = document.createElement('a');
  81. const blob = new Blob([fileContent], { type: 'text/plain' });
  82. const url = URL.createObjectURL(blob);
  83. element.href = url;
  84. element.download = `${timestamp}.txt`; // 设置文件名为时间戳.txt
  85.  
  86. // 将<a>元素添加到DOM中并触发点击事件
  87. document.body.appendChild(element);
  88. element.click();
  89.  
  90. // 清理
  91. document.body.removeChild(element);
  92. URL.revokeObjectURL(url);
  93.  
  94. console.log('文件已准备下载');
  95. alert('文件已准备下载');
  96. }
  97. });
  98. })();