replace jQuery

Inserts chinese hosted jQuery into your page

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

  1. // ==UserScript==
  2. // @name replace jQuery
  3. // @name:en replace jQuery
  4. // @name:zh 代替jQuery
  5. // @match *://*/*
  6. // @run-at document-start
  7. // @version 0.1
  8. // @license MIT
  9. // @author Brock Adams and hamsolo474
  10. // @description Inserts chinese hosted jQuery into your page
  11.  
  12. // @namespace https://greasyfork.org/users/809044
  13. // ==/UserScript==
  14.  
  15. /*--- Important!
  16. (1) We need another add-on, besides Greasemonkey, to
  17. disable the old, undesired script. # no we dont, this will be run in china behind the Great Firewall
  18. (2) The DOM is not available yet
  19. (@run-at == document-start).
  20. (3) We cannot use a loop to check for the DOM because
  21. loading is halted while the loop runs.
  22. (4) setTimeout() and setInterval() are not fast enough due
  23. to minimum interval clamping. By the time they detect
  24. the DOM, scripts that we need to precede may have
  25. loaded.
  26. (5) Therefor, we use a "set Zero Timeout" function as
  27. explained by David Baron at
  28. http://dbaron.org/log/20100309-faster-timeouts .
  29. (6) By the time FF reports that the `document.head` is
  30. available, several head elements have loaded!
  31. (Is this a bug?)
  32. That means that if any dependent scripts are loaded
  33. before we can inject our jQuery version, then we must
  34. also reload those dependent scripts.
  35. */
  36.  
  37. ////// setZeroTimeout() implementation: BEGIN
  38.  
  39. /*--- Only add setZeroTimeout to the window object, and hide
  40. everything else in a closure.
  41. */
  42. ( function () {
  43. var timeouts = [];
  44. var messageName = "zero-timeout-message";
  45.  
  46. /*--- Like setTimeout, but only takes a function argument.
  47. There's no time argument (always zero) and no arguments.
  48. You have to use a closure.
  49. */
  50. function setZeroTimeout(fn) {
  51. timeouts.push(fn);
  52. window.postMessage(messageName, "*");
  53. }
  54.  
  55. function handleMessage(event) {
  56. if (event.source == window && event.data == messageName) {
  57. event.stopPropagation();
  58. if (timeouts.length > 0) {
  59. var fn = timeouts.shift();
  60. fn();
  61. }
  62. }
  63. }
  64.  
  65. window.addEventListener ("message", handleMessage, true);
  66.  
  67. // Add the one thing we want added to the window object.
  68. window.setZeroTimeout = setZeroTimeout;
  69. })();
  70.  
  71. ////// setZeroTimeout() implementation: END
  72.  
  73. /*--- Now wait for the DOM and then add our version of jQuery,
  74. first thing.
  75. */function SearchForDOM () {
  76.  
  77. var targetNode;
  78. if (typeof document.head == "undefined")
  79. targetNode = document.querySelector ("head, body");
  80. else
  81. targetNode = document.head;
  82. if (targetNode) {
  83.  
  84. var scriptNode = document.createElement ("script");
  85. scriptNode.src = 'https://cdn.bootcdn.net/ajax/libs/jquery/3.6.0/jquery.min.js';
  86. targetNode.appendChild (scriptNode);
  87.  
  88. /*--- By the time FF reports that the head element is
  89. available, a key dependent script has loaded!
  90. So, we reload it here, so that it can run with jQuery
  91. available.
  92. */
  93. var scriptNode = document.createElement ("script");
  94. scriptNode.src = location.protocol
  95. + '\/\/' + location.host
  96. + '/content/js/stub.js?v=49f661361016';
  97. targetNode.appendChild (scriptNode);
  98. }
  99. else
  100. setZeroTimeout (SearchForDOM);
  101. }
  102.  
  103. SearchForDOM ();