Code Injector - Bonk.io

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

Version vom 26.10.2021. Aktuellste Version

Du musst eine Erweiterung wie Tampermonkey, Greasemonkey oder Violentmonkey installieren, um dieses Skript zu installieren.

You will need to install an extension such as Tampermonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey or Userscripts to install this script.

You will need to install an extension such as Tampermonkey to install this script.

Sie müssten eine Skript Manager Erweiterung installieren damit sie dieses Skript installieren können

(Ich habe schon ein Skript Manager, Lass mich es installieren!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(I already have a user style manager, let me install it!)

// ==UserScript==
// @name         Code Injector - Bonk.io
// @version      1.0.1
// @description  Allows different userscripts to define functions that modify the game's code
// @author       Excigma & kklkkj
// @namespace    https://greatest.deepsurf.us/users/416480 
// @match        https://bonk.io/gameframe-release.html
// @run-at       document-body
// @grant        GM_xmlhttpRequest
// @grant        unsafeWindow
// ==/UserScript==

// What this does:
// - Finds other userscripts that have defined their injectors in `window.bonkCodeInjectors`, and runs them

// Credits to https://github.com/kklkkj for creating this system where multiple
// extensions can edit different parts of Bonk.io' code.

// Much of the code below was copied from or inspired by https://github.com/kklkkj/kklee/blob/master/src/runInjectors.js

// Go thank kklkkj for this

(() => {
	// Original `document.head.appendChild` function that we're going to overwrite
	const _appendChild = document.head.appendChild;

	// When RequireJS tries to add alpha2s.js to <head> we will intercept it,
	// and patch it using functions defined by other scripts in window.bonkCodeInjectors
	document.head.appendChild = (...args) => {
		// Sure, I can do `args?.[0]?.src.includes("alpha2s.js")`, but it's more difficult to read
		if (args[0] && args[0].src.includes("alpha2s.js")) {
			console.log("[Injector] Fetching original alpha2s.js...");
			// Store the url of the original unmodified alpha2s.js
			const alpha2sURL = args[0].src;

			// Remove the src attribute so it doesn't try to load the original alpha2s.js script
			args[0].removeAttribute("src");

			// Fetch the original unmodified alpha2s.js so we can patch it
			// eslint-disable-next-line no-undef
			GM_xmlhttpRequest({
				method: "GET",
				url: alpha2sURL,
				onload: (response) => {
					console.log("[Injector] Fetched alpha2s.js. Patching...");
					// Original alpha2s.js' code
					let str = response.responseText;

					// No bonkCodeInjectors found, this might mean that the user does not have any bonk userscripts installed
					// or that they failed to load before this script

					/* I commented this out because people who prefer userscripts over extensions likely
					 * enjoy the flexibility of being able to disable userscripts easily, and are thus likely to have
					 * all their userscripts disabled
					*/

					if (!unsafeWindow.bonkCodeInjectors) {
					    unsafeWindow.bonkCodeInjectors = [];
					// 	alert("Did not find any Bonk.io userscripts to load. This may be an error, make sure you have scripts installed.");
					}

					// Loop through `bonkCodeInjectors` and pass alpha2s.js' code in for them to modify
					for (const injector of unsafeWindow.bonkCodeInjectors) {
						try {
							// Run injector from other userscripts
							str = injector(str);
						} catch (error) {
							// An injector from one of the other userscripts failed to load
							alert("One of your Bonk.io userscripts was unable to be loaded");
							console.error(error);
						}
					}

					// Add the new script to the <script>'s contents
					args[0].textContent = str;

					// Make RequireJS think that the script loaded properly
					args[0].dispatchEvent(new Event("load"));

					console.log("[Injector] Patched alpha2s.js successfully");

					// Append the modified <script> tag to document.head
					return _appendChild.apply(document.head, args);
				},
				onerror: (error) => {
					// Error fetching original alpha2s.js
					console.error(error);
					alert("An error occurred whilst loading bonk injectors.");
					return _appendChild.apply(document.head, args);
				}
			});
		} else {
			return _appendChild.apply(document.head, args);
		}
	};
})();