Code Injector - Bonk.io

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

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

  1. // ==UserScript==
  2. // @name Code Injector - Bonk.io
  3. // @version 1.0.2
  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. // @license GPL-3.0
  8. // @match https://bonk.io/gameframe-release.html
  9. // @run-at document-body
  10. // @grant none
  11. // ==/UserScript==
  12.  
  13. // What this does:
  14. // - Finds other userscripts that have defined their injectors in `window.bonkCodeInjectors`, and runs them
  15.  
  16. // Much of the code below was copied from or inspired by https://github.com/kklkkj/kklee/blob/master/src/runInjectors.js
  17. // Credits to https://github.com/kklkkj for creating this system where multiple
  18. // extensions can edit different parts of Bonk.io' code.
  19.  
  20. // This script was originally made to inject kklkkj's kklee as a userscript
  21.  
  22. // Go thank kklkkj for this
  23.  
  24. (function () {
  25. // Original `document.head.appendChild` function that we're going to overwrite
  26. const _appendChild = document.head.appendChild;
  27. const log = (msg) => console.log(`%c[Injector] ${msg}`, "color: #06c26d");
  28.  
  29. // When RequireJS tries to add alpha2s.js to <head> we will intercept it,
  30. // and patch it using functions defined by other scripts in window.bonkCodeInjectors
  31. document.head.appendChild = function (...args) {
  32. // `args?.[0]?.src.includes("alpha2s.js")` is more difficult to read
  33. if (args[0] && args[0].src.includes("alpha2s.js")) {
  34. // Store the url of the original unmodified alpha2s.js
  35. const code_url = args[0].src;
  36.  
  37. // Remove the src attribute so it doesn't try to load the original alpha2s.js script
  38. args[0].removeAttribute("src");
  39.  
  40. (async function () {
  41. // Fetch alpha2s.js
  42. log("Fetching alpha2s.js...");
  43. const request = await fetch(code_url).catch(error => console.error(error));
  44.  
  45. // Error fetching alpha2s.js (for example http 404)
  46. if (!request.ok) {
  47. log("Failed to fetch alpha2s.js");
  48. alert("An error occurred whilst fetching game code");
  49.  
  50. // Fallback to what bonk.io would normally do
  51. args[0].src = code_url;
  52. return _appendChild.apply(document.head, args);
  53. }
  54.  
  55. let code = await request.text();
  56.  
  57. log("Patching alpha2s.js...");
  58. const start_time = performance.now();
  59.  
  60. // No bonkCodeInjectors found, this might mean that the user does not have any bonk userscripts installed
  61. // or that they failed to load before this script
  62.  
  63. // I removed the alert because people who prefer userscripts over extensions likely
  64. // enjoy the flexibility of being able to disable userscripts easily
  65. // and it's possible that they will have all their userscripts disabled at one time
  66.  
  67. if (!window.bonkCodeInjectors) {
  68. // Still log to the console
  69. log(
  70. "Did not find any Bonk.io userscripts to load. This may be an error, make sure you have scripts installed."
  71. );
  72. } else {
  73. // Loop through `bonkCodeInjectors` and pass alpha2s.js' code in for them to modify
  74. let error_notified = false;
  75. for (const injector of window.bonkCodeInjectors) {
  76. try {
  77. // Run injector from other userscripts
  78. code = injector(code);
  79.  
  80. // I could check if code === injector(code); and throw an error if the code
  81. // did not change, but Salama also does in their userscripts which would caue
  82. // a double up in alert()s, which is not very nice
  83. } catch (error) {
  84. // Only notify the user once if any userscript fails to load
  85. // helpful to prevent spamming alerts()
  86. if (!error_notified) {
  87. // An injector from one of the other userscripts failed to load
  88. alert("One of your Bonk.io userscripts was unable to be loaded");
  89. error_notified = true;
  90. }
  91.  
  92. console.error(error);
  93. }
  94. }
  95. }
  96.  
  97. const end_time = performance.now();
  98. log(`Patched alpha2s.js successfully (${(end_time - start_time).toFixed(0)}ms)`);
  99.  
  100. // Add the new script to the <script>'s contents
  101. args[0].textContent = code;
  102.  
  103. // Make RequireJS think that the script loaded
  104. args[0].dispatchEvent(new Event("load"));
  105.  
  106. // Append the modified <script> tag to document.head
  107. return _appendChild.apply(document.head, args);
  108. })().catch(error => {
  109. // Error patching alpha2s.js somewhere
  110. log("Failed to inject code into alpha2s.js");
  111. console.error(error);
  112. alert("An error occurred whilst patching game code");
  113.  
  114. // Fallback to what bonk.io would normally do without this userscript
  115. args[0].src = code_url;
  116. return _appendChild.apply(document.head, args);
  117. });
  118. } else {
  119. return _appendChild.apply(document.head, args);
  120. }
  121. };
  122. })();

QingJ © 2025

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