Bilibili 视频截图按钮

在投稿时间之后显示一个截屏按钮,点击后复制到粘贴板

  1. // ==UserScript==
  2. // @name Bilibili 视频截图按钮
  3. // @namespace http://tampermonkey.net/
  4. // @version 0.8.0.8.0.8
  5. // @description 在投稿时间之后显示一个截屏按钮,点击后复制到粘贴板
  6. // @author 0808
  7. // @match http*://www.bilibili.com/*
  8. // @icon https://www.bilibili.com/favicon.ico
  9. // @grant none
  10. // @license MIT
  11. // ==/UserScript==
  12.  
  13. (function () {
  14. "use strict";
  15.  
  16. /** 配置选项 **/
  17. const CONFIG = {
  18. logPrefix: "[视频截图按钮]", // 日志前缀
  19. buttonText: "截屏",
  20. buttonClass: "screenshotBtn08",
  21. buttonStyle: {
  22. backgroundColor: 'rgba(0,174,236, 0.5)',
  23. transition: 'background-color 0.3s',
  24. color: '#ffffff',
  25. fontSize: '15px',
  26. cursor: 'pointer',
  27. borderRadius: '10px',
  28. border: '0px solid #ffffff',
  29. paddingLeft: '10px',
  30. paddingRight: '10px',
  31. marginBottom: '2px'
  32. },
  33. hoverStyle: {
  34. backgroundColor: 'rgba(0,174,236, 1)'
  35. },
  36. pubDateSelector: '.pubdate-ip',
  37. checkInterval: 3000,
  38. buttonAdded: false // 标记按钮是否已添加
  39. };
  40.  
  41. /** 封装 console.log,自动添加前缀 **/
  42. function log(message) {
  43. console.log(`${CONFIG.logPrefix} ${message}`);
  44. }
  45.  
  46. /** 主初始化函数 **/
  47. function init() {
  48. log("脚本初始化");
  49. FindvideoEle();
  50. }
  51.  
  52. /** 查找视频元素并添加截图按钮 **/
  53. function FindvideoEle() {
  54. function f() {
  55. let videos = document.getElementsByTagName('video');
  56. if (videos.length > 0 && !CONFIG.buttonAdded) {
  57. addScreenShotEle(videos[0]); // 只处理第一个视频元素
  58. CONFIG.buttonAdded = true; // 标记按钮已添加
  59. clearInterval(interval); // 停止定时器
  60. }
  61. }
  62. const interval = setInterval(f, CONFIG.checkInterval);
  63. }
  64.  
  65. /** 添加截图按钮 **/
  66. function addScreenShotEle(videoElement) {
  67. let SsIDname = videoElement.id + "_Sshot";
  68. if (document.getElementById(SsIDname) === null) {
  69. let SsHtml = document.createElement("button");
  70. SsHtml.textContent = CONFIG.buttonText;
  71. SsHtml.className = CONFIG.buttonClass;
  72.  
  73. // 设置按钮样式
  74. Object.assign(SsHtml.style, CONFIG.buttonStyle);
  75.  
  76. // 添加悬停效果
  77. SsHtml.addEventListener("mouseover", function (event) {
  78. SsHtml.style.backgroundColor = CONFIG.hoverStyle.backgroundColor;
  79. });
  80. SsHtml.addEventListener("mouseout", function (event) {
  81. SsHtml.style.backgroundColor = CONFIG.buttonStyle.backgroundColor;
  82. });
  83.  
  84. // 找到投稿时间的元素
  85. let pubDateElement = document.querySelector(CONFIG.pubDateSelector);
  86. if (pubDateElement) {
  87. SsHtml.setAttribute("id", SsIDname);
  88. pubDateElement.insertAdjacentElement('afterend', SsHtml); // 在投稿时间元素后插入按钮
  89. log("截图按钮已添加");
  90. }
  91.  
  92. // 添加点击事件
  93. SsHtml.addEventListener("click", function (event) {
  94. event.stopPropagation();
  95. takeScreenshot(videoElement);
  96. });
  97. } else {
  98. log("截图按钮已存在,跳过添加");
  99. }
  100. }
  101.  
  102. /** 截图并复制到剪贴板 **/
  103. function takeScreenshot(videoElement) {
  104. var myCanvas = document.createElement('canvas');
  105. myCanvas.width = videoElement.videoWidth;
  106. myCanvas.height = videoElement.videoHeight;
  107. var ctx = myCanvas.getContext('2d');
  108. ctx.drawImage(videoElement, 0, 0, videoElement.videoWidth, videoElement.videoHeight);
  109. myCanvas.toBlob(function (blob) {
  110. navigator.clipboard.write([
  111. new ClipboardItem({ 'image/png': blob })
  112. ]).then(function () {
  113. log('截图已复制到剪贴板');
  114. }).catch(function (err) {
  115. log('截图复制失败: ' + err);
  116. });
  117. });
  118. }
  119.  
  120. // 执行初始化
  121. init();
  122. })();

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址