Hook

hook function in crawling

  1. // ==UserScript==
  2. // @name Hook
  3. // @namespace https://github.com/JaycgbEDC/Script/Js
  4. // @version 0.3
  5. // @license Unlicense
  6. // @description hook function in crawling
  7. // @author Kribe
  8. // @homepage https://github.com/JaycgbEDC/Script/blob/main/Js/Hook/Hook.user.js
  9. // @supportURL https://github.com/JaycgbEDC/Script/issues
  10. // @match *://*/*
  11. // @icon https://s1.imagehub.cc/images/2023/08/12/default.th.jpeg
  12. // @grant unsafeWindow
  13. // @grant GM_registerMenuCommand
  14. // @grant GM_log
  15. // @grant GM_setValue
  16. // @grant GM_getValue
  17. // @run-at document-start
  18. // ==/UserScript==
  19.  
  20. (function () {
  21. 'use strict';
  22. const hookFunctionDebugger = () => {
  23. Function.prototype.constructor_ = Function.prototype.constructor;
  24. Function.prototype.constructor = function (a) {
  25. if(a == "debugger") {
  26. return function (){};
  27. }
  28. return Function.prototype.constructor_(a);
  29. };
  30. };
  31.  
  32. const hookCookie = () => {
  33. var cookie_cache = document.cookie;
  34. Object.defineProperty(document, 'cookie', {
  35. get: function () {
  36. return cookie_cache;
  37. },
  38.  
  39. set: function (val) {
  40. GM_log('Setting cookie', val);
  41. // 填写cookie名
  42. if (val.indexOf('m=') != -1) {
  43. debugger;
  44. }
  45. var cookie = val.split(";")[0];
  46. var ncookie = cookie.split("=");
  47. var flag = false;
  48. var cache = cookie_cache.split("; ");
  49. cache = cache.map(function (a) {
  50. if (a.split("=")[0] === ncookie[0]) {
  51. flag = true;
  52. return cookie;
  53. }
  54. return a;
  55. })
  56. cookie_cache = cache.join("; ");
  57. if (!flag) {
  58. cookie_cache += cookie + "; ";
  59. }
  60. return cookie_cache;
  61. }
  62. });
  63. };
  64.  
  65. const hookSetInterval = () => {
  66. let setInterval_ = unsafeWindow.setInterval;
  67. unsafeWindow.setInterval = function() {
  68. GM_log("setInterval");
  69. return function () {};
  70. };
  71. unsafeWindow.setInterval.toString = function() {
  72. GM_log("有函数正在检测setInterval是否被hook");
  73. return setInterval_.toString();
  74. }
  75. };
  76.  
  77. const hookRandom = () => {
  78. /* hook掉随机值,比如Date、Math,方便调试 */
  79. Date.now = function now() {
  80. console.log("hook Date.now");
  81. return 1692702146230;
  82. };
  83. Date.parse = function parse() {
  84. console.log("hook Date.parse");
  85. return 1692702146230;
  86. };
  87. Date.prototype.valueOf = function () {
  88. console.log("hook Date.prototype.valueOf");
  89. return 1692702146230;
  90. };
  91. Date.prototype.getTime = function () {
  92. console.log("hook Date.prototype.getTime");
  93. return 1692702146230;
  94. };
  95. Date.prototype.toString = function () {
  96. console.log("hook Date.prototype.toString");
  97. return 1692702146230;
  98. };
  99.  
  100. Math.random = function random() {
  101. console.log("hook Math.random");
  102. return 0.08636862211354912;
  103. };
  104. };
  105.  
  106. (() => {
  107. /* 注册用户脚本菜单 */
  108. const menu1 = GM_registerMenuCommand(`${GM_getValue("hookFunctionDebugger") === true ? '✅' : '⬜'} Hook FunctionDebugger`, (event) => {
  109. if (GM_getValue("hookFunctionDebugger") === true) {
  110. GM_setValue("hookFunctionDebugger", false);
  111. } else {
  112. GM_setValue("hookFunctionDebugger", true);
  113. }
  114. window.location.reload(true);
  115. });
  116.  
  117. const menu2 = GM_registerMenuCommand(`${GM_getValue("hookCookie") === true ? '✅' : '⬜'} Hook Cookie`, (event) => {
  118. if (GM_getValue("hookCookie") === true) {
  119. GM_setValue("hookCookie", false);
  120. } else {
  121. GM_setValue("hookCookie", true);
  122. }
  123. window.location.reload(true);
  124. });
  125.  
  126. const menu3 = GM_registerMenuCommand(`${GM_getValue("hookSetInterval") === true ? '✅' : '⬜'} Hook setInterval`, (event) => {
  127. if (GM_getValue("hookSetInterval") === true) {
  128. GM_setValue("hookSetInterval", false);
  129. } else {
  130. GM_setValue("hookSetInterval", true);
  131. }
  132. window.location.reload(true);
  133. });
  134.  
  135. const menu4 = GM_registerMenuCommand(`${GM_getValue("hookRandom") === true ? '✅' : '⬜'} Hook hookRandom`, (event) => {
  136. if (GM_getValue("hookRandom") === true) {
  137. GM_setValue("hookRandom", false);
  138. } else {
  139. GM_setValue("hookRandom", true);
  140. }
  141. window.location.reload(true);
  142. });
  143. })();
  144.  
  145. if (GM_getValue("hookFunctionDebugger") === true) {
  146. hookFunctionDebugger();
  147. }
  148. if (GM_getValue("hookCookie") === true) {
  149. hookCookie();
  150. }
  151. if (GM_getValue("hookSetInterval") === true) {
  152. hookSetInterval();
  153. }
  154. if (GM_getValue("hookRandom") === true) {
  155. hookRandom();
  156. }
  157. })();