复制剪贴板保护 修改版

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

目前为 2024-08-08 提交的版本。查看 最新版本

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