Universal Select-click-copy Enabler

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

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

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