复制剪贴板保护 修改版

管理网页剪贴板的写入行为&禁用音视频内容预载及自动播放。

目前为 2024-06-09 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name 复制剪贴板保护 修改版
  3. // @author 人民的勤务员 <millle@foxmail.com> Sky,仰望星空
  4. // @description 管理网页剪贴板的写入行为&禁用音视频内容预载及自动播放。
  5. // @version 1.1
  6. // @include *
  7. // @run-at document-start
  8. // @namespace https://greasyfork.org/users/1169082
  9. // @license MIT
  10. // ==/UserScript==
  11. //在其他地方下载的脚本,作者不知道是谁很不错,防范牛皮癣 。用Chatgpt修改了下,让脚本在PC端浏览时可以使用快捷键复制
  12. !function () {
  13. // 定义唯一键,用于防止重复执行
  14. const key = encodeURIComponent('剪贴板保护&禁用预载:执行判断');
  15.  
  16. // 如果已经执行过,直接返回
  17. if (window[key]) { return; }
  18.  
  19. try {
  20. // 标记已经执行过
  21. window[key] = true;
  22.  
  23. // 初始化变量
  24. let red = true;
  25. let green = false;
  26. let orange = false;
  27.  
  28. // 获取页面中的所有视频元素
  29. var videoTags = document.getElementsByTagName("video");
  30.  
  31. // 创建开关按钮元素
  32. const sw = document.createElement("div");
  33. // 设置按钮样式
  34. sw.style = 'position:fixed!important;bottom:30%;right:10px;z-index:2147483647;width:18px;height:18px;opacity:0.4;border-radius:9px;background:red;visibility:hidden';
  35. // 将按钮添加到页面中
  36. document.body.appendChild(sw);
  37.  
  38. // 对所有视频元素进行处理,禁用预加载和自动播放
  39. for (var i = 0; i < videoTags.length; i++) {
  40. videoTags[i].setAttribute("preload", "metadata");
  41. videoTags[i].removeAttribute("autoplay");
  42. };
  43.  
  44. // 获取页面中的所有音频元素
  45. var audioTags = document.getElementsByTagName("audio");
  46.  
  47. // 对所有音频元素进行处理,禁用预加载和自动播放
  48. for (var i = 0; i < audioTags.length; i++) {
  49. audioTags[i].setAttribute("preload", "metadata");
  50. audioTags[i].removeAttribute("autoplay");
  51. };
  52.  
  53. // 显示提示消息的函数
  54. function Toast(msg, duration, backgroundColor, textColor) {
  55. duration = isNaN(duration) ? 3000 : duration;
  56. backgroundColor = backgroundColor || 'rgba(0, 0, 0, 0.7)';
  57. textColor = textColor || 'rgb(255, 255, 255)';
  58.  
  59. var m = document.createElement('div');
  60. m.innerHTML = msg;
  61. m.style.cssText = "max-width:60%;min-width: 150px;padding:0 14px;height: 40px;color: " + textColor + ";line-height: 40px;text-align: center;border-radius: 12px;position: fixed;top: 95%;left: 50%;transform: translate(-50%, -50%);z-index: 2147483647;background: " + backgroundColor + ";font-size: 16px;";
  62. document.body.appendChild(m);
  63.  
  64. setTimeout(function () {
  65. var d = 0.5;
  66. m.style.transition = '-webkit-transform ' + d + 's ease-in, opacity ' + d + 's ease-in';
  67. m.style.opacity = '0';
  68. setTimeout(function () {
  69. document.body.removeChild(m)
  70. }, d * 1000);
  71. }, duration);
  72. }
  73.  
  74.  
  75. // 处理复制事件的函数
  76. function pc(e) {
  77. if (red) {
  78. var confirmed = window.confirm("允许网页复制吗?");
  79. if (confirmed) {
  80. // 用户点击了确认按钮
  81. // 执行复制操作
  82. Toast('已允许网页写入剪贴板', 500, 'rgba(0, 255, 0, 0.7)', 'rgb(255, 255, 255)');
  83. } else {
  84. // 用户点击了取消按钮
  85. // 执行阻止写入剪贴板的操作
  86. e.preventDefault();
  87. e.stopPropagation();
  88. sw.style.visibility = "visible";
  89. Toast('已阻止网页写入剪贴板', 500, 'rgba(255, 0, 0, 0.7)', 'rgb(255, 255, 255)');
  90. }
  91.  
  92. }
  93. setTimeout(function() {
  94. sw.style.visibility = "hidden";
  95. }, 4000);
  96. }
  97.  
  98.  
  99.  
  100.  
  101.  
  102. // 添加复制事件监听器
  103. document.addEventListener('copy', (e) => pc(e), { 'passive': false, 'capture': true });
  104.  
  105. // 遍历所有iframe元素,为其内容添加复制事件监听器
  106. Array.from(document.getElementsByTagName('iframe')).forEach((i) => i.contentDocument.addEventListener('copy', (e) => pc(e), { 'passive': false, 'capture': true }));
  107.  
  108. // 点击开关按钮的事件处理
  109. sw.addEventListener('click', function (e) {
  110. if (!orange) {
  111. sw.style.background = red ? 'green' : 'red';
  112. red = !red;
  113. green = !green;
  114. } else {
  115. sw.style.background = 'red';
  116. red = !red;
  117. orange = !orange;
  118. }
  119. }, { 'passive': true });
  120.  
  121. // 右键菜单事件处理
  122. document.addEventListener('contextmenu', function (e) {
  123. if (!green) {
  124. sw.style.visibility = "visible"
  125. sw.style.background = 'orange';
  126. red = false;
  127. orange = true;
  128. setTimeout(function () { sw.style.visibility = "hidden"; }, 4000);
  129. }
  130. }, { 'passive': true });
  131.  
  132. // 复制完成后的事件处理
  133. document.addEventListener('copy', function (e) {
  134. if (orange) {
  135. sw.style.background = 'red';
  136. red = true;
  137. orange = false;
  138. sw.style.visibility = "hidden"
  139. }
  140. }, { 'passive': true });
  141.  
  142. // 添加键盘事件监听器,支持快捷键复制
  143. document.addEventListener('keydown', function (e) {
  144. if (e.ctrlKey && e.key === 'c') {
  145. sw.style.visibility = "visible"
  146. sw.style.background = 'orange';
  147. red = false;
  148. orange = true;
  149. }
  150. }, { 'passive': true });
  151.  
  152. // 鼠标按下事件处理
  153. document.addEventListener('mousedown', function (e) {
  154. if (orange) {
  155. sw.style.background = 'red';
  156. red = true;
  157. orange = false;
  158. }
  159. }, { 'passive': true });
  160.  
  161. } catch (err) {
  162. console.log('剪贴板保护&禁用预载:', err);
  163. }
  164.  
  165. }();