WordSleuth

A script that helps you guess words in skribblio

目前為 2023-05-24 提交的版本,檢視 最新版本

// ==UserScript==
// @name         WordSleuth
// @namespace    https://greasyfork.org/en/users/1084087-kueldev
// @version      0.1
// @description  A script that helps you guess words in skribblio
// @author       kueldev
// @match        http*://www.skribbl.io/*
// @match        http*://skribbl.io/*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=skribbl.io
// @grant        none
// @license MIT
// ==/UserScript==

(function() {
  'use strict';

  let wordSet = new Set();

  // Create an element to display the best guesses
  let guessElem = document.createElement('div');
  guessElem.style.position = 'fixed';
  guessElem.style.bottom = '0';
  guessElem.style.left = '0';
  guessElem.style.right = '0';
  guessElem.style.padding = '10px';
  guessElem.style.backgroundColor = 'white';
  guessElem.style.maxHeight = '200px';
  guessElem.style.overflowX = 'auto';
  guessElem.style.whiteSpace = 'nowrap';
  guessElem.style.maxWidth = '100vw';
  document.body.appendChild(guessElem);

  fetch('https://raw.githubusercontent.com/arstgit/high-frequency-vocabulary/master/30k.txt')
    .then(response => response.text())
    .then(data => {
      let words = data.split('\n').map(word => word.trim());
      words.forEach(word => wordSet.add(word));
      observeHints();
    });

  function observeHints() {
    const targetNode = document.querySelector('.hints .container');

    const config = { childList: true, subtree: true };

    const callback = function(mutationsList, observer) {
      for (let mutation of mutationsList) {
        if (mutation.type === 'childList') {
          const wordLengthElem = document.querySelector('.hints .word-length');
          const wordLength = wordLengthElem ? parseInt(wordLengthElem.textContent) : 0;

          const hintElems = Array.from(document.querySelectorAll('.hints .hint'));
          const hints = hintElems.map(elem => elem.textContent === '_' ? '.' : elem.textContent).join('');

          const regex = new RegExp('^' + hints + '$');

          const possibleWords = Array.from(wordSet.values()).filter(word => word.length === wordLength && regex.test(word));

          // Clear previous guesses
          guessElem.innerHTML = '';

          // Add new guesses
          possibleWords.slice(0, 100).forEach((word, index) => {
            let wordElem = document.createElement('div');
            wordElem.textContent = word;
            wordElem.style.fontWeight = 'bold';
            wordElem.style.display = 'inline-block';

            // Set background color based on position
            let colorValue = Math.floor(255 * index / 99);
            wordElem.style.backgroundColor = `rgb(${colorValue}, ${255 - colorValue}, 0)`;
            wordElem.style.padding = '5px';
            wordElem.style.marginRight = '2px';
            wordElem.style.color = 'white';

            // Add hover effect
            wordElem.addEventListener('mouseenter', function() {
              if (!wordElem.classList.contains('pressed')) {
                wordElem.style.backgroundColor = 'lightgray';
              }
              wordElem.classList.add('hovered');
            });

            wordElem.addEventListener('mouseleave', function() {
              if (!wordElem.classList.contains('pressed')) {
                wordElem.style.backgroundColor = `rgb(${colorValue}, ${255 - colorValue}, 0)`;
              }
              wordElem.classList.remove('hovered');
            });

            // Add click functionality
            wordElem.addEventListener('mousedown', function() {
              wordElem.classList.add('pressed');
              wordElem.style.backgroundColor = 'gray';
            });

            wordElem.addEventListener('mouseup', function() {
              wordElem.classList.remove('pressed');
              if (!wordElem.classList.contains('hovered')) {
                wordElem.style.backgroundColor = `rgb(${colorValue}, ${255 - colorValue}, 0)`;
              } else {
                wordElem.style.backgroundColor = 'lightgray';
              }
            });

            wordElem.addEventListener('click', function() {
              const inputElem = document.querySelector('#game-chat input[data-translate="placeholder"]');
              const formElem = document.querySelector('#game-chat form');
              inputElem.value = word;
              formElem.dispatchEvent(new Event('submit', { bubbles: true }));
            });

            guessElem.appendChild(wordElem);
          });
        }
      }
    };

    const observer = new MutationObserver(callback);
    observer.observe(targetNode, config);
  }
})();