|
10 months ago | |
---|---|---|
README.md | 10 months ago | |
badge.png | 11 months ago | |
blaseball-userscript-loader.user.js | 10 months ago |
README.md
Blaseball Userscript Loader
Because Blaseball is a single-page application, there's no real good way to write userscripts for it. Simply defining functions and event handlers once, the way you usually would, doesn't work because elements in the DOM are loaded and unloaded all the time. The usual workaround for this is to use a MutationObserver
, which runs a callback function when the DOM is changed. The script can then detect whether the current state of the DOM represents one that it cares about and run its code then.
This is gross, and no one likes doing it. Additionally, it means that if you have a number of userscripts installed, they're all watching the same changes in the DOM, which is quite wasteful.
This script also watches the DOM for changes. However, it has no function on its own. Instead, it allows other scripts to register callbacks. That way, only one script needs to be watching for DOM mutations, and the other userscripts just need to define a function to be called when a mutation they care about occurs.
Requires Tampermonkey (chrome, firefox)
For developers:
Your script will run on page load as normal, and you should use this time to do all of your setup (anything that would be done outside of the MutationObserver
). However, instead of creating a MutationObserver
, call the function document._BLASEBALL_USERSCRIPT_REGISTER(name, callback, condition = (mutationsList) => (true))
.
name
: The name of your script. Must be unique. I recommend using your namespace
if you support Blaseball Userscript Options.
callback
: This is your callback function, what runs when your script is called. It takes a single argument, the mutationsList
of the MutationObserver
.
condition
: An optional argument. If provided, it is a function that is run before your callback is. The callback is only run if the condition function returns true
. It takes a single argument, the mutationsList
of the MutationObserver
. If omitted, your callback function will run every time the DOM is mutated.
For an example of how to use the loader if available, and a MutationObserver
otherwise, see Blaseball Default Max Bet.
You are encouraged to include this badge if you support the loader :)
[](https://git.hollymcfarland.com/monorail/blaseball-userscript-loader)