Contexto Hack

This is a Contexto Hack that Gives you the word of the day for everyday.

目前為 2023-06-12 提交的版本,檢視 最新版本

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         Contexto Hack
// @namespace    your-namespace-here
// @version      1.0
// @author       longkidkoolstar
// @description  This is a Contexto Hack that Gives you the word of the day for everyday.
// @icon         https://th.bing.com/th/id/R.7b926e0e3ff4e38bba110ae354e71ef8?rik=ssJbNW18rrXCuw&pid=ImgRaw&r=0
// @match        https://contexto.me/*
// @license      CC BY-NC-ND 4.0
// @grant        GM_setValue
// @grant        GM_getValue
// ==/UserScript==

(function() {
    'use strict';

    // Function to check if a string contains the number 1 by itself
    function containsOne(str) {
        return /^\D*1\D*$/.test(str);
    }

    // Retrieve saved words and game numbers from Tampermonkey storage
    let savedWords = GM_getValue('savedWords', {});
    let gameNumbers = GM_getValue('gameNumbers', {});

    // Function to search for words and game numbers to save on the page
    let currentGameNumber = '';

    function searchForWordsAndGameNumbers() {
        // Find the game number element on the page
        const gameNumberElement = document.querySelector('.info-bar span:nth-child(2)');
        currentGameNumber = gameNumberElement ? gameNumberElement.textContent.trim().replace('#', '') : '';

        // Find all the div elements with class "row" on the page
        const rows = document.querySelectorAll('.row');
        for (let i = 0; i < rows.length; i++) {
            const row = rows[i];
            // Find all the span elements within the row
            const spans = row.querySelectorAll('span');
            let hasOne = false;
            let word = '';
            for (let j = 0; j < spans.length; j++) {
                const span = spans[j];
                if (containsOne(span.innerHTML)) {
                    hasOne = true;
                } else {
                    word = span.innerHTML;
                }
            }
            // Save the word and game number to the objects if the word has the number 1 by itself and it's not already saved
            if (hasOne && word && !savedWords[word]) {
                savedWords[word] = true;
                gameNumbers[word] = currentGameNumber; // Save the current game number instead of searching for it again
                // Log the game number for the saved word
                console.log(`Game number for ${word}: ${currentGameNumber}`);
            }
        }

        // Save the updated objects to Tampermonkey storage
        GM_setValue('savedWords', savedWords);
        GM_setValue('gameNumbers', gameNumbers);

        // Update the GUI with the saved words and game numbers
        updateSavedWordsGUI();
    }

    // Function to reveal the word for the current game number
    function revealWordForCurrentGameNumber() {
        // Find the saved word for the current game number
        const savedWordsForCurrentGameNumber = Object.keys(savedWords).filter((word) => {
            return gameNumbers[word] === currentGameNumber;
        });

        // Display the saved word in an alert box
        if (savedWordsForCurrentGameNumber.length > 0) {
            alert(`The word for game number ${currentGameNumber} is: ${savedWordsForCurrentGameNumber[0]}`);
        } else {
            alert(`No saved words for game number ${currentGameNumber}`);
        }
    }

    // Create a button to reveal the word for the current game number
    const revealButton = document.createElement('button');
    revealButton.textContent = 'Reveal Word';
    revealButton.addEventListener('click', revealWordForCurrentGameNumber);
    document.body.appendChild(revealButton);

    // Create a div element to hold the saved words GUI
    const savedWordsGUI = document.createElement('div');
    savedWordsGUI.id = 'saved-words-list';
    document.body.appendChild(savedWordsGUI);

    // Create a button to show the saved words GUI
    const showSavedWordsButton = document.createElement('button');
    showSavedWordsButton.textContent = 'Saved Words';
    showSavedWordsButton.addEventListener('click', () => {
        savedWordsGUI.classList.add('open');
    });
    document.body.appendChild(showSavedWordsButton);

    // Create a button to minimize the saved words GUI
    const minimizeSavedWordsButton = document.createElement('button');
    minimizeSavedWordsButton.innerHTML = '<img src="https://th.bing.com/th/id/R.6a6eda3ee63c80ebc02dc830b395324e?rik=t2E%2fYYP3IGbSsQ&pid=ImgRaw&r=0" alt="Close">';
    minimizeSavedWordsButton.addEventListener('click', () => {
        savedWordsGUI.classList.remove('open');
    });
    savedWordsGUI.appendChild(minimizeSavedWordsButton);


    // Create a list element to display the saved words
    const savedWordsList = document.createElement('ul');
    savedWordsGUI.appendChild(savedWordsList);

    // Function to update the saved words GUI with the saved words and game numbers
    function updateSavedWordsGUI() {
        // Clear the current saved words list
        savedWordsList.innerHTML = '';

        // Get all saved words sorted by game number
        const savedWordsSorted = Object.keys(gameNumbers).sort((a, b) => {
            return gameNumbers[a] - gameNumbers[b];
        });

        // Add each saved word to the list
        for (let i = 0; i < savedWordsSorted.length; i++) {
            const word = savedWordsSorted[i];
            const gameNumber = gameNumbers[word];
            const listItem = document.createElement('li');
            listItem.textContent = `${word} (Game ${gameNumber})`;
            savedWordsList.appendChild(listItem);
        }
    }

    // Update the saved words GUI with the saved words and game numbers
    updateSavedWordsGUI();

    // Function to clear the saved words and game numbers from Tampermonkey storage
    function clearSavedWords() {
        savedWords = {};
        gameNumbers = {};
        GM_setValue('savedWords', savedWords);
        GM_setValue('gameNumbers', gameNumbers);
        updateSavedWordsGUI();
        alert('Saved words cleared');
    }

    // Create a button to clear the saved words and game numbers
    const clearSavedWordsButton = document.createElement('button');
    clearSavedWordsButton.textContent = 'Clear Saved Words';
    clearSavedWordsButton.addEventListener('click', clearSavedWords);
    savedWordsGUI.appendChild(clearSavedWordsButton);

    // Function to export the saved words and game numbers as JSON
    function exportSavedWords() {
        const savedWordsData = {
            savedWords: savedWords,
            gameNumbers: gameNumbers
        };
        const dataStr = 'data:text/json;charset=utf-8,' + encodeURIComponent(JSON.stringify(savedWordsData));
        const downloadAnchorNode = document.createElement('a');
        downloadAnchorNode.setAttribute('href', dataStr);
        downloadAnchorNode.setAttribute('download', 'contexto_saved_words.json');
        document.body.appendChild(downloadAnchorNode); // required for firefox
        downloadAnchorNode.click();
        downloadAnchorNode.remove();
    }

    // Create a button to export the saved words and game numbers
    const exportSavedWordsButton = document.createElement('button');
    exportSavedWordsButton.textContent = 'Export Saved Words';
    exportSavedWordsButton.addEventListener('click', exportSavedWords);
    savedWordsGUI.appendChild(exportSavedWordsButton);

    // Function to import saved words and game numbers from JSON
    function importSavedWords() {
        const fileInput = document.createElement('input');
        fileInput.type = 'file';
        fileInput.accept = '.json';
        fileInput.addEventListener('change', () => {
            const file = fileInput.files[0];
            const reader = new FileReader();
            reader.onload = (e) => {
                try {
                    const savedWordsData = JSON.parse(e.target.result);
                    savedWords = savedWordsData.savedWords;
                    gameNumbers = savedWordsData.gameNumbers;
                    GM_setValue('savedWords', savedWords);
                    GM_setValue('gameNumbers', gameNumbers);
                    updateSavedWordsGUI();
                    alert('Saved words imported');
                } catch (err) {
                    alert('Error importing saved words');
                }
            };
            reader.readAsText(file);
        });
        fileInput.click();
    }

    // Create a button to import saved words and game numbers
    const importSavedWordsButton = document.createElement('button');
    importSavedWordsButton.textContent = 'Import Saved Words';
    importSavedWordsButton.addEventListener('click', importSavedWords);
    savedWordsGUI.appendChild(importSavedWordsButton);

    // Define CSS styles for the saved words GUI
    const css = `
        #saved-words-list {
            position: fixed;
            bottom: 0;
            right: 0;
            background-color: white;
            border: 2px solid black;
            border-radius: 5px 0 0 0;
            padding: 10px;
            max-height: 300px;
            overflow-y: auto;
            display: none;
        }
        #saved-words-list.open {
            display: block;
        }
    #saved-words-list button {
        margin: 5px;
        padding: 0;
        background: none;
        border: none;
        cursor: pointer;
    }
    #saved-words-list img {
        width: 20px;
        height: 20px;
    }
`;

    // Add the CSS styles to the page
    const style = document.createElement('style');
    style.appendChild(document.createTextNode(css));
    document.head.appendChild(style);

    // Call the searchForWordsAndGameNumbers function every 5 seconds to continuously search for new words and game numbers on the page
    setInterval(searchForWordsAndGameNumbers, 5000);
})();