User:PetraMagna/momotalk-choice.js

From Blue Archive Wiki
Revision as of 03:25, 8 December 2023 by PetraMagna (talk | contribs) (switch to class-based implementation)
Jump to navigation Jump to search

Note: After publishing, you may have to bypass your browser's cache to see the changes.

  • Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
  • Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
  • Internet Explorer / Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5
  • Opera: Press Ctrl-F5.
const momotalk_options = function() {

    function defer(method) {
        if (window.jQuery) {
            method();
        } else {
            setTimeout(function() { defer(method) }, 50);
        }
    }

    function momotalk_option_click(group_num, option_num) {
        const container = $("#momotalk-sensei-option-group-" + group_num);
        container.children('.momotalk-reply-option').each(function(num, option) {
			const index = num + 1;
            // addClass and removeClass become nop if the corresponding jquery objects don't exist
            if (index === option_num) {
                $(option).addClass("momotalk-selected-option");
            } else {
                $(option).removeClass("momotalk-selected-option");
            }
            $(".momotalk-student-option-" + group_num + "-" + index).each(function(i, student_reply) {
            	if (index === option_num) {
            		$(student_reply).removeClass("momotalk-hidden-student-text");
            	} else {
            		$(student_reply).addClass("momotalk-hidden-student-text");
            	}
            });
        });
        
    }
    
    function momotalk_init() {
        $(".momotalk-reply-container").each(function(i, container) {
            const container_id = container.id;
            if (container_id == null) {
            	return;
            }
            // extract group number from the id
            const container_id_split = container_id.split("-");
            const group_num = container_id_split[container_id_split.length - 1];
            // register onclick function for each option
            $(container).children('.momotalk-reply-option').each(function(option_num, option) {
                option.onclick = function() {
                    momotalk_option_click(group_num, option_num + 1);
                };
                // by default, the first option is selected
                if (option_num === 0) {
                	option.click();
                }
            });
        });
    }
    
    function momotalk_main() {
        $(document).ready(function() {
            const title_split = mw.config.get('wgTitle').toLowerCase().split("/");
            // don't do anything if this is not a MomoTalk page
            if (mw.config.get('wgNamespaceNumber') === 0 && title_split.length === 2 && title_split[1] === 'momotalk') {
                momotalk_init();
            } else {
                console.log("Not MomoTalk page. Exit.");
            }
        });
    }
    
    defer(momotalk_main);
};

momotalk_options();