Word Guessing Hints (Español words only)

Word Guessing hints or cheat

目前為 2022-05-28 提交的版本,檢視 最新版本

// ==UserScript==
// @name         Word Guessing Hints (Español words only)
// @namespace    http://tampermonkey.net/
// @version      0.2
// @description  Word Guessing hints or cheat
// @author       Vholran
// @match        https://*.drawaria.online/*
// @require      https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js
// @require      https://cdnjs.cloudflare.com/ajax/libs/jquery-cookie/1.4.1/jquery.cookie.min.js
// @icon         https://www.google.com/s2/favicons?domain=drawaria.online
// @grant        none
// ==/UserScript==


(function ($, undefined) {
    $(function () {

        let rightBar = $("#rightbar");
        let wordDif = new RegExp('\Palabras');

        let joinRoomObserver = new MutationObserver(function(mutations) {
            if(wordDif.test(mutations[0].target.textContent)){
                guessing_word();
            }
            joinRoomObserver.disconnect();
        });
        joinRoomObserver.observe($('#infotext')[0], {childList: true });

        const guessing_word = async () => {
            'use strict';

            const sendButton = $('#chatattop-sendbutton'),
                  inputChat = $('#chatbox_textinput'),
                  targetWord = $('#targetword_tip')[0],
                  rightBar = $('#rightbar');

            $('<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 wordList=[];
            let lastWords=[];

            let hintClick = (event) => {
                inputChat.val(event.target.innerHTML);
                sendButton.click();
            };

            let assist = (event, wordChanged = false) => {
                $('#hintsBox').empty();
                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 hints available!</span>');
                    return;
                } else {
                    $('#hintsBox').append('<span style="color:green; font-weight:bold">Click on a hint to submit it: </span><br>');
                }

                hints = hints.map((hint) => {
                    return hint.substring(1, hint.length - 1);
                });

                lastWords=hints;

                let newHints=[];

                hints.forEach((hint) => {
                    const hintSpan = document.createElement('a');
                    hintSpan.innerHTML = hint;
                    hintSpan.style.color = '#007CFF';
                    hintSpan.style.background = "#FFF78F";
                    hintSpan.style.userSelect = 'none';
                    hintSpan.href = 'javascript:void(0);';
                    hintSpan.onclick = hintClick;

                    if (
                        !(inputChat.val() &&
                          hint.toLowerCase().search(inputChat.val().toLowerCase()) !== -1
                         )) {
                        hintSpan.style.display= 'none';
                        newHints.push(hint);
                    }else{
                        $('#hintsBox').append(hintSpan)
                        $('#hintsBox').append(document.createTextNode(', '));
                    }

                });
                newHints.forEach((hint) => {
                    let hintSpan = document.createElement('a');
                    hintSpan.innerHTML = hint;
                    hintSpan.style.backgroundColor = 'none';
                    hintSpan.href = 'javascript:void(0);';
                    hintSpan.style.userSelect = 'none';
                    hintSpan.onclick = hintClick;
                    $('#hintsBox').append(hintSpan);
                    $('#hintsBox').append(document.createTextNode(', '));
                });
                $("#hintsBox").contents()[$("#hintsBox").contents().length-1].remove();

            };

            let wordTipObserver = new MutationObserver(function(mutations) {
                if(targetWord.style.display==''){
                    $('#hintsPanel')[0].style.display='';
                    assist();
                    return;
                }
                $('#hintsPanel')[0].style.display='none';
            });
            wordTipObserver.observe(targetWord, {attributes: true, attributeFilter:['style']});

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

            const initialize = async () => {
                let lang = localStorage.getItem('gamelang');
                try {
                    wordList = await fetch(
                        'https://api.npoint.io/736deb566436d7b1462f/' + lang
                    ).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));