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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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
(function () {
    console.log("%c Monkeytype Command Typer (Human-Like) ", "background: #222; color: #ff0000; font-size: 20px");

    const CONFIG = {
        minWPM: 310,              // Minimum expected WPM
        maxWPM: 550,              // Maximum expected WPM
        startDelay: 50,           // Delay before starting to type

        // === HUMAN IMPERFECTION RATES ===
        // Wrong character (random letter instead of correct one)
        wrongCharRate: 0.025,

        // Adjacent key typo (hit a nearby key on keyboard)
        adjacentKeyRate: 0.02,

        // Double/triple letter (accidentally press key multiple times)
        doubleLetterRate: 0.015,
        tripleLetterRate: 0.003,

        // Skip letter (finger moved too fast, missed a key)
        skipLetterRate: 0.01,

        // Transposed letters (swap two adjacent letters like "teh" instead of "the")
        transposeRate: 0.012,

        // Ctrl+Backspace (delete whole word when frustrated)
        ctrlBackspaceRate: 0.008,

        // Hesitation/thinking pause (longer pause before difficult letters)
        hesitationRate: 0.04,
        hesitationMultiplier: 3.5,  // How much longer the pause is

        // Burst typing (fast typing followed by slowdown)
        burstTypingRate: 0.08,
        burstSpeedMultiplier: 0.5,  // Faster during burst
        burstLength: 5,             // Characters in burst

        // Slow start to words (first letter of word typed slower)
        wordStartSlowdown: 1.8,

        // Fatigue simulation (gradually slow down over time)
        fatigueEnabled: true,
        fatigueRate: 0.0001,        // How much to slow down per character

        // Recovery pause after mistakes (humans pause after making errors)
        postMistakePauseMultiplier: 2.0,
    };

    // QWERTY keyboard adjacency map for realistic typos
    const ADJACENT_KEYS = {
        'a': ['q', 'w', 's', 'z'],
        'b': ['v', 'g', 'h', 'n'],
        'c': ['x', 'd', 'f', 'v'],
        'd': ['s', 'e', 'r', 'f', 'c', 'x'],
        'e': ['w', 's', 'd', 'r'],
        'f': ['d', 'r', 't', 'g', 'v', 'c'],
        'g': ['f', 't', 'y', 'h', 'b', 'v'],
        'h': ['g', 'y', 'u', 'j', 'n', 'b'],
        'i': ['u', 'j', 'k', 'o'],
        'j': ['h', 'u', 'i', 'k', 'm', 'n'],
        'k': ['j', 'i', 'o', 'l', 'm'],
        'l': ['k', 'o', 'p'],
        'm': ['n', 'j', 'k'],
        'n': ['b', 'h', 'j', 'm'],
        'o': ['i', 'k', 'l', 'p'],
        'p': ['o', 'l'],
        'q': ['w', 'a'],
        'r': ['e', 'd', 'f', 't'],
        's': ['a', 'w', 'e', 'd', 'x', 'z'],
        't': ['r', 'f', 'g', 'y'],
        'u': ['y', 'h', 'j', 'i'],
        'v': ['c', 'f', 'g', 'b'],
        'w': ['q', 'a', 's', 'e'],
        'x': ['z', 's', 'd', 'c'],
        'y': ['t', 'g', 'h', 'u'],
        'z': ['a', 's', 'x'],
    };

    let isArmed = true;
    let inBurstMode = false;
    let burstCharsRemaining = 0;
    let totalCharsTyped = 0;
    let recentMistake = false;

    // Type a single character
    function typeChar(char) {
        const target = document.activeElement || document.body;
        const keyConfig = {
            key: char,
            code: char === ' ' ? 'Space' : `Key${char.toUpperCase()}`,
            bubbles: true,
            cancelable: true,
            view: window
        };

        target.dispatchEvent(new KeyboardEvent('keydown', keyConfig));
        target.dispatchEvent(new KeyboardEvent('keypress', keyConfig));
        document.execCommand('insertText', false, char);
        target.dispatchEvent(new KeyboardEvent('keyup', keyConfig));

        totalCharsTyped++;
    }

    // Single backspace
    function typeBackspace() {
        const target = document.activeElement || document.body;
        const bsConfig = { key: 'Backspace', code: 'Backspace', bubbles: true, cancelable: true, view: window };
        target.dispatchEvent(new KeyboardEvent('keydown', bsConfig));
        document.execCommand('delete', false, null);
        target.dispatchEvent(new KeyboardEvent('keyup', bsConfig));
    }

    // Ctrl+Backspace to delete whole word
    function typeCtrlBackspace() {
        const target = document.activeElement || document.body;
        const ctrlBsConfig = {
            key: 'Backspace',
            code: 'Backspace',
            ctrlKey: true,
            bubbles: true,
            cancelable: true,
            view: window
        };

        target.dispatchEvent(new KeyboardEvent('keydown', ctrlBsConfig));

        // Delete characters until we hit a space or beginning
        const activeWord = document.querySelector('#words .word.active');
        if (activeWord) {
            const incorrectLetters = activeWord.querySelectorAll('letter.incorrect, letter.extra');
            incorrectLetters.forEach(() => {
                document.execCommand('delete', false, null);
            });
        }

        target.dispatchEvent(new KeyboardEvent('keyup', ctrlBsConfig));
    }

    function sleep(ms) {
        return new Promise(resolve => setTimeout(resolve, ms));
    }

    // Get adjacent key for realistic typo
    function getAdjacentKey(char) {
        const lowerChar = char.toLowerCase();
        const adjacent = ADJACENT_KEYS[lowerChar];
        if (adjacent && adjacent.length > 0) {
            const randomAdj = adjacent[Math.floor(Math.random() * adjacent.length)];
            return char === char.toUpperCase() ? randomAdj.toUpperCase() : randomAdj;
        }
        return char; // Fallback to same char if no adjacent found
    }

    // Get random wrong character
    function getRandomChar() {
        const chars = "abcdefghijklmnopqrstuvwxyz";
        return chars.charAt(Math.floor(Math.random() * chars.length));
    }

    // Calculate keystroke delay with fatigue and burst mode
    function getKeystrokeDelay() {
        const currentWPM = Math.floor(Math.random() * (CONFIG.maxWPM - CONFIG.minWPM + 1)) + CONFIG.minWPM;
        let baseDelay = 60000 / (currentWPM * 5);

        // Apply fatigue (gradually slow down)
        if (CONFIG.fatigueEnabled) {
            baseDelay *= (1 + totalCharsTyped * CONFIG.fatigueRate);
        }

        // Apply burst mode (faster typing)
        if (inBurstMode && burstCharsRemaining > 0) {
            baseDelay *= CONFIG.burstSpeedMultiplier;
            burstCharsRemaining--;
            if (burstCharsRemaining === 0) {
                inBurstMode = false;
            }
        }

        // Add variance
        const variance = baseDelay * 0.25;
        const noise = (Math.random() * variance * 2) - variance;

        return Math.max(8, baseDelay + noise);
    }

    // Get current word's remaining untyped text
    function getCurrentWordText() {
        const activeWord = document.querySelector('#words .word.active');
        if (!activeWord) return null;

        const letters = activeWord.querySelectorAll('letter');
        let text = "";
        let foundUntyped = false;

        for (const letter of letters) {
            if (!letter.classList.contains('correct') && !letter.classList.contains('incorrect')) {
                foundUntyped = true;
            }
            if (foundUntyped) {
                text += letter.textContent;
            }
        }

        return text;
    }

    // Count how many incorrect letters we've typed in current word
    function getIncorrectCount() {
        const activeWord = document.querySelector('#words .word.active');
        if (!activeWord) return 0;
        return activeWord.querySelectorAll('letter.incorrect, letter.extra').length;
    }

    // Check if we're at the start of a word
    function isWordStart() {
        const activeWord = document.querySelector('#words .word.active');
        if (!activeWord) return false;
        const typed = activeWord.querySelectorAll('letter.correct, letter.incorrect');
        return typed.length === 0;
    }

    // === MISTAKE SIMULATION FUNCTIONS ===

    // Type wrong character and correct it
    async function simulateWrongChar(correctChar) {
        const wrongChar = getRandomChar();
        typeChar(wrongChar);
        await sleep(getKeystrokeDelay() * 2.5);
        typeBackspace();
        await sleep(getKeystrokeDelay() * 1.5);
        recentMistake = true;
    }

    // Type adjacent key typo and correct it
    async function simulateAdjacentKeyTypo(correctChar) {
        const adjacentChar = getAdjacentKey(correctChar);
        if (adjacentChar !== correctChar) {
            typeChar(adjacentChar);
            await sleep(getKeystrokeDelay() * 2.2);
            typeBackspace();
            await sleep(getKeystrokeDelay() * 1.3);
            recentMistake = true;
        }
    }

    // Double or triple letter mistake
    async function simulateDoubleLetter(char, count = 2) {
        // Type the correct char first, then extra(s)
        for (let i = 1; i < count; i++) {
            typeChar(char);
            await sleep(getKeystrokeDelay() * 0.3); // Very fast double tap
        }
        // Pause to realize mistake
        await sleep(getKeystrokeDelay() * 2.0);
        // Delete the extras
        for (let i = 1; i < count; i++) {
            typeBackspace();
            await sleep(getKeystrokeDelay() * 0.5);
        }
        await sleep(getKeystrokeDelay() * 1.2);
        recentMistake = true;
    }

    // Skip a letter (will be detected as wrong, then backspace and fix)
    async function simulateSkipLetter() {
        // The letter gets skipped - we don't type it
        // The next letter will be typed in its place
        // This creates a natural error that gets corrected
        return true; // Signal that we should skip
    }

    // Transpose two letters (type in wrong order, then fix)
    async function simulateTranspose(char1, char2) {
        // Type second char first
        typeChar(char2);
        await sleep(getKeystrokeDelay() * 0.4);
        // Type first char second
        typeChar(char1);
        await sleep(getKeystrokeDelay() * 2.5);
        // Delete both
        typeBackspace();
        await sleep(getKeystrokeDelay() * 0.4);
        typeBackspace();
        await sleep(getKeystrokeDelay() * 1.5);
        recentMistake = true;
        // Return true so main loop knows we handled these chars
        return true;
    }

    // Use Ctrl+Backspace to clear word and retype
    async function simulateCtrlBackspace() {
        const incorrectCount = getIncorrectCount();
        if (incorrectCount > 2) {
            await sleep(getKeystrokeDelay() * 3); // Frustration pause
            typeCtrlBackspace();
            await sleep(getKeystrokeDelay() * 2);
            recentMistake = true;
            return true;
        }
        return false;
    }

    // Main typing loop with all human imperfections
    async function autoTypeLoop() {
        console.log("Starting human-like auto-type loop...");
        console.log("Active imperfections: wrong char, adjacent key, double/triple letter, skip, transpose, Ctrl+Backspace, hesitation, burst typing, fatigue");

        let wordCount = 0;
        let skipNext = false;

        while (true) {
            const currentWordText = getCurrentWordText();

            if (currentWordText === null) {
                console.log(`Auto-type complete! Typed ${wordCount} words, ${totalCharsTyped} characters.`);
                break;
            }

            // Check if we should use Ctrl+Backspace (when frustrated with errors)
            if (Math.random() < CONFIG.ctrlBackspaceRate) {
                const didCtrlBackspace = await simulateCtrlBackspace();
                if (didCtrlBackspace) continue;
            }

            if (currentWordText.length === 0) {
                const activeWord = document.querySelector('#words .word.active');
                const nextWord = activeWord ? activeWord.nextElementSibling : null;

                if (!nextWord || !nextWord.classList.contains('word')) {
                    await sleep(50);
                    const stillActive = document.querySelector('#words .word.active');
                    if (!stillActive) {
                        console.log(`Auto-type complete! Typed ${wordCount} words.`);
                        break;
                    }
                    continue;
                }

                typeChar(' ');
                wordCount++;

                // Longer pause between words
                let delay = getKeystrokeDelay() * 1.4;
                await sleep(delay);
                continue;
            }

            const char = currentWordText[0];
            const nextChar = currentWordText.length > 1 ? currentWordText[1] : null;
            let delay = getKeystrokeDelay();

            // === APPLY HUMAN IMPERFECTIONS ===

            // Slow start to words
            if (isWordStart()) {
                delay *= CONFIG.wordStartSlowdown;
            }

            // Random hesitation (thinking pause)
            if (Math.random() < CONFIG.hesitationRate) {
                delay *= CONFIG.hesitationMultiplier;
            }

            // Post-mistake recovery pause
            if (recentMistake) {
                delay *= CONFIG.postMistakePauseMultiplier;
                recentMistake = false;
            }

            // Trigger burst mode randomly
            if (!inBurstMode && Math.random() < CONFIG.burstTypingRate) {
                inBurstMode = true;
                burstCharsRemaining = CONFIG.burstLength;
            }

            // Skip letter imperfection (handled by typing next char instead, creating error)
            if (/[a-zA-Z]/.test(char) && Math.random() < CONFIG.skipLetterRate && nextChar) {
                // Skip this char - type next char instead (creates transposition-like error)
                typeChar(nextChar);
                await sleep(getKeystrokeDelay() * 2.5);
                typeBackspace();
                await sleep(getKeystrokeDelay() * 1.3);
                recentMistake = true;
                // Now type correct char
                typeChar(char);
                await sleep(delay);
                continue;
            }

            // Transpose letters
            if (/[a-zA-Z]/.test(char) && nextChar && /[a-zA-Z]/.test(nextChar) && Math.random() < CONFIG.transposeRate) {
                await simulateTranspose(char, nextChar);
                // Type both chars correctly now
                typeChar(char);
                await sleep(getKeystrokeDelay());
                typeChar(nextChar);
                await sleep(delay);
                // Skip the next character in main loop since we already typed it
                skipNext = true;
                continue;
            }

            // Triple letter (rarer)
            if (/[a-zA-Z]/.test(char) && Math.random() < CONFIG.tripleLetterRate) {
                typeChar(char); // Type the correct one first
                await simulateDoubleLetter(char, 3);
                await sleep(delay);
                continue;
            }

            // Double letter
            if (/[a-zA-Z]/.test(char) && Math.random() < CONFIG.doubleLetterRate) {
                typeChar(char); // Type the correct one first
                await simulateDoubleLetter(char, 2);
                await sleep(delay);
                continue;
            }

            // Adjacent key typo
            if (/[a-zA-Z]/.test(char) && Math.random() < CONFIG.adjacentKeyRate) {
                await simulateAdjacentKeyTypo(char);
            }

            // Wrong character
            if (/[a-zA-Z]/.test(char) && Math.random() < CONFIG.wrongCharRate) {
                await simulateWrongChar(char);
            }

            // Type the correct character
            typeChar(char);
            await sleep(delay);
        }
    }

    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;
                window.removeEventListener('keydown', triggerHandler);

                console.log("Trigger detected. Starting Human-Like Command Typer...");
                console.log("Config:", CONFIG);
                setTimeout(() => {
                    autoTypeLoop();
                }, CONFIG.startDelay);
            }
        }
    };

    window.addEventListener('keydown', triggerHandler);
    console.log("READY! Type the first letter to activate Human-Like Mode.");
    console.log("Imperfections enabled: wrong char, adjacent key typo, double/triple letter, skip, transpose, Ctrl+Backspace, hesitation, burst typing, fatigue, word-start slowdown");
})();