Code Injector - Bonk.io

Allows different userscripts to define functions that modify the game's code

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

  1. // ==UserScript==
  2. // @name Code Injector - Bonk.io
  3. // @version 1.0.1
  4. // @description Allows different userscripts to define functions that modify the game's code
  5. // @author Excigma & kklkkj
  6. // @namespace https://gf.qytechs.cn/users/416480
  7. // @match https://bonk.io/gameframe-release.html
  8. // @run-at document-body
  9. // @grant GM_xmlhttpRequest
  10. // @grant unsafeWindow
  11. // ==/UserScript==
  12.  
  13. // What this does:
  14. // - Finds other userscripts that have defined their injectors in `window.bonkCodeInjectors`, and runs them
  15.  
  16. // Credits to https://github.com/kklkkj for creating this system where multiple
  17. // extensions can edit different parts of Bonk.io' code.
  18.  
  19. // Much of the code below was copied from or inspired by https://github.com/kklkkj/kklee/blob/master/src/runInjectors.js
  20.  
  21. // Go thank kklkkj for this
  22.  
  23. (() => {
  24. // Original `document.head.appendChild` function that we're going to overwrite
  25. const _appendChild = document.head.appendChild;
  26.  
  27. // When RequireJS tries to add alpha2s.js to <head> we will intercept it,
  28. // and patch it using functions defined by other scripts in window.bonkCodeInjectors
  29. document.head.appendChild = (...args) => {
  30. // Sure, I can do `args?.[0]?.src.includes("alpha2s.js")`, but it's more difficult to read
  31. if (args[0] && args[0].src.includes("alpha2s.js")) {
  32. console.log("[Injector] Fetching original alpha2s.js...");
  33. // Store the url of the original unmodified alpha2s.js
  34. const alpha2sURL = args[0].src;
  35.  
  36. // Remove the src attribute so it doesn't try to load the original alpha2s.js script
  37. args[0].removeAttribute("src");
  38.  
  39. // Fetch the original unmodified alpha2s.js so we can patch it
  40. // eslint-disable-next-line no-undef
  41. GM_xmlhttpRequest({
  42. method: "GET",
  43. url: alpha2sURL,
  44. onload: (response) => {
  45. console.log("[Injector] Fetched alpha2s.js. Patching...");
  46. // Original alpha2s.js' code
  47. let str = response.responseText;
  48.  
  49. // No bonkCodeInjectors found, this might mean that the user does not have any bonk userscripts installed
  50. // or that they failed to load before this script
  51.  
  52. /* I commented this out because people who prefer userscripts over extensions likely
  53. * enjoy the flexibility of being able to disable userscripts easily, and are thus likely to have
  54. * all their userscripts disabled
  55. */
  56.  
  57. if (!unsafeWindow.bonkCodeInjectors) {
  58. unsafeWindow.bonkCodeInjectors = [];
  59. // alert("Did not find any Bonk.io userscripts to load. This may be an error, make sure you have scripts installed.");
  60. }
  61.  
  62. // Loop through `bonkCodeInjectors` and pass alpha2s.js' code in for them to modify
  63. for (const injector of unsafeWindow.bonkCodeInjectors) {
  64. try {
  65. // Run injector from other userscripts
  66. str = injector(str);
  67. } catch (error) {
  68. // An injector from one of the other userscripts failed to load
  69. alert("One of your Bonk.io userscripts was unable to be loaded");
  70. console.error(error);
  71. }
  72. }
  73.  
  74. // Add the new script to the <script>'s contents
  75. args[0].textContent = str;
  76.  
  77. // Make RequireJS think that the script loaded properly
  78. args[0].dispatchEvent(new Event("load"));
  79.  
  80. console.log("[Injector] Patched alpha2s.js successfully");
  81.  
  82. // Append the modified <script> tag to document.head
  83. return _appendChild.apply(document.head, args);
  84. },
  85. onerror: (error) => {
  86. // Error fetching original alpha2s.js
  87. console.error(error);
  88. alert("An error occurred whilst loading bonk injectors.");
  89. return _appendChild.apply(document.head, args);
  90. }
  91. });
  92. } else {
  93. return _appendChild.apply(document.head, args);
  94. }
  95. };
  96. })();

QingJ © 2025

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