CallbackEditor

Assist programmers in hooking code into callbacks.

此脚本不应直接安装。它是供其他脚本使用的外部库,要使用该库请加入元指令 // @require https://update.gf.qytechs.cn/scripts/429237/949518/CallbackEditor.js

  1. // ==UserScript==
  2. // @name CallbackEditor
  3. // @namespace http://tampermonkey.net/
  4. // @version 1.0
  5. // @description Assist programmers in hooking code into callbacks.
  6. // @author MTP3
  7. // @match http://*/*
  8. // @icon https://www.google.com/s2/favicons?domain=manyland.com
  9. // @grant none
  10. // ==/UserScript==
  11.  
  12. /*
  13. CallbackEditor by MTP3
  14. edit callbacks without necessarily having to deal with terrible string manipulation directly™
  15.  
  16. CallbackEditor.prototype
  17. disassemble(Function func): Object funcobj
  18. Converts a function into a form that CallbackEditor can manipulate
  19. reassemble(Object funcobj): Function func
  20. Converts a funcobj generated by this.disassemble back to a new Function.
  21.  
  22. toFuncString(Object funcobj): String string
  23. Returns a string that defines an anonymous arrow function with the funcobj's arguments and code.
  24. funcToString(Function func): String string
  25. Returns a string that defines an anonymous arrow function with the function's arguments and code.
  26. Disassembles func and runs this.toFuncString with the resulting funcobj.
  27. getCallString(String str, String args): String string
  28. getCallString(String str, Array args): String string
  29. Appends `args` to str, surrounded with parentheses. This creates a function call when converted to code.
  30. `args` is treated like in 'new Function(args, code)'.
  31.  
  32. ! Actual functions that I expect people to call.
  33.  
  34. prepend(Function func, String string): Function func
  35. Prepends an arbitrary string to the beginning of the function.
  36. Automatically appends a semicolon during concatenation.
  37. append(Function func, String string): Function func
  38. Appends an arbitrary string to the end of the function.
  39. Automatically prepends a semicolon during concatenation.
  40.  
  41. callBefore(Function func, String funcref, String args): Function func
  42. callBefore(Function func, String funcref, Array args): Function func
  43. Prepends a function call to the beginning of `func`. Does not overwrite the original reference.
  44. `funcref` is expected to be a path to the function accessible from the calling function.
  45. `args` is treated like in 'new Function(args, code)'.
  46. callAfter(Function func, String funcref, String args): Function func
  47. callAfter(Function func, String funcref, Array args): Function func
  48. Appends a function call to the end of `func`. Does not overwrite the original reference.
  49. `funcref` is expected to be a path to the Function variable accessible from the calling function.
  50. `args` is treated like in 'new Function(args, code)'.
  51.  
  52. runBefore(Function func, Function func2, String args): Function func
  53. runBefore(Function func, Function func2, Array args): Function func
  54. Prepends a copy of `func2` to the beginning of `func`. Does not overwrite the original reference.
  55. `func2` inherits `func`'s scope.
  56. `args` is treated like in 'new Function(args, code)'.
  57. runAfter(Function func, Function func2, String args): Function func
  58. runAfter(Function func, Function func2, Array args): Function func
  59. Appends a copy of `func2` to the end of `func`. Does not overwrite the original reference.
  60. `func2` inherits `func`'s scope.
  61. `args` is treated like in 'new Function(args, code)'.
  62. */
  63.  
  64. if (typeof window.CallbackEditor === "undefined") {
  65. window.CallbackEditor = class {
  66. constructor() {
  67. let _console = consoleref;
  68.  
  69. if (typeof _console === "undefined")
  70. _console = console;
  71.  
  72. _console.log("%c loaded CallbackEditor by MTP3 ", "background: #aa7700; color: #ffff00");
  73. }
  74.  
  75. disassemble(func) { /* Split up a function into strings for manipulation. */
  76. let code = func.toString();
  77.  
  78. return {
  79. argumentStr: code.slice(code.indexOf("(") + 1, code.indexOf(")")),
  80. code: code.slice(code.indexOf("{") + 1, -1)
  81. }
  82. };
  83.  
  84. reassemble(funcobj) { /* Generate a new function from a disassembled function. */
  85. return new Function(funcobj.argumentStr, funcobj.code);
  86. };
  87.  
  88. toFuncString(funcobj) { /* Generate a stringified function from a disassembled function. */
  89. return "(" + funcobj.argumentStr + ")=>{" + funcobj.code + "}";
  90. };
  91.  
  92. funcToString(func) { /* Stringify a function. */
  93. return this.toFuncString(this.disassemble(func));
  94. };
  95.  
  96. getCallString(str, args) { /* Append function call syntax to a string, using an argument list. */
  97. let argstr;
  98.  
  99. if (typeof args === "undefined")
  100. argstr = "";
  101. else
  102. argstr = args.toString();
  103.  
  104. return str + `(${argstr})`;
  105. };
  106.  
  107. prepend(func, string) { /* Prepend arbitrary text to the beginning of a function. */
  108. let funcobj = this.disassemble(func);
  109. funcobj.code = string + ";" + funcobj.code;
  110. return this.reassemble(funcobj);
  111. };
  112.  
  113. append(func, string) { /* Append arbitrary text to the end of a function. */
  114. let funcobj = this.disassemble(func);
  115. funcobj.code += ";" + string;
  116. return this.reassemble(funcobj);
  117. };
  118.  
  119. callBefore(func, funcref, args) { /* Prepend arbitrary function call to the beginning of a function. */
  120. return this.prepend(func, this.getCallString(funcref, args));
  121. };
  122.  
  123. callAfter(func, funcref, args) { /* Append arbitrary function call to the end of a function. */
  124. return this.append(func, this.getCallString(funcref, args));
  125. };
  126.  
  127. runBefore(func, func2, args) { /* Prepend arbitrary code to the beginning of a function. */
  128. return this.prepend(func, this.getCallString("(" + this.funcToString(func2) + ")", args));
  129. };
  130.  
  131. runAfter(func, func2, args) { /* Append arbitrary code to the end of a function. */
  132. return this.append(func, this.getCallString("(" + this.funcToString(func2) + ")", args));
  133. };
  134. };
  135.  
  136. window.callbackEditor = new CallbackEditor;
  137. }

QingJ © 2025

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