Unlimit-Web

解除网页限制: 恢复文本的选中和复制, 过滤文本小尾巴, 恢复右键菜单. Remove webpage restrictions: restore the selection and copy of text, clear the text tail, and restore the right-click menu.

  1. // ==UserScript==
  2. // @name Unlimit-Web
  3. // @description 解除网页限制: 恢复文本的选中和复制, 过滤文本小尾巴, 恢复右键菜单. Remove webpage restrictions: restore the selection and copy of text, clear the text tail, and restore the right-click menu.
  4. // @version 17.1
  5. // @author xcanwin
  6. // @namespace https://github.com/xcanwin/Unlimit-Web/
  7. // @supportURL https://github.com/xcanwin/Unlimit-Web/
  8. // @icon data:image/svg+xml;utf8,<svg xmlns="http://www.w3.org/2000/svg" height="24" width="24" stroke-width="2" fill="none" stroke="currentColor"><path d="M9 19c-5 1.5-5-2.5-7-3m14 6v-3.87a3.37 3.37 0 0 0-.94-2.61c3.14-.35 6.44-1.54 6.44-7A5.44 5.44 0 0 0 20 4.77 5.07 5.07 0 0 0 19.91 1S18.73.65 16 2.48a13.38 13.38 0 0 0-7 0C6.27.65 5.09 1 5.09 1A5.07 5.07 0 0 0 5 4.77a5.44 5.44 0 0 0-1.5 3.78c0 5.42 3.3 6.61 6.44 7A3.37 3.37 0 0 0 9 18.13V22"></path></svg>
  9. // @license GPL-2.0-only
  10. // @match *://www.zhihu.com/*
  11. // @match *://blog.csdn.net/*
  12. // @match *://www.bilibili.com/*
  13. // @match *://www.cnblogs.com/*
  14. // @match *://www.360doc.com/*
  15. // @match *://blog.51cto.com/*
  16. // @match *://guofeng.yuedu.163.com/*
  17. // @match *://www.kuwo.cn/*
  18. // @match *://chuangshi.qq.com/*
  19. // @match *://read.qidian.com/*
  20. // @match *://dafrok.github.io/*
  21. // @match *://shushan.zhangyue.net/*
  22. // @match *://aqistudy.cn/*
  23. // @match *://www.xuexila.com/*
  24. // @match *://www.51test.net/*
  25. // @match *://www.laokaoya.com/*
  26. // @match *://utaten.com/*
  27. // @match *://book.qq.com/*
  28. // @match *://doc.mbalib.com/*
  29. // @match *://www.oh100.com/*
  30. // @match *://51test.net/*
  31. // @match *://www.cspengbo.com/*
  32. // @match *://www.diyifanwen.com/*
  33. // @match *://www.ahsrst.cn/*
  34. // @match *://kt250.com/*
  35. // @match *://boke112.com/*
  36. // @match *://*/*
  37. // @grant GM_getValue
  38. // @grant GM_setValue
  39. // @grant GM_registerMenuCommand
  40. // @grant GM_unregisterMenuCommand
  41. // @grant GM_addStyle
  42. // @run-at document-end
  43. // ==/UserScript==
  44.  
  45. (function() {
  46. 'use strict';
  47.  
  48. const $ = (Selector, el) => (el || document).querySelector(Selector);
  49. const $$ = (Selector, el) => (el || document).querySelectorAll(Selector);
  50.  
  51. const muob = (Selector, el, func) => {
  52. const observer = new MutationObserver((mutationsList, observer2) => {
  53. for (let mutation of mutationsList) {
  54. if (mutation.type === 'childList') {
  55. const target = mutation.target.querySelector(Selector);
  56. if (target && !target.hasAttribute('data-duplicate')) {
  57. target.setAttribute('data-duplicate', 'true');
  58. func(target);
  59. }
  60. }
  61. }
  62. });
  63. observer.observe(el, {
  64. childList: true,
  65. subtree: true
  66. });
  67. };
  68.  
  69. /*黑名单: 需解除限制*/
  70. const block_list = {
  71. // 域名
  72. domain: {
  73. // 初始化,首次安装插件时使用此列表,之后使用插件存储的列表
  74. init: ["www.zhihu.com", "blog.csdn.net","www.bilibili.com","www.cnblogs.com","www.360doc.com","blog.51cto.com","guofeng.yuedu.163.com","www.kuwo.cn","chuangshi.qq.com","read.qidian.com","dafrok.github.io","shushan.zhangyue.net","aqistudy.cn","www.xuexila.com","www.51test.net","www.laokaoya.com","utaten.com","book.qq.com","doc.mbalib.com","www.oh100.com","51test.net","www.cspengbo.com","www.diyifanwen.com","www.ahsrst.cn","kt250.com","boke112.com"],
  75. // 硬编码,除了使用插件存储的列表,每次也会使用此硬编码列表
  76. hard: [],
  77. },
  78. };
  79.  
  80. /*白名单: 指的是放行,无需解除限制*/
  81. const allow_list = {
  82. // 网页元素名称
  83. element: ['script', 'style', 'video'],
  84. // 网页元素id
  85. id: ['video'],
  86. // 网页元素className
  87. className: ['video'],
  88. };
  89.  
  90. const symbol = ["❎", "✅"];
  91. const symbol2 = ["未勾选", "已勾选"];
  92. let mc = [];
  93.  
  94. const sv = (key, value = "") => {
  95. GM_setValue(key, value);
  96. };
  97.  
  98. const gv = (key, value = "") => {
  99. return GM_getValue(key, value);
  100. };
  101.  
  102. const purify_style = `
  103. .unslcl {
  104. /* 浅色模式下的文本选中样式 */
  105. @media (prefers-color-scheme: light) {
  106. :not(foo):not(bar):not(baz):not(qux)::selection {
  107. background-color: #007BFF !important;
  108. color: white !important;
  109. }
  110. }
  111.  
  112. /* 深色模式下的文本选中样式 */
  113. @media (prefers-color-scheme: dark) {
  114. :not(foo):not(bar):not(baz):not(qux)::selection {
  115. background-color: #5DACDD !important;
  116. color: black !important;
  117. }
  118. }
  119. }
  120. `;
  121.  
  122. /*枚举网页元素*/
  123. const eNumUnLimit = (EL = document) => {
  124. $('html').classList.add('unslcl');
  125. $$("*", EL).forEach(unLimit);
  126. try {
  127. console.clear = () => {};
  128. window.debugger = () => {};
  129. } catch (e) {
  130. }
  131. };
  132.  
  133. /*判断是否包含*/
  134. const isIn = (el, list, type) => {
  135. /*
  136. 例如 'video' 包含于 ['hello', 'video']
  137. 例如 'video' 模糊包含于 ['hello', 'good_player_top']
  138. 例如 'good_video_top' 特殊包含于 ['hello', 'video']
  139. */
  140. switch (type) {
  141. case 'fuzzy': // 模糊包含
  142. return list.some(item => item === el || item.includes(el));
  143. case 'fancy': // 特殊包含
  144. return list.some(item => item === el || el.includes(item));
  145. default: // 正常包含
  146. return list.some(item => item === el);
  147. }
  148. };
  149.  
  150. /*解除限制*/
  151. const unLimit = (el = null) => {
  152. if (
  153. isIn(el.nodeName.toLowerCase(), allow_list.element)
  154. || isIn(el.id?.toString().toLowerCase(), allow_list.id, 'fancy')
  155. || isIn(el.className?.toString().toLowerCase(), allow_list.className, 'fancy')
  156. ) return;
  157.  
  158. [
  159. "user-select", "-webkit-user-select", "-moz-user-select", "-ms-user-select", "-khtml-user-select",
  160. ].forEach(xcanwin => {
  161. const ec = el.childNodes;
  162. const j1 = ec && ec.length == 1 && ec[0] && ec[0].nodeType && ec[0].nodeType == 3;
  163. const style = document.defaultView.getComputedStyle(el, null)[xcanwin];
  164. const j2 = style && style != 'auto';
  165. if (j1 || j2){
  166. // 处理第一个子标签是text类型的标签 或者 处理select值被修改过的标签
  167. el.style.setProperty(xcanwin, "unset", "important");
  168. }
  169. });
  170.  
  171. [
  172. "onselect", "onselectstart", "onselectionchange",
  173. "oncopy", "onbeforecopy",
  174. "onpaste", "onbeforepaste", "oncut", "onbeforecut",
  175. "onpointercancel", "onpointerdown", "onpointerenter", "onpointerleave", "onpointerlockchange", "onpointerlockerror", "onpointermove", "onpointerout", "onpointerover", "onpointerrawupdate", "onpointerup",
  176. ].forEach(xcanwin => {
  177. el[xcanwin] = e => {
  178. // 处理能影响文本的事件
  179. e.stopImmediatePropagation();
  180. }
  181. });
  182.  
  183. [
  184. "onmouseenter", "onmousedown", "onmouseup", "onmouseout", "onmouseleave", "onmouseover",
  185. ].forEach(xcanwin => {
  186. el[xcanwin] = e => {
  187. if ([ "P" ].indexOf(e.target.nodeName) >=0 && e.button == 0) {
  188. // 处理单击左键和滑动左键下的html文本标签
  189. e.stopImmediatePropagation();
  190. }
  191. }
  192. });
  193.  
  194. [
  195. "onkeypress", "onkeyup", "onkeydown",
  196. ].forEach(xcanwin => {
  197. el[xcanwin] = e => {
  198. const keyCode = e.keyCode || e.which || e.charCode;
  199. const ctrlKey = e.ctrlKey || e.metaKey;
  200. if ((ctrlKey && keyCode == 67) || keyCode == 123) {
  201. // 处理ctrl+c和F12
  202. e.stopImmediatePropagation();
  203. }
  204. }
  205. });
  206.  
  207. [
  208. "oncontextmenu",
  209. ].forEach(xcanwin => {
  210. el[xcanwin] = e => {
  211. if (e.target && e.target.points == undefined){
  212. // 处理普通的单击右键,跳过滑动右键
  213. e.stopImmediatePropagation();
  214. }
  215. }
  216. });
  217. };
  218.  
  219. /*加入自动破解列表*/
  220. const switchAuto = (domain) => {
  221. let autolist = JSON.parse(gv("ul_autolist", "[]"));
  222. domain = domain ? domain : getdomain();
  223. if (isIn(domain, autolist)) {
  224. autolist = autolist.filter(el => el !== domain);
  225. } else {
  226. autolist.push(domain);
  227. }
  228. sv("ul_autolist", JSON.stringify(autolist));
  229. rmc();
  230. eNumUnLimit();
  231. };
  232.  
  233. /*查看自动破解列表*/
  234. const showAuto = () => {
  235. prompt("自动破解列表", gv("ul_autolist", "[]"));
  236. };
  237.  
  238. /*初始化自动破解列表*/
  239. const initAutoList = () => {
  240. const init = block_list.domain.init;
  241. //为空或者为[]时,说明首次运行,进行初始化
  242. if (gv("ul_autolist", "[]") === "[]") {
  243. sv("ul_autolist", JSON.stringify(init));
  244. }
  245. //解析移除时,进行初始化
  246. try {
  247. JSON.parse(gv("ul_autolist", "[]"));
  248. } catch (e) {
  249. sv("ul_autolist", JSON.stringify(init));
  250. }
  251. };
  252.  
  253. /*取消注册菜单*/
  254. const unrmc = () => {
  255. mc.forEach(x => GM_unregisterMenuCommand(x));
  256. };
  257.  
  258. /*注册菜单*/
  259. const rmc = () => {
  260. unrmc();
  261. let isauto;
  262. const autolist = JSON.parse(gv("ul_autolist", "[]"));
  263. const domain = getdomain();
  264. if (isIn(domain, autolist.concat(block_list.domain.hard))) {
  265. isauto = 1;
  266. } else {
  267. isauto = 0;
  268. }
  269. mc.push(GM_registerMenuCommand(`查看自动破解列表`, () => showAuto()));
  270. mc.push(GM_registerMenuCommand(`临时破解:${domain}`, () => eNumUnLimit()));
  271. mc.push(GM_registerMenuCommand(`自动破解:${domain} ${symbol[isauto]}${symbol2[isauto]}`, () => switchAuto(domain)));
  272. };
  273.  
  274. const getdomain = () => {
  275. return (new URL(location.href)).hostname;
  276. };
  277.  
  278. const main = () => {
  279. initAutoList();
  280. rmc();
  281. const autolist = JSON.parse(gv("ul_autolist", "[]"));
  282. const domain = getdomain();
  283. if (isIn(domain, autolist.concat(block_list.domain.hard))) {
  284. eNumUnLimit();
  285. setInterval(() => eNumUnLimit(), 3000);
  286. muob(`*`, $(`body`), unLimit);
  287. }
  288. };
  289.  
  290. GM_addStyle(purify_style);
  291. main();
  292.  
  293. })();