File size: 3,965 Bytes
364eb96
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
(function () {
    console.log("%c Monkeytype Legacy / Input Event Typer ", "background: #222; color: #ff00ff; font-size: 20px");

    const CONFIG = {
        startDelay: 100,
        typingDelay: 20, // Slightly slower to be safe
    };

    let isArmed = true;

    // Different method: Focus purely on InputEvent which carries the character
    // Many modern sites (React based) care more about 'beforeinput' and 'input' than keydown.
    function typeChar(char) {
        const target = document.activeElement || document.body;

        // 1. Dispatch 'beforeinput' (modern standard)
        const beforeInput = new InputEvent('beforeinput', {
            data: char,
            inputType: 'insertText',
            bubbles: true,
            cancelable: true,
            view: window,
            isTrusted: true // We hope...
        });
        target.dispatchEvent(beforeInput);

        // 2. Dispatch 'input'
        const inputEvent = new InputEvent('input', {
            data: char,
            inputType: 'insertText',
            bubbles: true,
            cancelable: true,
            view: window,
        });
        target.dispatchEvent(inputEvent);

        // 3. Dispatch 'textInput' (legacy, but sometimes checked)
        if (document.createEvent) {
            const textEvent = document.createEvent('TextEvent');
            textEvent.initTextEvent('textInput', true, true, window, char, 0, "en-US");
            target.dispatchEvent(textEvent);
        }
    }

    async function autoType(text) {
        console.log(`Typing ${text.length} chars (InputEvent only)...`);
        for (let i = 0; i < text.length; i++) {
            typeChar(text[i]);
            if (CONFIG.typingDelay > 0) {
                await new Promise(r => setTimeout(r, CONFIG.typingDelay));
            }
        }
    }

    const triggerHandler = (e) => {
        if (!isArmed) return;

        if (e.key.length === 1 && !e.ctrlKey && !e.altKey && !e.metaKey) {

            const activeWord = document.querySelector('#words .word.active');
            if (!activeWord) return;

            const firstLetterElement = activeWord.querySelector('letter');
            const firstLetter = firstLetterElement ? firstLetterElement.textContent : null;

            if (firstLetter && e.key === firstLetter) {
                isArmed = false;

                // Get ALL remaining text from DOM
                const allWords = document.querySelectorAll('#words .word');
                let fullBuffer = "";
                let foundActive = false;

                allWords.forEach(word => {
                    if (word === activeWord) {
                        foundActive = true;
                        const letters = word.querySelectorAll('letter');
                        // Start from index 1 (user just typed 0)
                        for (let i = 1; i < letters.length; i++) {
                            fullBuffer += letters[i].textContent;
                        }
                        fullBuffer += " ";
                    } else if (foundActive) {
                        const letters = word.querySelectorAll('letter');
                        letters.forEach(l => fullBuffer += l.textContent);
                        fullBuffer += " ";
                    }
                });

                if (fullBuffer.endsWith(" ")) {
                    fullBuffer = fullBuffer.slice(0, -1);
                }

                window.removeEventListener('keydown', triggerHandler);

                console.log("Trigger detected. Starting Legacy Typer...");
                setTimeout(() => {
                    autoType(fullBuffer);
                }, CONFIG.startDelay);
            }
        }
    };

    window.addEventListener('keydown', triggerHandler);
    console.log("READY! Type the first letter to test Legacy Mode.");
})();