/
hu60bot.js
698 lines (622 loc) · 25.8 KB
/
hu60bot.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
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
window.hu60_res_bot_icon = '/q.php/api.webplug-file/22780_public_hu_icon.svg'
window.hu60_res_back_icon = '/q.php/api.webplug-file/22780_public_return_icon.svg'
window.hu60_res_loading_icon = '/q.php/api.webplug-file/22780_public_loading.svg'
window.hu60_res_exit_chat_icon = '/q.php/api.webplug-file/22780_public_exit_window.svg'
window.hu60_res_clear_icon = '/q.php/api.webplug-file/22780_public_cancel_icon.svg'
window.hu60_res_clear_all_icon = '/q.php/api.webplug-file/22780_public_qingkong_icon.svg'
window.hu60_res_source_link_icon = '/q.php/api.webplug-file/22780_public_source_link_icon.svg'
window.hu60_res_default_avatar = '/upload/default.jpg'
window.hu60_res_robot_icon = 'https://file.hu60.cn/avatar/-50.jpg'
window.hu60_site_file_url = 'https://file.hu60.cn'
window.hu60_hu60bot_uid = -54
window.hu60_hu60bot_welcome = '您好,有什么我可以为您效劳的吗?'
function startPlugin() {
initCurrentUserInfo()
initHu60botChat()
initGlobalListener()
connectWs()
showPluginDoor()
smallScreenDeviceSafeInit()
largeScreenDeviceSafeInit()
checkUnreadMsgs()
}
function checkUnreadMsgs() {
let formData = new FormData()
formData.append('data', '{"type":1}')
fetch('/q.php/api.msg.isread.get.json', {
body: formData,
method: "post"
}).then(res => res.json()).then(jres => {
console.log(JSON.stringify(jres))
if(Object.keys(jres.result).length == 0) {
document.querySelectorAll('#chatList li').forEach(chatItem => {
let newMsgTips = chatItem.querySelector('.newMsgTips')
newMsgTips.innerText = '0'
newMsgTips.style.display = 'none'
})
let hu60botChatList = JSON.parse(window.localStorage.getItem("hu60bot_chat_list.json"))
for(let i =0;i<hu60botChatList.length;i++) {
hu60botChatList[i].tipsCount = 0
}
window.localStorage.setItem("hu60bot_chat_list.json", JSON.stringify(hu60botChatList))
}
})
}
function initCurrentUserInfo() {
let uid = window.localStorage.getItem('uid')
if(uid == null) {
fetch('/q.php/user.index.json').then(r => r.json()).then(j => {
window.localStorage.setItem('uid', j.uid)
window.hu60_uid = j.uid
})
} else {
window.hu60_uid = uid
}
}
function initHu60botChat() {
let hu60botChatBaseHTML =
`<div id="chatList">
<div class="hltitle">
<span class="hu60botwsstatus" title="disconnected"></span>
<img src="${window.hu60_res_exit_chat_icon}" class="hu60botminwindow" title="minimize"/>
<img src="${window.hu60_res_clear_all_icon}" class="hu60botclearall hu60botmenuicon" title="clear all conversation" />
</div>
<ul></ul>
</div>
<div id="chatWindow"></div>`
let hu60botChatRoot = document.createElement('div')
hu60botChatRoot.id = 'hu60botChat'
hu60botChatRoot.innerHTML = hu60botChatBaseHTML
document.body.appendChild(hu60botChatRoot)
document.querySelector('#hu60botChat .hu60botminwindow')
.addEventListener('click', e => hu60botWindowOp(false))
document.querySelector('#hu60botChat .hu60botclearall')
.addEventListener('click', e => {
JSON.parse(window.localStorage.getItem('hu60bot_chat_list.json')).forEach(chat => window.localStorage.removeItem(`${chat.uid}convo.json`))
window.localStorage.removeItem('hu60bot_chat_list.json')
initChatWindowData()
})
initChatWindowData()
}
function initChatWindowData() {
renderChatList()
document.querySelector(`#chat-${window.hu60_hu60bot_uid}`)
.click()
}
function renderChatList() {
document.querySelector("#chatList ul").innerHTML = ''
let hu60bot_chat_list = window.localStorage.getItem("hu60bot_chat_list.json")
if (hu60bot_chat_list == null) {
let hu60botChat = {
uid: window.hu60_hu60bot_uid,
name: "hu60bot",
avatar: window.hu60_res_robot_icon,
isRobot: true,
tipsCount: 0
}
window.localStorage.setItem("hu60bot_chat_list.json", JSON.stringify([hu60botChat]))
appendChatList(hu60botChat, {updateStorage: false})
} else {
let hu60botChatList = JSON.parse(hu60bot_chat_list)
hu60botChatList.forEach(chat => {
if(chat.uid == window.hu60_hu60bot_uid) {
appendChatList(chat, {updateStorage: false,focused: window.hu60_chatwindow == chat.uid})
}
})
hu60botChatList.forEach(chat => {
if(chat.uid == window.hu60_hu60bot_uid) {
return
}
appendChatList(chat, {updateStorage: false,focused: window.hu60_chatwindow == chat.uid})
})
}
if(window.innerWidth < 1080) {
document.querySelectorAll('#chatList li')
.forEach(chatItem => chatItem.addEventListener('click', e=>{
document.querySelector('#chatWindow').style.display = 'block'
document.querySelector('#chatList').style.display = 'none'
}))
}
}
function appendChatList(chat, opts={updateStorage: true, focused: false}) {
// UI
const chatItem = document.createElement('li')
chatItem.innerHTML =
`<span class="newMsgTips">${chat.tipsCount}</span>
<img class="cavatar" src="${chat.avatar}" />
${chat.name}<br />
<span class="latestMsgOverview">${chat.overview?chat.overview:""}</span>
<img class="clearChat" src="${window.hu60_res_clear_icon}" />`
chatItem.id = `chat-${chat.uid}`
chatItem.addEventListener('mouseover', e => chatItem.querySelector('.clearChat').style.display = 'block')
chatItem.addEventListener('mouseout', e => chatItem.querySelector('.clearChat').style.display = 'none')
chatItem.addEventListener('click', e => {
document.querySelectorAll('#chatList li')
.forEach( item => item.classList.remove('activeChat'))
chatItem.classList.add("activeChat")
initChatWindow(chat)
let newMsgTips = chatItem.querySelector('.newMsgTips')
newMsgTips.innerText = '0'
newMsgTips.style.cssText = 'display: none'
let hu60botChatList = JSON.parse(window.localStorage.getItem("hu60bot_chat_list.json"))
for(let i =0;i<hu60botChatList.length;i++) {
let chat = hu60botChatList[i]
if(chat.uid == window.hu60_chatwindow) {
chat.tipsCount = 0
break
}
}
window.localStorage.setItem("hu60bot_chat_list.json", JSON.stringify(hu60botChatList))
})
chatItem.querySelector('.clearChat').addEventListener('click', e=> {
if(chat.isRobot) {
window.localStorage.removeItem(`${chat.uid}convo.json`)
document.querySelector('#chatContainer').innerHTML = ''
appendChatText(window.hu60_hu60bot_welcome, chat.uid,
{self: false, updateStorage: true})
if(chat.uid == window.hu60_hu60bot_uid) {
window.hu60_ws.send(JSON.stringify({action: "rmconvo"}))
}
e.stopPropagation()
return
}
let prevUid = window.hu60_chatwindow
let hu60botChatList = JSON.parse(window.localStorage.getItem('hu60bot_chat_list.json'))
for(let i =0;i<hu60botChatList.length;i++) {
if(hu60botChatList[i].uid == chat.uid) {
let elementToMove = hu60botChatList.splice(i, 1)
window.localStorage.setItem('hu60bot_chat_list.json', JSON.stringify(hu60botChatList))
break
}
}
renderChatList()
e.stopPropagation()
})
if (opts.focused) {
document.querySelectorAll('#chatList li')
.forEach( item => item.classList.remove('activeChat'))
chatItem.classList.add('activeChat')
}
if (chat.tipsCount > 0) {
chatItem.querySelector('.newMsgTips').style.cssText = 'display: block'
}
document.querySelector("#chatList ul").appendChild(chatItem)
// Update data
if(opts.updateStorage) {
let hu60bot_chat_list = window.localStorage.getItem("hu60bot_chat_list.json")
let hu60botChatList = null
if (hu60bot_chat_list == null) {
hu60botChatList = []
} else {
hu60botChatList = JSON.parse(hu60bot_chat_list)
}
if(hu60botChatList[0].uid == chat.uid) {
return
}
hu60botChatList.unshift(chat)
window.localStorage.setItem("hu60bot_chat_list.json", JSON.stringify(hu60botChatList))
}
}
function initChatWindow(chat) {
if(window.hu60_chatwindow) {
// update UI
window.hu60_chatwindow = chat.uid
window.hu60_chatwindow_obj = chat
document.querySelector("#ctitle .chatName").innerText = chat.name
document.querySelector('#chatContainer').innerHTML = ''
let convo = window.localStorage.getItem(`${chat.uid}convo.json`)
if (convo == null) {
if (chat.isRobot) {
appendChatText(window.hu60_hu60bot_welcome, chat.uid,
{self: false, updateStorage: true})
}
} else {
JSON.parse(convo).forEach(c => {
appendChatText(c.words, chat.uid,
{self: c.self, updateStorage: false,msgid: c.msgid})
})
}
return
}
// create UI
window.hu60_chatwindow = chat.uid
window.hu60_chatwindow_obj = chat
let chatWindow = document.querySelector("#chatWindow")
const chatReturnBtn = document.createElement('img')
chatReturnBtn.id = 'chatReturnBtn'
chatReturnBtn.src = window.hu60_res_back_icon
chatReturnBtn.addEventListener('click', e => {
document.querySelector('#chatWindow').style.display = 'none'
document.querySelector('#chatList').style.display = 'block'
})
chatWindow.appendChild(chatReturnBtn)
const chatTitile = document.createElement("div")
chatTitile.id = 'ctitle'
chatTitile.innerHTML = `<span class="chatName">${chat.name}</span>`
chatWindow.appendChild(chatTitile)
const chatContainer = document.createElement("ul")
chatContainer.id = 'chatContainer'
chatWindow.appendChild(chatContainer)
const chatInput = document.createElement("div")
chatInput.id = 'chatInput'
const _chatInputSend = document.createElement("button")
_chatInputSend.innerText="发送"
_chatInputSend.style.display = 'none'
_chatInputSend.id = 'chatInputSend'
_chatInputSend.addEventListener("click", e => {
let text = document.querySelector("#chatInput textarea")
text.focus()
if (text.value == "") {
text.style.cssText = "border: 1px solid red"
setTimeout(()=>{text.style.cssText = "border: 0.5px solid #ddd"},200)
setTimeout(()=>{text.style.cssText = "border: 1px solid red"},300)
setTimeout(()=>{text.style.cssText = "border: 0.5px solid #ddd"},500)
setTimeout(()=>{text.style.cssText = "border: 1px solid red"},600)
setTimeout(()=>{text.style.cssText = "border: 0.5px solid #ddd"},800)
return
}
handleMsgSend(text.value)
appendChatText(text.value, window.hu60_chatwindow, {self: true, updateStorage: true})
text.value = ''
_chatInputSend.style.display = 'none'
})
const _chatInput = document.createElement("textarea")
_chatInput.placeholder="请提问(Ctrl + Enter 快捷发送)"
_chatInput.addEventListener("input", e=>{_chatInputSend.style.display = 'block'})
chatInput.appendChild(_chatInput)
chatInput.appendChild(_chatInputSend)
chatWindow.appendChild(chatInput)
// render conversation
let convo = window.localStorage.getItem(`${chat.uid}convo.json`)
if (convo == null) {
if (chat.isRobot) {
appendChatText(window.hu60_hu60bot_welcome, chat.uid,
{self: false, updateStorage: true})
}
} else {
JSON.parse(convo).forEach(c =>
appendChatText(c.words, chat.uid,
{self: c.self, updateStorage: false,msgid:c.msgid}))
}
}
function appendChatText(words, uid, opts={self: false,updateStorage: true}) {
if (opts.updateStorage) {
// update convo.json and chat_list.json (for order the chat list)
let convo = window.localStorage.getItem(`${uid}convo.json`)
let convoObj = JSON.parse(convo)
if (convo == null) {
convoObj = []
}
let isRepeat = false
if(convoObj.length > 0) {
let lastConvo = convoObj.slice(-1)[0]
if(lastConvo.words == words && lastConvo.self == opts.self) {
isRepeat = true
}
}
if(!isRepeat) {
convoObj.push({words: words,self: opts.self, msgid: opts.msgid})
window.localStorage.setItem(`${uid}convo.json`, JSON.stringify(convoObj))
}
document.querySelector(`#chat-${uid} .latestMsgOverview`).innerText = words
if(window.hu60_chatwindow != uid) {
let newMsgTips = document.querySelector(`#chat-${uid} .newMsgTips`)
let newTipsCount = parseInt(newMsgTips.innerText)+1
newMsgTips.innerText = newTipsCount
if(newTipsCount>0) {
newMsgTips.style.display = 'block'
}
}
let hu60bot_chat_list = window.localStorage.getItem('hu60bot_chat_list.json')
if (hu60bot_chat_list != null) {
let hu60botChatList = JSON.parse(hu60bot_chat_list)
let hit = null
for(let i =0 ;i < hu60botChatList.length;i++) {
let chat = hu60botChatList[i]
if (chat.uid == uid ) {
hit = i
chat.overview = words
if(window.hu60_chatwindow != uid) {
chat.tipsCount =
parseInt(document.querySelector(`#chat-${uid} .newMsgTips`).innerText)
}
hu60botChatList[i] = chat
break
}
}
let elementToMove = hu60botChatList.splice(hit, 1)
hu60botChatList.unshift(elementToMove[0])
window.localStorage.setItem('hu60bot_chat_list.json', JSON.stringify(hu60botChatList))
}
renderChatList()
}
if(window.hu60_chatwindow != uid) {
return
}
// create UI (text, avatar and loading icon)
const chatItem = document.createElement("li")
chatItem.classList.add('chat')
let chatContainer = document.querySelector('#chatContainer')
chatContainer.appendChild(chatItem)
const avatar = document.createElement("img")
avatar.classList.add('cavatar')
avatar.src = window.hu60_chatwindow_obj.avatar
const text = document.createElement("div")
text.classList.add('hu60bot')
let optComp = ""
if(opts.msgid) {
optComp = `<a class="source-link" href="/q.php/link.ack.msg.${opts.msgid}.html" title="click me">
<img src="${window.hu60_res_source_link_icon}" /></a>`
}
text.innerHTML = `${words}${optComp}`
chatItem.appendChild(avatar)
chatItem.appendChild(text)
if (opts.self) {
avatar.classList.add('crightpos')
text.classList.add('crightpos')
text.classList.add('cuser')
text.innerText = words
avatar.src = `${window.hu60_site_file_url}/avatar/${window.hu60_uid}.jpg`
avatar.addEventListener('error', (e) => avatar.src = window.hu60_res_default_avatar)
if(opts.updateStorage) {
const icon = document.createElement("img")
icon.classList.add('send_status_icon')
icon.classList.add('crightpos')
icon.src = window.hu60_res_loading_icon
chatItem.appendChild(icon)
}
} else {
if(opts.updateStorage) {
document.querySelectorAll('.send_status_icon')
.forEach(icon => icon.style.display = 'none')
}
}
chatContainer.scrollTop = chatContainer.scrollHeight
}
function hu60botWindowOp(open) {
if(open) {
document.querySelector('header').style.display = 'none'
document.querySelector('.container').style.display = 'none'
document.querySelector('footer').style.display = 'none'
document.querySelector('#hu60botChat').style.display = 'flex'
let chatContainer = document.querySelector('#chatContainer')
chatContainer.scrollTop = chatContainer.scrollHeight
return
}
document.querySelector('header').style.display= 'block'
document.querySelector('.container').style.display= 'block'
document.querySelector('footer').style.display= 'block'
document.querySelector('#hu60botChat').style.display = 'none'
}
function initGlobalListener() {
document.addEventListener("keydown", function(event){
if(event.ctrlKey && event.code === 'Enter') {
document.querySelector('#chatInput button').click()
}
})
setInterval(()=>{
const tips = document.querySelectorAll('#chatList .newMsgTips')
for(let i =0;i<tips.length;i++) {
let tipsCount = parseInt(tips[i].innerText)
if(tipsCount>0) {
if(window.hu60_new_tips_task) {
return
}
window.hu60_new_tips_task = setInterval(()=>{
const hu60botPlugin = document.querySelector('#hu60botPlugin')
if(hu60botPlugin.style.opacity == 0) {
hu60botPlugin.style.opacity = 1
} else {
hu60botPlugin.style.opacity = 0
}
},500)
return
}
}
clearInterval(window.hu60_new_tips_task)
document.querySelector('#hu60botPlugin').style.opacity = 1
window.hu60_new_tips_task = null
},100)
}
function connectWs() {
let protocol = window.location.protocol === 'https:' ? 'wss' : 'ws'
let ws = new WebSocket(`${protocol}://${location.host}/ws/msg`)
window.hu60_ws = ws
ws.addEventListener("message", (event) => handleWsMsg(JSON.parse(event.data)))
ws.addEventListener("error", e => window.hu60_ws.close())
ws.addEventListener("open", e => {
window._hu60bot_hb_task = setInterval(() => ws.send('{"action":"ping"}'), 60000)
let wsStatus = document.querySelector('#chatList .hu60botwsstatus')
wsStatus.style.cssText = 'background: green'
wsStatus.title = 'connected'
})
ws.addEventListener("close", e => {
clearInterval(window._hu60bot_hb_task)
let wsStatus = document.querySelector('#chatList .hu60botwsstatus')
wsStatus.style.cssText = 'background: red'
wsStatus.title = 'WS_CLOSED'
setTimeout(() => window.hu60_ws = connectWs(), 5000)
})
}
function showPluginDoor() {
let floatPluginMenu = document.querySelector('#floatPluginMenu')
if (floatPluginMenu == null) {
floatPluginMenu = document.createElement("div")
floatPluginMenu.id = 'floatPluginMenu'
document.querySelector('body').appendChild(floatPluginMenu)
}
let hu60botPlugin = document.createElement("div")
hu60botPlugin.id = 'hu60botPlugin'
hu60botPlugin.innerHTML = `<img src="${window.hu60_res_bot_icon}" />`
hu60botPlugin.addEventListener('click', e => {
let hu60botChat = document.querySelector('#hu60botChat')
if (hu60botChat.style.display == 'none' || hu60botChat.style.display == "") {
hu60botWindowOp(true)
} else {
hu60botWindowOp(false)
}
})
floatPluginMenu.appendChild(hu60botPlugin)
}
async function handleMsgSend(words) {
let currentChatWindowUID = window.hu60_chatwindow
if (!currentChatWindowUID) {
alert("SYS_ERR")
return
}
if(currentChatWindowUID == window.hu60_hu60bot_uid) {
try {
window.hu60_ws.send(JSON.stringify({action: "chat", data: words, id: "chat"}))
} catch(e) {
log.debug(JSON.stringify(e))
appendChatText("WS_SEND_ERR: " + JSON.stringify(e), window.hu60_chatwindow,
{self: false,updateStorage: false})
try{
if (window.hu60_ws.readyState === WebSocket.CLOSED) {
setTimeout(() =>
window.hu60_ws.send(JSON.stringify({action: "chat", data: text.value})), 6000)
}
} catch(_){}
}
return
}
// 发送聊天室消息
let chatroom = await (await fetch('/q.php/addin.chat.hu60bot.json')).json()
let formData = new FormData()
formData.append('content', '<!md>\n@#' + currentChatWindowUID + ' ' + words)
formData.append('token', chatroom.token)
formData.append('go', '1')
fetch('/q.php/addin.chat.hu60bot.json', {
body: formData,
method: "post",
redirect: "manual" // 不自动重定向
}).then(res => res.json()).then(jres => {
console.log(JSON.stringify(jres))
if(jres.success) {
document.querySelectorAll('.send_status_icon').forEach(icon => icon.style.display='none')
}
})
}
function handleWsMsg(msg) {
if (msg.event == 'chat') {
appendChatText(`${msg.data.newConversation?"[新会话] ":""}${setext(msg.data.response)}`,
window.hu60_hu60bot_uid,
{self: false, updateStorage: true})
return
}
if(msg.event == 'ack') {
if(msg.data == 'chat') {
document.querySelectorAll('.send_status_icon').forEach(icon => icon.style.animationDuration = '0.5s')
}
return
}
if (msg.event == 'msg' && msg.data.type == 1) {
let msgContent = JSON.parse(msg.data.content)
let msgContentText = getHu60MsgText(msgContent[0].msg)
if (document.querySelector(`#chat-${msg.data.byuid}`) == null) {
if (!window.hu60_user_info_map) {
window.hu60_user_info_map = {}
}
if (!window.hu60_user_info_map[msg.data.byuid]) {
fetch(`/q.php/user.info.${msg.data.byuid}.json`)
.then(res => res.json()).then(jres => {
window.hu60_user_info_map[msg.data.byuid] = jres
appendChatList({
uid: msg.data.byuid,
name: jres.name,
avatar: `${window.hu60_site_file_url}/avatar/${msg.data.byuid}.jpg`,
isRobot: msg.data.byuid<0,
overview: msgContentText,
tipsCount: 0
}, {updateStorage: true})
appendChatText(msgContentText, msg.data.byuid,
{self: false, updateStorage: true, msgid: msg.data.id})
})
return
}
let userInfo = window.hu60_user_info_map[msg.data.byuid]
appendChatList({
uid: msg.data.byuid,
name: userInfo.name,
avatar: `${window.hu60_site_file_url}/avatar/${msg.data.byuid}.jpg`,
isRobot: msg.data.byuid<0,
overview: msgContentText,
tipsCount: 0
}, {updateStorage: true})
}
appendChatText(msgContentText, msg.data.byuid,
{self: false, updateStorage: true, msgid: msg.data.id})
return
}
console.debug('unsupported message: ', msg)
}
function smallScreenDeviceSafeInit() {
if (window.innerWidth <= 1080) {
let hu60botChat = document.querySelector('#hu60botChat')
let chatList = document.querySelector('#chatList')
let chatContainer = document.querySelector('#chatContainer')
let chatReturnBtn = document.querySelector('#chatReturnBtn')
let winHeight = window.innerHeight
hu60botChat.style.cssText = `height: ${winHeight}px`
chatContainer.style.cssText = `height: ${(winHeight - 130)}px`
chatList.style.display = 'none'
chatReturnBtn.style.display = 'block'
document.querySelectorAll('#chatList li')
.forEach(chatItem => chatItem.addEventListener('click', e=>{
document.querySelector('#chatWindow').style.display = 'block'
document.querySelector('#chatList').style.display = 'none'
}))
}
}
function largeScreenDeviceSafeInit() {
if (window.innerWidth > 1080) {
let hu60botChat = document.querySelector('#hu60botChat')
if(window.innerHeight<=720) {
hu60botChat.style.bottom = '0px'
} else {
hu60botChat.style.bottom = `${(window.innerHeight-720)/2}px`
}
}
}
startPlugin()
// -----
// utils
// -----
function getHu60MsgText(msgContent) {
const validUnits = ["text","imgzh","mdpre","mdcode","face","at"]
let text = msgContent.filter(unit => validUnits.includes(unit.type) ).map(unit => {
if(unit.type == "text") {
return setext(unit.value)
}
if(unit.type == "mdpre") {
return unit.data
}
if(unit.type == "face") {
return `{${unit.face}}`
}
if(unit.type == "mdcode") {
return unit.quote + unit.lang + unit.data + unit.quote
}
if(unit.type == "at") {
return `@${unit.tag}(${unit.uid}) `
}
if(unit.type == "imgzh") {
return `<img style="max-width: 90%;display: block" src="${unit.src}" alt="${unit.alt}" />`
}
}).join("").trim()
for(let i=0;i<3;i++) {
if(text.startsWith('<br />')) {
text = text.substr(6)
}
}
for(let i =0;i<5;i++) {
if (text.startsWith(',') || text.startsWith(',') || text.startsWith('\n')) {
text = text.substr(1)
}
}
console.debug(text)
return text
}
function setext(unsestr) {
return unsestr.replaceAll('<', '<').replaceAll('>','>').replaceAll(' ', ' ').replaceAll('\n', '<br />')
}