User:Polygnotus/Scripts/GetAPIBatch.js

This is an old revision of this page, as edited by Polygnotus (talk | contribs) at 22:06, 26 August 2024 (Created page with '(function() { 'use strict'; // Add styles GM_addStyle(` #word-fetcher-container { position: fixed; top: 10px; right: 10px; background-color: white; padding: 10px; border-radius: 8px; box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1); z-index: 9999; } #word-display { font-size: 1.2rem; margin-bottom: 0.5rem;...'). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
(diff) ← Previous revision | Latest revision (diff) | Newer revision → (diff)
Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
(function() {
    'use strict';

    // Add styles
    GM_addStyle(`
        #word-fetcher-container {
            position: fixed;
            top: 10px;
            right: 10px;
            background-color: white;
            padding: 10px;
            border-radius: 8px;
            box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
            z-index: 9999;
        }
        #word-display {
            font-size: 1.2rem;
            margin-bottom: 0.5rem;
            min-height: 1.5rem;
        }
        #next-button {
            font-size: 0.9rem;
            padding: 0.3rem 0.7rem;
            cursor: pointer;
        }
        #debug-info {
            margin-top: 0.5rem;
            font-size: 0.7rem;
            color: #666;
        }
    `);

    // Create and append UI elements
    const container = document.createElement('div');
    container.id = 'word-fetcher-container';
    container.innerHTML = `
        <div id="word-display">Click 'Next Word' to start</div>
        <button id="next-button">Next Word</button>
        <div id="debug-info"></div>
    `;
    document.body.appendChild(container);

    class TypoFetcher {
        constructor() {
            this.words = [];
            this.currentIndex = 0;
            this.displayElement = document.getElementById('word-display');
            this.nextButton = document.getElementById('next-button');
            this.debugElement = document.getElementById('debug-info');

            this.nextButton.addEventListener('click', () => this.displayNextWord());

            this.fetchWords();
        }

        async fetchWords() {
            try {
                const response = await fetch('http://localhost:8082/output');
                if (!response.ok) {
                    throw new Error('Network response was not ok');
                }
                const newWords = await response.json();
                this.words = [...this.words, ...newWords];
                this.updateDebugInfo();
            } catch (error) {
                console.error('Error fetching words:', error);
                this.debugElement.textContent = `Error: ${error.message}`;
            }
        }

        async displayNextWord() {
            if (this.currentIndex >= this.words.length) {
                await this.fetchWords();
            }

            if (this.words.length > 0) {
                this.displayElement.textContent = this.words[this.currentIndex];
                this.currentIndex++;

                if (this.currentIndex >= this.words.length) {
                    this.currentIndex = 0;
                    this.words = [];
                    this.fetchWords();
                }
            } else {
                this.displayElement.textContent = 'No more words available';
            }

            this.updateDebugInfo();
        }

        updateDebugInfo() {
            this.debugElement.textContent = `Words in buffer: ${this.words.length}, Current index: ${this.currentIndex}`;
        }
    }

    new TypoFetcher();
})();