-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathautoWootalk.js
543 lines (501 loc) · 17.3 KB
/
autoWootalk.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
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
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
class wooControler {
constructor() {
this.debug = [];
this.bot = 0;
this.myTexts = [];
this.otherTexts = [];
this.texts = [];
this.historyMyTexts = [];
this.historyOtherTexts = [];
this.historyTexts = [];
this.recordOtherId = 0;
this.otherId = 0;
this.replyMid = -1;
this.sendFirstTextFlag = false;
this.blockListId = 0;
this.appendAutostartCheckBox();
this.appendAutoReplyCheckBox();
this.appendBlockCheckBox();
this.appendBlockList();
this.autoRecord();
document.querySelector('#checkbox-autostart').onclick = () => {
this.autoStart();
}
document.querySelector('#checkbox-autoReply').onclick = () => {
this.autoReply();
}
document.querySelector('#checkbox-block').onclick = () => {
this.block();
}
document.querySelector('#add-block-list').onclick = () => {
this.appendBlockList();
}
}
resetText() {
this.myTexts = [];
this.otherTexts = [];
this.replyMid = -1;
}
extractOtherText(text) {
if(typeof text === 'object') {
text = text.textContent;
}
text = text.slice('陌生人:'.length, -1);
text = text.slice(0, text.lastIndexOf(' ('));
return text;
}
extractMyText(text) {
if(typeof text === 'object') {
text = text.textContent;
}
text = text.slice('我:'.length, -1);
text = text.slice(0, text.lastIndexOf('已'));
return text;
}
_clearInput() {
messageInput.value = '';
}
send(text) {
if(document.querySelector('#sendButton input').value !== "回報" && this.isChat()) {
messageInput.value = text;
document.querySelector('#sendButton input').click()
}
}
sendBotReply(otherId, text) {
if(otherId != this.otherId) { return; }
if(document.querySelector('#sendButton input').value !== "回報" && this.isChat()) {
messageInput.value = text;
document.querySelector('#sendButton input').click()
}
}
sendMsg() {
if (document.querySelector('#sendButton input').value !== "回報" && this.isChat()) {
document.querySelector('#sendButton input').click()
}
}
isTitle() {
let systemTexts = document.querySelectorAll('.system.text');
if(systemTexts.length === 1) {
let otherTexts = document.querySelectorAll('.stranger.text');
let myTexts = document.querySelectorAll('.my.text');
if(otherTexts.length === 0 &&
myTexts.length === 0) {
return true;
}
} else {
return false;
}
}
isWait() {
let systemTexts = document.querySelectorAll('.system.text');
if(systemTexts.length === 3 &&
systemTexts[0].textContent === "系統訊息:粉絲專頁 | 尋人啟事 | App下載") {
return true;
} else {
return false;
}
}
isChat() {
let systemTexts = document.querySelectorAll('.system.text');
if(systemTexts.length >= 2) {
let systemTextSp = systemTexts[systemTexts.length-2].textContent.split("離開");
if (systemTextSp[1] === "了,請按") {
return false;
}
}
if(systemTexts.length >= 3 &&
(systemTexts[0].textContent === "系統訊息:加密連線完成,開始聊天囉!" ||
systemTexts[2].textContent === "系統訊息:加密連線完成,開始聊天囉!")) {
return true;
}
if(systemTexts.length >= 1) {
let otherTexts = document.querySelectorAll('.stranger.text');
let myTexts = document.querySelectorAll('.my.text');
if(otherTexts.length > 0 || myTexts.length > 0) {
return true;
}
}
return false;
}
isLeave() {
let systemTexts = document.querySelectorAll('.system.text');
if(systemTexts.length >= 2) {
let systemTextSp = systemTexts[systemTexts.length - 2].textContent.split("離開")
if (systemTextSp[1] === "了,請按") {
this.updateRecord();
return true;
}
}
return false;
}
leaveChat() {
document.querySelector('#changeButton > input').click();
let ensureText = document.querySelector('#ensureText');
if(ensureText !== null) {
ensureText.value = 'leave';
}
document.querySelector('#popup-yes').click();
}
sendFirstText = () => {
if(this.sendFirstTextInterval === void 0 &&
document.querySelectorAll('.me.text').length === 0) {
const self = this;
this.sendFirstTextInterval = setInterval(() => {
let firstText = document.querySelector('#first-text').value
let stopFlag = false;
for(let sendedText of document.querySelectorAll('.me.text')) {
if(self.extractMyText(sendedText) === firstText) {
stopFlag = true;
}
}
if(stopFlag || self.isTitle() || self.isLeave()) {
clearInterval(self.sendFirstTextInterval);
self.sendFirstTextInterval = undefined;
} else {
document.getElementById('messageInput').value = firstText;
self.sendMsg();
}
}, 300);
}
}
updateRecord() {
if(this.myTexts.length == 0 && this.otherTexts.length && !this.isChat()) { return; }
let myTexts = [];
document.querySelectorAll('.me.text').forEach(e => myTexts.push({'mid':+e.getAttribute('mid'), 'text':this.extractMyText(e.textContent)}));
let otherTexts = [];
document.querySelectorAll('.stranger.text').forEach(e => otherTexts.push({'mid':+e.getAttribute('mid'), 'text':this.extractOtherText(e.textContent)}));
if(myTexts.length !== 0) {
let lastMidOfMyText;
if(this.myTexts.length !== 0) {
lastMidOfMyText = this.myTexts[this.myTexts.length - 1].mid;
} else {
lastMidOfMyText = -1;
}
for(let msg of myTexts) {
if(msg.mid > lastMidOfMyText) {
this.myTexts.push(msg);
}
}
}
if(otherTexts.length !== 0) {
let lastMidOfOtherText;
if(this.otherTexts.length !== 0) {
lastMidOfOtherText = this.otherTexts[this.otherTexts.length - 1].mid;
} else {
lastMidOfOtherText = -1;
}
for(let msg of otherTexts) {
if(msg.mid > lastMidOfOtherText) {
this.otherTexts.push(msg);
}
}
}
}
autoRecord() {
const self = this;
this.recordInterval = setInterval(() => {
self.updateRecord();
if((!self.isChat()) && (self.myTexts.length || self.otherTexts.length)) {
self.historyMyTexts.push(self.myTexts);
self.historyOtherTexts.push(self.otherTexts);
self.resetText();
self.leaveChat();
}
}, 1000);
}
autoStart() {
if(this.autoStartInterval === void 0) {
const self = this;
this.autoStartInterval = setInterval(() => {
let systemTexts = document.querySelectorAll('.system.text');
if(systemTexts[0].textContent === "系統訊息:WooTalk系統出了一些狀況,我們正在緊急維護中U+0001F605請稍後即可繼續聊天至粉絲專頁獲取更多相關資訊") {
self.sendFirstTextFlag = false;
setTimeout(() => {
self.leaveChat();
}, 30000);
} else if (self.isTitle()) {
document.querySelector('#startButton').click()
let firstText = document.querySelector('#first-text').value;
self.otherId = self.historyOtherTexts.length;
if(firstText) { self.sendFirstTextFlag = true; }
} else if(self.isWait() || self.isChat()) {
if (self.sendFirstTextFlag) {
self.sendFirstTextFlag = false;
self.sendFirstText();
}
} else {
self.sendFirstTextFlag = false;
clearInterval(self.sendFirstTextInterval);
self.sendFirstTextInterval = undefined;
self.leaveChat();
}
}, 2500);
console.log("start autoStart");
return "start autoStart";
}
if(this.autoStartInterval) {
//document.querySelector('#first-text').style.display = "none"
clearInterval(this.sendFirstTextInterval);
this.sendFirstTextInterval = undefined;
clearInterval(this.autoStartInterval);
this.autoStartInterval = undefined;
console.log("stop autoStart");
return "stop autoStart";
}
}
block() {
const self = this;
if (this.blockInterval === void 0) {
this.blockInterval = setInterval(() => {
let otherMsgsLen = document.querySelectorAll('.stranger.text').length;
let scanSize = document.querySelector('#block-scan-size').value;
let breakFlag = false;
if(!scanSize) { scanSize = 0; }
let otherTextObjs = document.querySelectorAll('.stranger.text');
let blockListLabels = document.querySelectorAll('.block-list');
if(self.isChat() && document.querySelector('.loadMoreButton').style.display == 'none') {
for(let blockListLabel of blockListLabels) {
let blockText = blockListLabel.querySelector('.block-text').value;
for(let i = 0; i < otherTextObjs.length && i < scanSize; i++) {
let otherText = self.extractOtherText(otherTextObjs[i]);
for(let radio of blockListLabel.querySelectorAll('.block-radio')) {
if(radio.checked) {
if(radio.value === "equivalent") {
if(otherText.includes(blockText)) {
self.leaveChat();
breakFlag = true;
}
}
if(radio.value === "inclusion") {
if(blockText === otherText) {
self.leaveChat();
breakFlag = true;
}
}
}
if(breakFlag) break;
}
if(breakFlag) break;
}
if(breakFlag) break;
}
}
}, 500);
console.log("start block");
return "start block";
}
if (this.blockInterval) {
clearInterval(this.blockInterval);
this.blockInterval = undefined;
console.log("stop block")
return "stop block";
}
}
getBotUrl(text) {
this.bot = 0;
if(this.bot == 0) {
return `https://api.ownthink.com/bot?appid=xiaosi&userid=user&spoken=${text}`;
} else {
return `https://api.qingyunke.com/api.php?key=free&appid=0&msg=${text}`;
}
}
parseBotResponse(res) {
let resJson;
let resText;
if(this.bot == 0) {
resJson = JSON.parse(res);
resText = resJson.data.info.text.replace('小思', '我');
} else {
resJson = JSON.parse(res);
resText = resJson.content.replace('菲菲', '我');
}
return [resJson, resText];
}
botReply(text, callback, args) {
if(text == '?') { text = '??'; }
const self = this;
const request = new XMLHttpRequest();
if(Math.floor(Math.random() * 4) == 0) {
this.bot = (this.bot == 0) ? 1 : 0;
}
let url = this.getBotUrl(text);
request.open('get', url, true);
request.send();
request.onreadystatechange = () => {
if(request.readyState === 4) {
let [resJson, resText] = self.parseBotResponse(request.response)
if(callback !== void 0) {
if(args !== void 0) {
callback(resText, ...args);
} else {
callback(resText);
}
}
}
}
}
translator(text, dest, callback, args) {
const self = this;
const request = new XMLHttpRequest();
request.open('get', `https://api.zhconvert.org/convert?converter=${dest}&text=${text}`, true);
request.send();
request.onreadystatechange = () => {
if(request.readyState === 4) {
const resJson = JSON.parse(request.response);
const resText = resJson.data.text;
if(callback !== void 0) {
if(args !== void 0) {
callback(resText, ...args);
} else {
callback(resText);
}
}
}
}
}
autoReply() {
const request = new XMLHttpRequest();
const url = "https://example.com"
const self = this;
if(self.autoReplyInterval === void 0) {
/* Test if CORS is allowed */
request.open('get', url, true);
request.send();
request.onreadystatechange = () => {
if(request.readyState === 4) {
if(request.status === 200) {
/* CORS is allowed */
const maxReplyQueue = 1;
self.updateRecord();
if (self.otherTexts.length) {
self.replyMid = self.otherTexts[self.otherTexts.length - 1].mid;
}
self.autoReplyInterval = setInterval(() => {
self.updateRecord();
const otherId = self.otherId;
const replyQuery = self.otherTexts.slice(self.otherTexts.length - maxReplyQueue, self.otherTexts.length);
for (let otherTextObj of replyQuery) {
const {mid, text} = otherTextObj;
if(mid > self.replyMid) {
self.replyMid = mid;
self.translator(
text, 'China',
(...args) => { self.botReply(...args); },
[
(...args) => { self.translator(...args) },
[
'Taiwan',
(...args) => { self.sendBotReply(otherId, ...args); }
]
]
);
}
}
}, 1000);
console.log("start autoReply");
}
else {
/* Cors is not allowed */
document.querySelector("#checkbox-autoReply").checked = false
clearInterval(this.autoReplyInterval);
self.autoReplyInterval = undefined;
console.log("stop autoReply")
alert("CORS is not allowed")
}
}
}
return;
}
if(this.autoReplyInterval) {
clearInterval(this.autoReplyInterval);
this.autoReplyInterval = undefined;
console.log("stop autoReply")
return "stop autoReply";
}
}
appendBlockList = () => {
const blockList = `
<br>
<i class="material-icons" onclick="document.querySelector('#block-list${this.blockListId}').remove()" style="cursor: pointer;">remove</i>
<input type="radio" class="block-radio" name="block-list${this.blockListId}-style" value="equivalent" style="-webkit-appearance: radio;" checked><label>包含</label>
<input type="radio" class="block-radio" name="block-list${this.blockListId}-style" value="inclusion" style="-webkit-appearance: radio;"><label>等於</label>
<input type="text" class="block-text" maxlength=500 placeholder="此訊息自動退出" style="width: 42%; border-style:ridge; border-width:1px; border-color: gray;">
`;
let label = document.createElement('label');
label.className = "block-list";
label.id = `block-list${this.blockListId}`;
label.style.fontSize = '6px';
label.innerHTML += blockList;
document.querySelector('#block-list-container').appendChild(label);
this.blockListId += 1;
}
appendAutostartCheckBox = () => {
const autostartCheckBox = `
<li>
<a href="#">
<label for="checkbox-autostart">
<i class="material-icons">sms</i>
<span>自動開始聊天</span>
</label>
<span class="right"> <input type="checkbox" class="ios8-switch" id="checkbox-autostart">
<label for="checkbox-autostart"></label>
</span>
<textarea id="first-text" rows=3 cols=500 maxlength="500" placeholder="自動送出第一則訊息" style="width: 100%;"></textarea>
</a>
</li>
`;
document.querySelector('.snap-drawer.snap-drawer-left ul:last-of-type').innerHTML += autostartCheckBox;
}
appendAutoReplyCheckBox = () => {
const autoReplyCheckBox = `
<li>
<a href="#">
<label for="checkbox-autoReply">
<i class="material-icons">mms</i>
<span>自動回復最後一則訊息</span>
</label>
<span class="right"> <input type="checkbox" class="ios8-switch" id="checkbox-autoReply">
<label for="checkbox-autoReply"></label>
</span>
</a>
</li>
`;
document.querySelector('.snap-drawer.snap-drawer-left ul:last-of-type').innerHTML += autoReplyCheckBox;
}
appendEmojiList = () => {
const emojilist = `
<li>
<div>
<a>快樂</a>
</div>
<div>
<a>(・ω´・ )</a><a>(・ω´・ )</a><a>(・ω´・ )</a>
</div>
</li>
`;
document.querySelector('.snap-drawer.snap-drawer-left ul:last-of-type').innerHTML += emojilist;
}
appendBlockCheckBox = () => {
const blockCheckBox = `
<li>
<a href="#">
<label for="checkbox-block">
<<i class="material-icons">block</i>
<span>當前 <input id="block-scan-size" type="text" maxlength=4 style="width: 14%; border-style:ridge; border-width:1px; border-color: gray"> 則訊息</span>
</label>
<span class="right"> <input type="checkbox" name="block-trigger" class="ios8-switch" id="checkbox-block">
<label for="checkbox-block"></label>
</span>
<div id="block-list-container">
<label for="add-block-list">
<i class="material-icons" id="add-block-list" style="cursor: pointer;">add</i>
</label>
<span style="color: grey;">新增封鎖語句</span>
</div>
</a>
</li>
`;
document.querySelector('.snap-drawer.snap-drawer-left ul:last-of-type').innerHTML += blockCheckBox;
}
}
let wooct = new wooControler()