Word Helper for Word guessing game

This script suggests possible words based from the pattern of the hidden word.

目前为 2022-07-02 提交的版本。查看 最新版本

// ==UserScript==
// @name         Word Helper for Word guessing game
// @namespace    http://tampermonkey.net/
// @version      0.5
// @description  This script suggests possible words based from the pattern of the hidden word.
// @author       Vholran
// @match        https://*.drawaria.online/*
// @require      https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js
// @icon         https://www.google.com/s2/favicons?domain=drawaria.online
// @grant        none
// ==/UserScript==

(($, undefined) => {
    $(() => {
        const wordAlts = new RegExp('\слов|Palabras|Word');
        const joinRoomObserver = new MutationObserver((mutations) => {
            if(wordAlts.test(mutations[0].target.textContent)){
                guessing_word();
                if($('.systemchatmessage8').length>=1){
                    $('#chatbox_messages').append('<div class="chatmessage systemchatmessage3">This room contains custom words, all words may not be on the word list</div>');
                }
            }
            joinRoomObserver.disconnect();
        });
        joinRoomObserver.observe($('#infotext')[0], {childList: true });

        const guessing_word = () => {
            'use strict';
            const sendButton = $('#chatattop-sendbutton'),
                  inputChat = $('#chatbox_textinput'),
                  targetWord = $('#targetword_tip')[0],
                  rightBar = $('#rightbar'),
                  lang = $('#langselector');

            $('<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()[3]);
            $('#hintsPanel').append('<span id="hintsBox">');

            let hintsPanel = $('#hintsPanel')[0],
                hintsBox = $('#hintsBox'),
                wordList = [];

            $("body").on('click','.hintClick',() => {
                inputChat.val(event.target.innerHTML);
                sendButton.click();
            });

            const assist = (event, wordChanged = false) => {
                hintsBox.empty();

                if(wordList.length==0){
                    hintsPanel.style.display = 'none';
                    if($('.delayed').length<1){
                        $('#chatbox_messages').append('<div class="chatmessage systemchatmessage3 delayed">Word list got delayed right now!</div>');
                    }
                    initialize();
                    return;
                }

                let wordRegex = targetWord.textContent.replace(/_/g, '[^ \\-"]');
                wordRegex = '"'.concat(wordRegex, '"');
                wordRegex = new RegExp(wordRegex, 'g');

                let hints= wordList.match(wordRegex);
                if(!hints){
                    hintsBox.append('<span style="color:red; font-weight:bold">Sorry, no words available!</span>');
                    return;
                }else{
                    hintsBox.append('<span style="color:green; font-weight:bold">Click any word to send it: </span><br>');
                }
                hints = hints.map((hint) => {
                    return hint.substring(1, hint.length - 1);
                });

                let newHints = [];
                hints.forEach((hint) => {
                    let hintWord = $('<a style="color:#007CFF; background:#C6FE71; user-select:none" href="javascript:void(0);" class="hintClick">'+ hint +'</a>')
                    if(!(inputChat.val() && hint.toLowerCase().search(inputChat.val().toLowerCase()) != -1)){
                        newHints.push(hint);
                    }else{
                        hintsBox.append(hintWord);
                        hintsBox.append(document.createTextNode(', '));
                    }
                });
                newHints.forEach((hint) => {
                    let hintWord = $('<a style="background:none; user-select:none" href="javascript:void(0);" class="hintClick">'+ hint +'</a>')
                    hintsBox.append(hintWord);
                    hintsBox.append(document.createTextNode(', '));
                });

                hintsBox.contents()[hintsBox.contents().length-1].remove();
            };

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

            inputChat.on('input',() => {
                if(targetWord.style.display == ''){
                    assist();
                }
            });

            const initialize = async () => {
                try{
                    wordList = await fetch('https://api.npoint.io/2efd2c3ecb68ca16ecef/' + lang.val()).then((response) => response.json());
                }catch(e){
                    await new Promise((resolve) => setTimeout(resolve, 500));
                    return initialize();
                }
                wordList = JSON.stringify(wordList);
                wordList = wordList.substring(1, wordList.length - 1);
            };
            initialize();
        }
        });
})(window.jQuery.noConflict(true));