File size: 9,314 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
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
// MnkLightning v3 - Browser-Based Human Typer
// Merges logic from Python script and JS injector into a single userscript/console script.

(function () {
    console.log("%c MnkLightning v3 Active ", "background: #222; color: #00ff00; font-size: 16px");

    // === CONFIGURATION ===
    const CONFIG = {
        min_wpm: 100,
        max_wpm: 140,
        // Key Hold Time (Seconds)
        min_key_hold: 0.01,
        max_key_hold: 0.04,

        // Imperfection Rates (0.0 to 1.0)
        wrong_char_rate: 0.005,
        adjacent_key_rate: 0.005,
        double_letter_rate: 0.001,
        skip_letter_rate: 0.001,

        hesitation_rate: 0.02,
        burst_rate: 0.08,
        insane_burst_rate: 0.001,

        // Timing Multipliers
        hesitation_multiplier: 2.5,
        burst_speed_multiplier: 0.6,
        post_mistake_pause: 0.4,
        word_start_slowdown: 1.2,
    };

    // QWERTY Adjacency Map
    const ADJACENT_KEYS = {
        'a': 'qwsz', 'b': 'vghn', 'c': 'xdfv', 'd': 'serfcx', 'e': 'wsdr',
        'f': 'drtgvc', 'g': 'ftyhbv', 'h': 'gyujnb', 'i': 'ujko', 'j': 'huikmn',
        'k': 'jiolm', 'l': 'kop', 'm': 'njk', 'n': 'bhjm', 'o': 'iklp',
        'p': 'ol', 'q': 'wa', 'r': 'edft', 's': 'awedxz', 't': 'rfgy',
        'u': 'yhji', 'v': 'cfgb', 'w': 'qase', 'x': 'zsdc', 'y': 'tghu',
        'z': 'asx'
    };

    // Global State
    let isTyping = false;
    let abortTyping = false;

    // === HELPERS ===

    const sleep = (ms) => new Promise(resolve => setTimeout(resolve, ms));
    const randomUniform = (min, max) => Math.random() * (max - min) + min;
    const randomInt = (min, max) => Math.floor(Math.random() * (max - min + 1)) + min;
    const randomChoice = (arr) => arr[Math.floor(Math.random() * arr.length)];
    const randomChar = () => "abcdefghijklmnopqrstuvwxyz"[Math.floor(Math.random() * 26)];

    function getKeystrokeDelay(wpm, inBurst = false) {
        const cpm = wpm * 5;
        let baseDelay = 60 / cpm;

        if (inBurst) {
            baseDelay *= CONFIG.burst_speed_multiplier;
        }

        const variance = baseDelay * 0.20;
        const noise = randomUniform(-variance, variance);
        return Math.max(0.005, baseDelay + noise) * 1000;
    }

    function getAdjacentKey(char) {
        const charLower = char.toLowerCase();
        if (ADJACENT_KEYS[charLower]) {
            const adj = randomChoice(ADJACENT_KEYS[charLower]);
            return char === char.toUpperCase() ? adj.toUpperCase() : adj;
        }
        return char;
    }

    // === INPUT SIMULATION ===

    const CODE_MAP = {
        ' ': 'Space',
        'Enter': 'Enter',
        'Backspace': 'Backspace',
        'Escape': 'Escape',
        ',': 'Comma',
        '.': 'Period',
        '/': 'Slash',
        ';': 'Semicolon',
        "'": 'Quote',
        '[': 'BracketLeft',
        ']': 'BracketRight',
        '\\': 'Backslash',
        '-': 'Minus',
        '=': 'Equal',
        '`': 'Backquote',
        '1': 'Digit1', '2': 'Digit2', '3': 'Digit3', '4': 'Digit4', '5': 'Digit5',
        '6': 'Digit6', '7': 'Digit7', '8': 'Digit8', '9': 'Digit9', '0': 'Digit0'
    };

    function dispatchKey(key, type) {
        let code;

        // 1. Check strict map
        if (CODE_MAP[key]) {
            code = CODE_MAP[key];
        }
        // 2. Letters
        else if (key.length === 1 && /[a-zA-Z]/.test(key)) {
            code = `Key${key.toUpperCase()}`;
        }
        // 3. Fallback for shifted punctuation
        else if (key === '!') code = 'Digit1';
        else if (key === '@') code = 'Digit2';
        else if (key === '#') code = 'Digit3';
        else if (key === '$') code = 'Digit4';
        else if (key === '%') code = 'Digit5';
        else if (key === '^') code = 'Digit6';
        else if (key === '&') code = 'Digit7';
        else if (key === '*') code = 'Digit8';
        else if (key === '(') code = 'Digit9';
        else if (key === ')') code = 'Digit0';
        else if (key === '_') code = 'Minus';
        else if (key === '+') code = 'Equal';
        else if (key === '{') code = 'BracketLeft';
        else if (key === '}') code = 'BracketRight';
        else if (key === '|') code = 'Backslash';
        else if (key === ':') code = 'Semicolon';
        else if (key === '"') code = 'Quote';
        else if (key === '<') code = 'Comma';
        else if (key === '>') code = 'Period';
        else if (key === '?') code = 'Slash';
        else if (key === '~') code = 'Backquote';
        else {
            code = key;
        }

        const event = new KeyboardEvent(type, {
            key: key,
            code: code,
            bubbles: true,
            cancelable: true,
            view: window,
            which: key.charCodeAt(0),
            keyCode: key.charCodeAt(0)
        });

        const target = document.activeElement || document.body;
        target.dispatchEvent(event);
    }

    async function typeChar(char) {
        dispatchKey(char, 'keydown');
        dispatchKey(char, 'keypress');

        const holdTime = randomUniform(CONFIG.min_key_hold, CONFIG.max_key_hold) * 1000;
        await sleep(holdTime);
        dispatchKey(char, 'keyup');
    }

    async function simulateBackspace(count = 1) {
        for (let i = 0; i < count; i++) {
            dispatchKey('Backspace', 'keydown');
            await sleep(randomUniform(CONFIG.min_key_hold, CONFIG.max_key_hold) * 1000);
            dispatchKey('Backspace', 'keyup');
            await sleep(randomUniform(50, 100));
        }
    }

    // === CORE LOGIC ===

    function getVisibleText() {
        const words = document.querySelectorAll('.word');
        if (words.length === 0) return null;

        let text = "";
        words.forEach((word) => {
            word.querySelectorAll('letter').forEach(letter => {
                text += letter.textContent;
            });
            text += " ";
        });
        return text.trim();
    }

    async function startTyping() {
        if (isTyping) return;

        const text = getVisibleText();
        if (!text) {
            console.error("MnkLightning: No text found! Are you in a test?");
            return;
        }

        // Try to focus the game area if possible
        const gameWords = document.getElementById('words');
        if (gameWords) {
            gameWords.click(); // Hack to ensure focus
        }

        isTyping = true;
        abortTyping = false;

        const wpm = randomInt(CONFIG.min_wpm, CONFIG.max_wpm);
        console.log(`MnkLightning: Starting... Target WPM: ${wpm}, Length: ${text.length}`);

        let idx = 0;
        let burstRemaining = 0;

        try {
            while (idx < text.length) {
                if (abortTyping) break;

                const char = text[idx];
                let delay = getKeystrokeDelay(wpm, burstRemaining > 0);

                if (burstRemaining > 0) burstRemaining--;

                // === IMPERFECTIONS ===

                if (Math.random() < CONFIG.hesitation_rate) {
                    delay *= CONFIG.hesitation_multiplier;
                }

                if (burstRemaining === 0 && Math.random() < CONFIG.burst_rate) {
                    burstRemaining = 5;
                }

                // Insane Burst
                if (Math.random() < CONFIG.insane_burst_rate) {
                    const burstLen = randomInt(4, 5);
                    for (let i = 0; i < burstLen; i++) {
                        if (idx >= text.length || abortTyping) break;
                        dispatchKey(text[idx], 'keydown');
                        dispatchKey(text[idx], 'keyup');
                        idx++;
                        await sleep(2);
                    }
                    continue;
                }

                // Mistakes
                if (Math.random() < CONFIG.wrong_char_rate && /[a-zA-Z0-9]/.test(char)) {
                    const wrong = randomChar();
                    await typeChar(wrong);
                    await sleep(delay * 2);
                    await simulateBackspace(1);
                    await sleep(delay);
                }

                await typeChar(char);
                idx++;

                if (char === ' ') {
                    delay *= CONFIG.word_start_slowdown;
                }

                await sleep(delay);
            }
        } catch (e) {
            console.error(e);
        }

        console.log("MnkLightning: Finished.");
        isTyping = false;
    }

    // === LISTENERS ===

    window.addEventListener('keydown', (e) => {
        if (e.key === 'Insert') {
            e.preventDefault();
            if (isTyping) {
                console.log("MnkLightning: Already running!");
                return;
            }
            startTyping();
        }
        if (e.key === 'Escape') {
            abortTyping = true;
            if (isTyping) {
                console.log("MnkLightning: Aborting...");
                isTyping = false;
            }
        }
    });

})();