复制剪贴板保护 修改版

管理网页剪贴板的写入行为 防止未经授权的自动复制 &禁用音视频内容预载及自动播放。

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

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