A Universal Script to Re-Enable the Selection and Copying

Enables select, right-click, copy and drag on pages that disable them.

当前为 2021-06-10 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name A Universal Script to Re-Enable the Selection and Copying
  3. // @version 1.3.2
  4. // @description Enables select, right-click, copy and drag on pages that disable them.
  5. // @include /^https?\:\/\//
  6. // @grant none
  7. // @run-at document-start
  8. // @namespace https://gf.qytechs.cn/users/371179
  9. // ==/UserScript==
  10. "use strict";
  11. (function() {
  12. var wasRun = false;
  13.  
  14. var cssStyle = '*, body *, div, span, body *::before, body *::after, *:hover, *:link, *:visited, *:active , *[style], *[class]{' +
  15. '-webkit-touch-callout: default !important; -webkit-user-select: auto !important; ' +
  16. '-khtml-user-select: auto !important; -moz-user-select: auto !important; ' +
  17. '-ms-user-select: auto !important; user-select: auto !important;}' +
  18. 'html body *:hover>img[src]{' +
  19. 'pointer-events:auto;' +
  20. '}';
  21.  
  22. function enableSelectClickCopy() {
  23.  
  24. var mKey = 'dqzadwpujtct';
  25. var enabledSCC = '___enabledSCC_' + mKey + '___';
  26. var nonFalseFunc = '___nff_' + mKey + '___';
  27. var rvSCC = '___returnValue_' + mKey + '___';
  28.  
  29. if (window[enabledSCC]) return;
  30. window[enabledSCC] = true;
  31.  
  32. Event.prototype.preventDefault = (function(f) {
  33. var eys = ['copy', 'contextmenu', 'select', 'selectstart', 'dragstart', 'beforecopy'];
  34. return function() {
  35. if (eys.indexOf(this.type) >= 0) return;
  36. return f.apply(this);
  37. }
  38. })(Event.prototype.preventDefault);
  39.  
  40. var exs = ['copy', 'contextmenu', 'select', 'selectstart', 'dragstart', 'beforecopy'];
  41. Object.defineProperty(Event.prototype, "returnValue", {
  42.  
  43. get() {
  44. return rvSCC in this ? this[rvSCC] : true;
  45. },
  46. set(newValue) {
  47. if (exs.indexOf(this.type) < 0 && newValue === false) this.preventDefault();
  48. this[rvSCC] = newValue;
  49. },
  50. enumerable: true,
  51. configurable: true
  52. });
  53.  
  54. var ezs = ['copy', 'contextmenu', 'select', 'selectstart', 'dragstart', 'beforecopy'];
  55. var eventsCount = ezs.length;
  56.  
  57. function universaler(originalFunc) {
  58. return function wrapFunction(ev) {
  59. var pName = 'on' + ev.type;
  60. var func = this[pName];
  61. if (typeof func == 'function' && func.name == 'wrapFunction') {
  62. var res = originalFunc.apply(this, arguments);
  63. if (res !== false) {
  64. originalFunc[nonFalseFunc] = true;
  65. this[pName] = originalFunc;
  66. return res;
  67. }
  68. }
  69. }
  70. }
  71.  
  72. function disableAll(event) {
  73. var elmNode = event.target
  74. while (elmNode && elmNode.nodeType > 0) {
  75. var pName = 'on' + event.type
  76. var f = elmNode[pName];
  77. if (f && f[nonFalseFunc] !== true) {
  78. var nf = universaler(f);
  79. nf[nonFalseFunc] = true;
  80. elmNode[pName] = nf;
  81. }
  82. elmNode = elmNode.parentNode;
  83. }
  84. }
  85.  
  86.  
  87. for (var i = 0; i < eventsCount; i++) {
  88. var event = ezs[i];
  89. document.addEventListener(event, disableAll, true);
  90. }
  91.  
  92.  
  93. var _alert = window.alert
  94. var _mAlert = null;
  95. if (_alert && typeof _alert == 'function') {
  96. _mAlert = function alert(msg) {
  97. setTimeout(() => {
  98. alert._click.isDisabled() ? console.log("alert msg disabled: ", msg) : alert.alert.apply(this, arguments)
  99. }, 9);
  100. };
  101.  
  102. _mAlert._click = {
  103. isDisabled: function() {
  104. return this.status == 1 && this.last + 50 > +new Date;
  105. }
  106. }
  107. _mAlert.alert = _alert
  108.  
  109. window.alert = _mAlert
  110.  
  111.  
  112. var cid_mouseup = 0;
  113.  
  114.  
  115. ["mousedown", "click", "dblclick", "contextmenu"].forEach(function(event) {
  116.  
  117. document.addEventListener(event, function(evt) {
  118. if (evt.type != "contextmenu" && evt.which != 3) return;
  119. if (cid_mouseup > 0) {
  120. clearTimeout(cid_mouseup)
  121. cid_mouseup = 0;
  122. }
  123. _mAlert._click.last = +new Date;
  124. _mAlert._click.status = 1;
  125. }, true);
  126.  
  127. })
  128.  
  129. document.addEventListener("mouseup", function(evt) {
  130. if (evt.which != 3) return;
  131. cid_mouseup = setTimeout(function() {
  132. _mAlert._click.last = +new Date;
  133. _mAlert._click.status = 0;
  134. }, 17);
  135. }, true);
  136.  
  137. }
  138. }
  139.  
  140.  
  141.  
  142. function loadedHandler() {
  143. if (wasRun) return;
  144. wasRun = true;
  145. console.log("Select-click-copy Enabler");
  146. try {
  147. document.removeEventListener('beforescriptexecute', loadedHandler, true);
  148. document.removeEventListener('beforeload', loadedHandler, true);
  149. document.removeEventListener('DOMContentLoaded', loadedHandler, true);
  150. } catch (e) {}
  151. appendScript(document);
  152.  
  153. }
  154.  
  155. function isDocumentObj(x) {
  156. return x && x.nodeType == 9
  157. }
  158.  
  159.  
  160. function isHTMLElementObj(x) {
  161. return x && x.nodeType == 1
  162. }
  163.  
  164. function makeScriptElm(documentObject) {
  165. if (!isDocumentObj(documentObject)) return null;
  166. var s = documentObject.createElement('script');
  167. s.type = 'text/javascript';
  168. s.innerHTML = '(' + enableSelectClickCopy.toString() + ')()';
  169. return s
  170. }
  171.  
  172. function appendScript(documentObject) {
  173. try {
  174. if (!isDocumentObj(documentObject)) return;
  175. var container = documentObject.head || documentObject.body;
  176. if (container) container.appendChild(makeScriptElm(documentObject));
  177. } catch (e) {}
  178. }
  179.  
  180.  
  181. function appendCssEnabler(container) {
  182. if (!isHTMLElementObj(container)) return;
  183. try {
  184. var css = document.createElement('style');
  185. css.type = 'text/css';
  186. css.innerHTML = cssStyle;
  187. container.appendChild(css);
  188. } catch (e) {}
  189. }
  190.  
  191. wasRun = false;
  192.  
  193. if (document != null) {
  194.  
  195. try {
  196. enableSelectClickCopy(); //try direct call
  197. } catch (e) {}
  198.  
  199. try {
  200. if ('onbeforescriptexecute' in document) {
  201. //for firefox
  202. document.addEventListener('beforescriptexecute', loadedHandler, true);
  203. } else {
  204. //for chrome and opera
  205. document.addEventListener('beforeload', loadedHandler, true);
  206. }
  207.  
  208. } catch (e) {}
  209.  
  210. function onReady() {
  211.  
  212. //in case all previous efforts fail
  213. try {
  214. loadedHandler();
  215. } catch (e) {}
  216.  
  217. appendCssEnabler(document.documentElement || document.body);
  218. }
  219.  
  220. if (document.readyState !== 'loading') {
  221. onReady();
  222. } else {
  223. document.addEventListener('DOMContentLoaded', onReady);
  224. }
  225.  
  226.  
  227. }
  228. })();

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址