Word Helper for Word guessing game

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

目前為 2022-07-02 提交的版本,檢視 最新版本

您需要先安裝使用者腳本管理器擴展,如 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      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));