Word Helper for Word guessing game

A script that showing a list of possible words for the current target word and allowing the user to easily input them into the chat.

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

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

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         Word Helper for Word guessing game
// @namespace    http://tampermonkey.net/
// @version      1.0
// @description  A script that showing a list of possible words for the current target word and allowing the user to easily input them into the chat.
// @match        https://*.drawaria.online/*
// @require      https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js
// @author       Vholran
// @icon         https://www.google.com/s2/favicons?domain=drawaria.online
// @grant        none
// ==/UserScript==

(($, undefined) => {
    $(() => {
        let wordCheatPanel = () => {
            const $sendButton = $('#chatattop-sendbutton');
            const $inputChat = $('#chatbox_textinput');
            const $targetWord = $('#targetword_tip');
            const $rightBar = $('#rightbar');
            const $hintsBox = $('<span id="hintsBox">');
            let lang = $('#langselector').val();
            let wordList;

            $('<div id="hintsPanel" style="display:none; background: #eeeeee; overflow-wrap: anywhere; border: 4px solid #eeeeee; border-radius: 2px;  overflow-y: scroll; height: 100%; width:100%; margin: 8px 0 0 0; color: #3675ce;">')
                .insertAfter($rightBar.children().eq(3))
                .append($hintsBox);

            $("body").on('click', '.hintClick', event => {
                $inputChat.val(event.target.innerHTML);
                $sendButton.click();
            });
            const assist = () => {
                if (!wordList) {
                    return;
                }

                $hintsBox.empty();
                let wordRegex = $targetWord.text().replace(/_/g, '[^ \\-"]');
                wordRegex = `"${wordRegex}"`;
                wordRegex = new RegExp(wordRegex, 'g');
                let hints = wordList.match(wordRegex);

                if (!hints) {
                    $hintsBox.append('<span style="color:red; font-weight:bold">Sorry, no word found!</span>');
                } else {
                    $hintsBox.append('<span style="color:green; font-weight:bold">Click any word to send it: </span><br>');
                    hints = hints.map(hint => hint.slice(1, -1));
                    let newHints = hints.filter(hint => !$inputChat.val() || hint.toLowerCase().search($inputChat.val().toLowerCase()) === -1);
                    hints = hints.filter(hint => !newHints.includes(hint));
                    let html = [...hints.map((hint, i) => `<a style="color:#007CFF; background:#C6FE71; user-select:none" href="javascript:void(0);" class="hintClick">${hint}</a>`),
                                ...newHints.map((hint, i) => `<a style="background:none; user-select:none" href="javascript:void(0);" class="hintClick">${hint}</a>`)].join(', ');
                    $hintsBox.append(html);
                }
            };

            $inputChat.on('input', assist);
            const targetWord = $targetWord[0];
            const hintsPanel = $('#hintsPanel');

            const initialize = async () => {
                try{
                    wordList = await fetch(`https://api.npoint.io/0fc9dd19c4867f584b74/${lang}`).then(response => response.text());
                }catch(e){
                    await new Promise((resolve) => setTimeout(resolve, 300));
                    return initialize();
                }
                wordList = wordList.slice(1, -1);
            };
            initialize();

            const wordTipObserver = new MutationObserver((mutations) => {
                mutations.forEach((mutation) => {
                    if (targetWord.style.display === 'none') {
                        hintsPanel.hide();
                    } else {
                        hintsPanel.show();
                        assist();
                    }
                });
            });
            wordTipObserver.observe(targetWord, { attributes: true, attributeFilter: ['style'] });

            const refreshWordObserver = new MutationObserver(function(mutations) {
                if (mutations[0].target.disabled) {
                    $('#wordchooser-refreshlist').prop("disabled",false);
                }
            });
            refreshWordObserver.observe($('#wordchooser-refreshlist')[0], {attributes: true});
        };

        const roomKeywords = /\слов|Palabras|Word/;
        $("#infotext").on("DOMSubtreeModified", e => {
            if (roomKeywords.test(e.target.textContent)) {
                wordCheatPanel();
                $(e.target).off("DOMSubtreeModified");
            }
        });
    });
})(window.jQuery.noConflict(true));