Repository navigation
Expand file tree
/
Copy pathmain.js
More file actions
304 lines (275 loc) · 11 KB
/
Copy pathmain.js
File metadata and controls
304 lines (275 loc) · 11 KB
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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
// Goes inside content script
class KeySim {
script_id_uuid = ""
pagescriptcode = function() {
class PageScript {
currentlyWriting = false;
percentageDone = 0;
doneWriting = false; // will always be true after first run but not during any runs
async sendState() {
window.postMessage({
type: 'eztyprState',
state: {
currentlyWriting: this.currentlyWriting,
percentageDone: this.percentageDone
}
}, '*');
}
async wordListProcessor(wordList) {
this.doneWriting = false;
this.currentlyWriting = true;
for (let i = 0; i < wordList.length; i++) {
const word = wordList[i];
console.log(`${word.filteredWord} and ${word.original}`)
this.percentageDone = Math.floor(wordList.length / wordList.indexOf(word)) * 100
const total_delay = (word.delay + word.random);
const [left_content, right_content] = word.original.split(word.filteredWord)
left_content != "" ? await this.typeString(left_content, left_content.length * 20) : {} // 20 ms per key
if (!word.mistake && !word.misspell) {
await this.typeString(word.filteredWord, total_delay)
} else if (word.mistake) {
await this.typeMistakeString(word.filteredWord, total_delay)
} else if (word.misspell) {
await this.typeMisspellString(word.filteredWord, total_delay)
}
right_content != "" ? await this.typeString(right_content, right_content.length * 20) : {} // 20 ms per key
await this.sendEventSequence("Space", 20);
}
this.currentlyWriting = false; //done from here on out
this.doneWriting = true;
}
async typeMistakeString(string, total_delay) {
const qwerty = "qwertyuiopasdfghjklzxcvbnm"
// sends the clossest 2 letters to the current one at offsets of 1 chars then deletes both of them and tries again
const delay_per_char = total_delay / string.length;
console.log(`mistaking word: ${string}`)
for (let i = 0; i < string.length; i++) {
const char = string[i];
if (char && qwerty.includes(char)) {
if (Math.random() > .75) { // misspells 75% of the time
// !TODO: figure out if the extra delay is necessary or not
const charToMistake = (qwerty[qwerty.indexOf(char) + (Math.random() > .5 ? -1 : +1)]);
console.log(charToMistake)
await this.sendEventSequence(charToMistake, delay_per_char)
console.log(char)
await this.sendEventSequence(char, delay_per_char)
// for some reason, sending one backspace makes it click twice
await this.sendEventSequence("Backspace", delay_per_char)
await this.sendEventSequence("Backspace", delay_per_char)
console.log(char)
await this.sendEventSequence(char, delay_per_char)
} else {
console.log(char)
await this.sendEventSequence(char, delay_per_char)
}
} else {
await this.sendEventSequence(char, delay_per_char)
}
}
}
async typeMisspellString(string, total_delay) {
const qwerty = "qwertyuiopasdfghjklzxcvbnm"
// typos a clossest letter to the current one at offsets of 2 chars then deletes the whole word and tries again
const delay_per_char = total_delay / string.length;
for (let i = 0; i < string.length; i++) {
const char = string[i];
if (string.indexOf(char) % 2 == 0 && qwerty.includes(char)) {
const closest_left = qwerty[qwerty.indexOf(char) - 1];
const closest_right = qwerty[qwerty.indexOf(char) + 1];
// On a coin toss it either send the left or right closes char
Math.random() > .5 ? await this.sendEventSequence(closest_left, delay_per_char) : await this.sendEventSequence(closest_right, delay_per_char)
} else {
await this.sendEventSequence(char, delay_per_char)
}
}
// !TODO: word delay is doubled here, this may or may not be wanted
// delete the entire word
for (const i = 0; i < string.length; i++) {
await this.sendEventSequence("Backspace", 20)
}
// rewrite it all over again
for (let i = 0; i < string.length; i++) {
await this.sendEventSequence(string[i], delay_per_char)
}
}
async typeString(string, total_delay) {
console.log(`typing string: ${string}`)
// simple function that types a string with no modifiers on it
const delay_per_char = total_delay / string.length;
for (let i = 0; i < string.length; i++) {
await this.sendEventSequence(string[i], delay_per_char)
}
}
async delay(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
async sendEventSequence(key, key_delay) {
if (key == undefined) return;
const keyevent = this.createKeyEvent(key);
if (keyevent.key == 'Backspace') {
// non printable keys appear on one key event so putting more then leads to repetiton
await this.sendKeyEvent("keyup", keyevent);
await this.delay(key_delay);
} else if (keyevent.key == 'Space') {
// non printable keys appear on one key event so putting more then leads to repetiton
await this.sendKeyEvent("keyup", keyevent);
await this.delay(key_delay);
}
// prepare the sequence of events for one word
const split_delay = key_delay / 3;
await this.sendKeyEvent("keydown", keyevent);
await this.delay(split_delay);
await this.sendKeyEvent("keypress", keyevent);
await this.delay(split_delay);
await this.sendKeyEvent("keyup", keyevent);
await this.delay(split_delay);
}
createKeyEvent(char) {
console.log(`Sending event ${char}`)
if (char == "Backspace") {
const backspaceEvent = {
bubbles: true,
cancelable: true,
key: 'Backspace',
code: 'Backspace',
keyCode: 8,
which: 8,
location: 0,
repeat: false,
timeStamp: Date.now(),
}; // backspace desnt have charcode because we only need to send keydown and keyup, no keypress
return backspaceEvent
} else if (char == "\n") {
const enterEvent = {
bubbles: true,
cancelable: true,
view: this.texteventiframe_window,
key: 'Enter',
code: 'Enter',
keyCode: 13,
which: 13,
location: 0,
repeat: false,
timeStamp: Date.now(),
};
return enterEvent
} else if (char == "Space") {
const spaceEvent = {
bubbles: true,
cancelable: true,
key: " ",
code: "Space",
location: 0,
repeat: false,
charCode: 0,
keyCode: 32,
which: 32,
timeStamp: Date.now(),
}
return spaceEvent;
} else {
const charCode = char.charCodeAt(0);
const codeString = "Key" + char.toUpperCase();
const keyevent = {
bubbles: true,
cancelable: true,
composed: true,
view: this.texteventiframe_window,
key: char,
code: codeString,
location: 0,
repeat: false,
charCode: charCode
};
return keyevent
}
}
async sendKeyEvent(eventType, keyevent) {
const keebEvent = new this.texteventiframe_window.KeyboardEvent(eventType, keyevent);
Object.defineProperties(keebEvent, {
keyCode: { value: keyevent.keyCode },
which: { value: keyevent.which },
//charCode: { value: eventType == 'keypress' ? keyevent.charCode : 0 },
// charCode may be optional in relation to backspace and enter
key: { value: keyevent.key },
code: { value: keyevent.code }
});
// !TODO: need to debug later because some parts are missing that might be needed that i think arent requered
return this.texteventiframe_doc.dispatchEvent(keebEvent);
}
constructor() {
// create the iframe field here because class field definetions cannot run logic
this.texteventiframe = document.querySelector('.docs-texteventtarget-iframe');
this.texteventiframe_window = this.texteventiframe.contentWindow;
this.texteventiframe_doc = this.texteventiframe_window.document;
//Start the event listener
window.addEventListener('message', async (event) => {
if (event.data.type === 'eztyprWrite') {
const data = event.data;
await this.wordListProcessor(event.data.textList)
}
//need to listen to state requests too
if (event.data.type === 'eztyprStateRequest') {
await this.sendState()
}
});
document.addEventListener('keydown', async (e) => {
if (e.ctrlKey && e.shiftKey && e.code === 'KeyY') {
alert("Sending Backspace event")
this.sendEventSequence("Backspace", 20)
}
});
}
}
const pagescript = new PageScript();
//page script is started up to this point
}
injectPageScript() {
if (!this.script_id_uuid) {
const script = document.createElement('script');
console.log("Script injected")
script.id = crypto.randomUUID();
script.textContent = `(${this.pagescriptcode.toString()})();`;
(document.head || document.documentElement).appendChild(script);
console.log(`script id is ${script.id}`)
this.script_id_uuid = script.id;
}
}
constructor() {
//listen to wriitng state messages
window.addEventListener('message', async (event) => {
if (event.data.type === 'eztyprState') {
if (event.source !== window) return;
try {
const runtime = (typeof browser !== 'undefined') ? browser : chrome;
runtime.runtime.sendMessage({
command: "state",
state: event.data.state,
});
} catch (error) {
console.error("Error:", error);
}
}
}
);
const runtime = (typeof browser !== 'undefined') ? browser : chrome;
runtime.runtime.onMessage.addListener((message) => {
if (message.command === "write") {
console.log("Got write command from background script")
// Relay message to the injected script
window.postMessage({
type: 'eztyprWrite',
textList: message.textToWrite
}, '*');
}
if (message.command === "stateRequest") {
// Relay message to the injected script
window.postMessage({
type: 'eztyprStateRequest',
}, '*');
}
});
}
}
const keysim = new KeySim()
keysim.injectPageScript()