A Universal Script to Re-Enable the Selection and Copying

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

目前为 2021-06-06 提交的版本。查看 最新版本

  1. // ==UserScript==
  2. // @name A Universal Script to Re-Enable the Selection and Copying
  3. // @version 1.2.4
  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://greasyfork.org/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 {' +
  15. '-webkit-touch-callout: text !important; -webkit-user-select: text !important; ' +
  16. '-khtml-user-select: text !important; -moz-user-select: text !important; ' +
  17. '-ms-user-select: text !important; user-select: text !important;}';
  18.  
  19. function enableSelectClickCopy() {
  20.  
  21. var mKey = 'dqzadwpujtct';
  22. var enabledSCC = 'enabledSCC_' + mKey;
  23. var nonFalseFunc = 'nff_' + mKey;
  24. var rvSCC = '___returnValue_' + mKey + '___';
  25.  
  26. if (window[enabledSCC]) return;
  27. window[enabledSCC] = true;
  28.  
  29. Event.prototype.preventDefault = (function(f) {
  30. var eys = ['copy', 'contextmenu', 'select', 'selectstart', 'dragstart', 'onbeforecopy'];
  31. return function() {
  32. if (eys.indexOf(this.type) >= 0) return;
  33. return f.apply(this);
  34. }
  35. })(Event.prototype.preventDefault);
  36.  
  37. var exs = ['copy', 'contextmenu', 'select', 'selectstart', 'dragstart', 'onbeforecopy'];
  38. Object.defineProperty(Event.prototype, "returnValue", {
  39.  
  40. get() {
  41. return rvSCC in this ? this[rvSCC] : true;
  42. },
  43. set(newValue) {
  44. if (exs.indexOf(this.type) < 0 && newValue === false) this.preventDefault();
  45. this[rvSCC] = newValue;
  46. },
  47. enumerable: true,
  48. configurable: true
  49. });
  50.  
  51. var ezs = ['copy', 'contextmenu', 'select', 'selectstart', 'dragstart', 'beforecopy'];
  52. var eventsCount = ezs.length;
  53.  
  54. function universaler(originalFunc) {
  55. return function wrapFunction(ev) {
  56. var pName = 'on' + ev.type;
  57. var func = this[pName];
  58. if (typeof func == 'function' && func.name == 'wrapFunction') {
  59. var res = originalFunc.apply(this, arguments);
  60. if (res !== false) {
  61. originalFunc[nonFalseFunc] = true;
  62. this[pName] = originalFunc;
  63. return res;
  64. }
  65. }
  66. }
  67. }
  68.  
  69. function disableAll(event) {
  70. var elmNode = event.target
  71. while (elmNode && elmNode.nodeType > 0) {
  72. var pName = 'on' + event.type
  73. var f = elmNode[pName];
  74. if (f && f[nonFalseFunc] !== true) {
  75. var nf = universaler(f);
  76. nf[nonFalseFunc] = true;
  77. elmNode[pName] = nf;
  78. }
  79. elmNode = elmNode.parentNode;
  80. }
  81. }
  82.  
  83.  
  84. for (var i = 0; i < eventsCount; i++) {
  85. var event = ezs[i];
  86. document.addEventListener(event, disableAll, true);
  87. }
  88.  
  89.  
  90. }
  91.  
  92. function loadedHandler() {
  93. if (wasRun) return;
  94. wasRun = true;
  95. console.log("Select-click-copy Enabler");
  96. try {
  97. document.removeEventListener('beforescriptexecute', loadedHandler, true);
  98. document.removeEventListener('beforeload', loadedHandler, true);
  99. document.removeEventListener('DOMContentLoaded', loadedHandler, true);
  100. } catch (e) {} finally {
  101. appendScript(document);
  102. }
  103. }
  104.  
  105. function isDocumentObj(x) {
  106. return x && x.nodeType == 9
  107. }
  108.  
  109.  
  110. function isHTMLElementObj(x) {
  111. return x && x.nodeType == 1
  112. }
  113.  
  114. function makeScriptElm(documentObject) {
  115. if (!isDocumentObj(documentObject)) return null;
  116. var s = documentObject.createElement('script');
  117. s.type = 'text/javascript';
  118. s.innerHTML = '(' + enableSelectClickCopy.toString() + ')()';
  119. return s
  120. }
  121.  
  122. function appendScript(documentObject) {
  123. try {
  124. if (!isDocumentObj(documentObject)) return;
  125. var container = documentObject.head || documentObject.body;
  126. if (container) container.appendChild(makeScriptElm(documentObject));
  127. } catch (e) {}
  128. }
  129.  
  130.  
  131. function appendCssEnabler(container) {
  132. if (!isHTMLElementObj(container)) return;
  133. try {
  134. var css = document.createElement('style');
  135. css.type = 'text/css';
  136. css.innerHTML = cssStyle;
  137. container.appendChild(css);
  138. } catch (e) {}
  139. }
  140.  
  141. wasRun = false;
  142.  
  143. if (document != null) {
  144.  
  145. enableSelectClickCopy(); //try direct call
  146.  
  147. try {
  148. if ('onbeforescriptexecute' in document) {
  149. //for firefox
  150. document.addEventListener('beforescriptexecute', loadedHandler, true);
  151. } else {
  152. //for chrome and opera
  153. document.addEventListener('beforeload', loadedHandler, true);
  154. }
  155.  
  156. } catch (e) {}
  157.  
  158. document.addEventListener('DOMContentLoaded', function() {
  159. //in case all previous efforts fail
  160. loadedHandler();
  161. appendCssEnabler(document.documentElement || document.body);
  162. }, true);
  163. }
  164. })();