-
Notifications
You must be signed in to change notification settings - Fork 0
/
spam.js
143 lines (117 loc) · 3.95 KB
/
spam.js
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
/*
EXAMPLE: @spam msg/api "message/apiName" 20 start
*/
const socialMediaPlatforms = {
whatsapp:
'div._ak1l .x1hx0egp.x6ikm8r.x1odjw0f.x1k6rcq7.x6prxxf[contenteditable="true"]',
telegram: '.input-message-input.scrollable.scrollable-y.no-scrollbar',
};
class API {
constructor(url) {
this.url = url;
}
async getResponse() {
console.log(`[SPAMMER] Fetching API: ${this.url}`);
const response = await fetch(this.url);
if (!response.ok) {
throw new Error('Failed to fetch API.');
}
const data = await response.json();
// save the JSON data into the object.
this.quote = data.value;
console.log('🚀 ~ API ~ getResponse ~ quote:', this.quote);
}
}
const chuckAPI = new API('https://api.chucknorris.io/jokes/random');
class Spammer {
constructor(selector) {
this.selector = selector;
}
// in milliseconds
speed = 500;
async spam() {
console.log('[SPAMMER] Writingag message...');
if (this.mode == 'api') {
await chuckAPI.getResponse().catch((err) => {
console.error(err);
});
this.msg = chuckAPI.quote;
}
const promise = new Promise((resolve, reject) => {
if (this.input) {
resolve(true);
}
});
promise
.then((result) => {
// fill the input box
this.input.textContent = this.msg;
// TODO: fix deleting first existing text for Whatsapp
// while (this.input.textContent != '') {
// this.input.dispatchEvent(
// new InputEvent("beforeinput", {
// inputType: "deleteHardLineBackward",
// bubbles: true,
// cancelable: true,
// }));
// }
this.input.dispatchEvent(
new InputEvent('beforeinput', {
inputType: 'insertReplacementText',
data: this.input.textContent,
bubbles: true,
cancelable: true,
})
);
})
.then((val) => {
// send message
this.input.dispatchEvent(
new KeyboardEvent('keydown', {
key: 'Enter',
code: 'Enter',
which: 13,
keyCode: 13,
})
);
});
}
start() {
const setTimeoutWrapper = () => {
// we need a timeout because otherwise dispatchEvent(kb) doesn't have enough time to run
setTimeout(() => {
this.spam();
}, this.speed);
};
for (let i = 0; i < this.times; i++) {
setTimeoutWrapper();
}
}
addEvent() {
// add event listener
this.input = document.querySelector(this.selector);
this.input.addEventListener('keyup', (event) => {
const splitted = event.target.textContent.split('"');
console.log(splitted);
if (splitted.length == 3 && splitted.join(' ').endsWith('start')) {
// msg / api
this.mode = splitted[0].split(' ')[1];
// actual message or API name
this.msg = splitted[1];
// number
this.times = +splitted[2].split(' ')[1].trim();
this.start();
}
});
}
}
function detectPlatform() {
// Thanks Stackoverflow: https://stackoverflow.com/a/16133523/21600888
const url = document.URL;
const name = Object.keys(socialMediaPlatforms).find((name) =>
url.includes(name)
);
return new Spammer(socialMediaPlatforms[name]);
}
const spammer = detectPlatform();
spammer.addEvent();