Library script that adds helpful methods for dealing with Turbo Events to WaniKani Open Framework that other userscripts can use to simplify their workflow.
All additions are added to a new turbo
property of the wkof
object, therefore accessible via wkof.turbo
.
Example usage:
// Make sure the events are fully loaded before starting your configuration.
wkof.ready('TurboEvents').then(configurePageHandler);
// The callback function can accept an event argument (if desired), which will be provided in all turbo events
// I.e., it will not be provided in the 'load' event (not to be confused with the 'turbo:load' event), if used.
function callbackFunction(event) { console.log(`callbackFunction() has run for event "${event?.type}"`); }
function configurePageHandler() {
// Run the callback on turbo:click on any page.
const onClick = wkof.turbo.on.click(callbackFunction);
// The returned object contains a reference to the listener, using the event name as the property name.
const onClickListener = onClick[wkof.turbo.events.click]; // alternative: onclick['turbo:click']
// Run the callback on turbo:before-render only on reviews pages
const onBeforeRender = wkof.turbo.on.before_render(callbackFunction, [/https:\/\/www\.wanikani\.com\/subjects\/review.*\/?$/]);
// Run the callback on initial page load and turbo:before-render only on the dashboard (note that two new listeners are added)
const onLoadAndBeforeRender = wkof.turbo.on.events(['load', wkof.turbo.events.before_render], callbackFunction, [/https:\/\/www\.wanikani\.com(\/dashboard.*)?\/?$/]);
// Remove the listener(s) if no longer desired (this convenience function removes both listeners)
onLoadAndBeforeRender.remove();
// Can alternatively remove listeners with the base turbo object
wkof.turbo.remove_event_listener(wkof.turbo.events.before_render, onBeforeRender[wkof.turbo.events.before_render.name]);
// Can also use the more generic method to add/remove listeners with more fine-tuned control
const visitListener = { callback: callbackFunction, urls: [] };
const visitHandler = wkof.turbo.add_event_listener(wkof.turbo.events.visit, visitListener)
wkof.turbo.remove_event_listener(wkof.turbo.events.visit, visitListener);
// onClick listener still active
}