Wanikani Open Framework Turbo Events

Adds helpful methods for dealing with Turbo Events to WaniKani Open Framework

目前为 2024-07-28 提交的版本。查看 最新版本

作者
Inserio
评分
0 0 0
版本
1.1.1
创建于
2024-07-28
更新于
2024-07-28
大小
10.9 KB
许可证
MIT
适用于

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
}

QingJ © 2025

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