-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEXT-Bard.js
222 lines (206 loc) · 6.97 KB
/
EXT-Bard.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
/**
** Module : EXT-Bard
** @bugsounet
** 07-2023
** support: https://forum.bugsounet.fr
**/
Module.register("EXT-Bard", {
defaults: {
debug: false,
COOKIE_KEY: null,
scrollActivate: true,
scrollStep: 25,
scrollInterval: 1000,
scrollStart: 10000
},
start: function () {
this.ready = false
this.session = {}
this.shown = false
},
getDom: function() {
var dom = document.createElement("div")
dom.style.display = 'none'
return dom
},
notificationReceived: function(noti, payload,sender) {
switch(noti) {
case "GW_READY":
if (sender.name == "Gateway") {
this.preparePopup()
this.sendNotification("EXT_HELLO", this.name)
this.sendSocketNotification("INIT", this.config)
}
break
case "EXT_BARD-SHOW":
if (!this.ready || this.shown) return
this.bardShow()
break
case "EXT_STOP":
case "EXT_BARD-HIDE":
if (!this.shown) return
this.bardHide()
break
case "EXT_BARD-QUERY":
if (payload && this.ready && this.shown) this.sendSocketNotification("QUERY", payload)
break
}
},
socketNotificationReceived: function(noti, payload) {
switch(noti) {
case "INITIALIZED":
this.ready = true
break
case "REPLY":
var replyStatus = document.getElementById("EXT_BARD-Status")
replyStatus.src = "modules/EXT-Bard/resources/standby.gif"
var reply = document.getElementById("EXT_BARD-Webview")
reply.src= "modules/EXT-Bard/tmp/output/tmp.html?seed="+Date.now()
break
case "THINK":
var thinkStatus = document.getElementById("EXT_BARD-Status")
thinkStatus.src = "modules/EXT-Bard/resources/think.gif"
var Text = document.getElementById("EXT_BARD-Text")
Text.textContent = payload
break
case "ERROR":
this.sendNotification("EXT_ALERT", {
message: "[BARD] " + payload,
type: "error"
})
break
case "TB_RESULT":
this.tbResult(payload)
break
}
},
preparePopup() {
var Bard = document.createElement("div")
Bard.id = "EXT_BARD"
Bard.className= "animate__animated"
Bard.style.setProperty('--animate-duration', '1s')
Bard.classList.add("hidden")
var Container = document.createElement("div")
Container.id = "EXT_BARD-Container"
Bard.appendChild(Container)
var TopBar = document.createElement("div")
TopBar.id = "EXT_BARD-TopBar"
Container.appendChild(TopBar)
var Status = document.createElement("img")
Status.id = "EXT_BARD-Status"
Status.src = "modules/EXT-Bard/resources/standby.gif"
TopBar.appendChild(Status)
var Text = document.createElement("div")
Text.id = "EXT_BARD-Text"
Text.textContent = "EXT-Bard Ready!"
TopBar.appendChild(Text)
Content = document.createElement("div")
Content.id = "EXT_BARD-Content"
Container.appendChild(Content)
var Box = document.createElement("div")
Box.id = "EXT_BARD-Box"
Content.appendChild(Box)
var Webview = document.createElement("webview")
Webview.id = "EXT_BARD-Webview"
Webview.scrolling="no"
Webview.src= "modules/EXT-Bard/tmp/output/tmp.html?seed="+Date.now()
Webview.addEventListener("did-stop-loading", (event) => {
event.stopPropagation()
if (Webview.getURL() == "about:blank") return
Webview.executeJavaScript(`
var timer = null
var timerScroll = null
function scrollDown(posY){
clearTimeout(timer)
timer = null
var scrollHeight = document.body.scrollHeight
if (posY == 0) console.log("Begin Scrolling")
if (posY > scrollHeight) posY = scrollHeight
document.documentElement.scrollTop = document.body.scrollTop = posY;
if (posY == scrollHeight) {
clearTimeout(timer)
timer = null
clearTimeout(timerScroll)
timerScroll = null
console.log("End Scrolling")
return
}
timer = setTimeout(function(){
if (posY < scrollHeight) {
posY = posY + ${this.config.scrollStep}
scrollDown(posY);
}
}, ${this.config.scrollInterval});
};
if (${this.config.scrollActivate}) {
timerScroll = setTimeout(() => scrollDown(0), ${this.config.scrollStart});
};`)
})
Box.appendChild(Webview)
document.body.appendChild(Bard)
},
getStyles: function () {
return [ "EXT-Bard.css" ]
},
bardShow() {
var bard = document.getElementById("EXT_BARD")
MM.getModules().enumerate((module) => {
module.hide(200, () => {}, {lockString: "EXT-BARD_LOCKED"})
})
bard.classList.remove("hidden")
bard.classList.remove("animate__backOutRight")
bard.style.animationFillMode = "inherit"
bard.classList.add("animate__backInLeft")
bard.style.display= "block"
this.shown = true
this.sendNotification("EXT_BARD-CONNECTED")
},
bardHide() {
var bard = document.getElementById("EXT_BARD")
bard.classList.remove("animate__backInLeft")
bard.style.animationFillMode = "both"
bard.classList.add("animate__backOutRight")
bard.addEventListener('animationend', (e) => {
if (e.animationName == "backOutRight") {
MM.getModules().enumerate((module)=> {
module.show(200, () => {}, {lockString: "EXT-BARD_LOCKED"})
})
bard.classList.add("hidden")
this.shown = false
this.sendNotification("EXT_BARD-DISCONNECTED")
}
e.stopPropagation()
}, {once: true})
},
/********************************/
/*** EXT-TelegramBot Commands ***/
/********************************/
EXT_TELBOTCommands: function(commander) {
commander.add({
command: "bard",
description: "Make a conversation with Google Bard",
callback: "tbBard"
})
},
tbBard: function(command, handler) {
if (!this.ready) return handler.reply("TEXT", "Not available actually.")
let key = Date.now()
this.session[key] = handler
if (handler.args) {
handler.reply("TEXT", "Query Bard for " + handler.args + "...")
this.sendSocketNotification("TB_QUERY", {TBkey:key, Query: handler.args})
} else {
handler.reply("TEXT", "Ask your Query for Google Bard")
}
},
tbResult: function(result) {
if (result.TBKey && result.Result && this.session[result.TBKey]) {
var handler = this.session[result.TBKey]
handler.reply("TEXT", TelegramBotExtraChars(result.Result), {parse_mode:"Markdown"})
this.session[result.TBKey] = null
delete this.session[result.TBKey]
} else {
this.sendNotification("EXT_TELBOT-TELL_ADMIN", "Bard data received error")
}
}
})