复制剪贴板保护 修改版

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

目前为 2024-07-19 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name 复制剪贴板保护 修改版
  3. // @namespace https://github.com/10086100886
  4. // @author 人民的勤务员 <toniaiwanowskiskr47@gmail.com> Sky,仰望星空
  5. // @description 管理网页剪贴板的写入行为&禁用音视频内容预载及自动播放。
  6. // @version 1.2.2.7
  7. // @include *
  8. // @run-at document-start
  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. const selection = window.getSelection().toString();
  79. var confirmed = window.confirm('允许网页复制以下内容吗?\n' + selection);
  80. if (confirmed) {
  81. // 用户点击了确认按钮
  82. // 执行复制操作
  83. Toast('已允许网页写入剪贴板', 500, 'rgba(0, 255, 0, 0.7)', 'rgb(255, 255, 255)');
  84. } else {
  85. // 用户点击了取消按钮
  86. // 执行阻止写入剪贴板的操作
  87. e.preventDefault();
  88. e.stopPropagation();
  89. sw.style.visibility = "visible";
  90. Toast('已阻止网页写入剪贴板', 500, 'rgba(255, 0, 0, 0.7)', 'rgb(255, 255, 255)');
  91. }
  92.  
  93. }
  94. setTimeout(function() {
  95. sw.style.visibility = "hidden";
  96. }, 4000);
  97. }
  98.  
  99.  
  100.  
  101.  
  102.  
  103. // 添加复制事件监听器
  104. document.addEventListener('copy', (e) => pc(e), { 'passive': false, 'capture': true });
  105.  
  106. // 遍历所有iframe元素,为其内容添加复制事件监听器
  107. Array.from(document.getElementsByTagName('iframe')).forEach((i) => i.contentDocument.addEventListener('copy', (e) => pc(e), { 'passive': false, 'capture': true }));
  108.  
  109. // 点击开关按钮的事件处理
  110. sw.addEventListener('click', function (e) {
  111. if (!orange) {
  112. sw.style.background = red ? 'green' : 'red';
  113. red = !red;
  114. green = !green;
  115. } else {
  116. sw.style.background = 'red';
  117. red = !red;
  118. orange = !orange;
  119. }
  120. }, { 'passive': true });
  121.  
  122. // 右键菜单事件处理
  123. document.addEventListener('contextmenu', function (e) {
  124. if (!green) {
  125. sw.style.visibility = "visible"
  126. sw.style.background = 'orange';
  127. red = false;
  128. orange = true;
  129. setTimeout(function () { sw.style.visibility = "hidden"; }, 4000);
  130. }
  131. }, { 'passive': true });
  132.  
  133. // 复制完成后的事件处理
  134. document.addEventListener('copy', function (e) {
  135. if (orange) {
  136. sw.style.background = 'red';
  137. red = true;
  138. orange = false;
  139. sw.style.visibility = "hidden"
  140. }
  141. }, { 'passive': true });
  142.  
  143. // 添加键盘事件监听器,支持快捷键复制
  144. document.addEventListener('keydown', function (e) {
  145. if (e.ctrlKey && e.key === 'c') {
  146. sw.style.visibility = "visible"
  147. sw.style.background = 'orange';
  148. red = false;
  149. orange = true;
  150. }
  151. }, { 'passive': true });
  152.  
  153. // 鼠标按下事件处理
  154. document.addEventListener('mousedown', function (e) {
  155. if (orange) {
  156. sw.style.background = 'red';
  157. red = true;
  158. orange = false;
  159. }
  160. }, { 'passive': true });
  161.  
  162. } catch (err) {
  163. console.log('剪贴板保护&禁用预载:', err);
  164. }
  165.  
  166. }();