-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoverlay.html
246 lines (233 loc) · 8.13 KB
/
overlay.html
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
<audio autoplay controls></audio>
<script type="module">
try{
const sentry=await import('https://cdn.jsdelivr.net/npm/@sentry/browser@8/+esm')
sentry.init({
dsn:'https://[email protected]/8354',
environmet:location.hostname,
release:"1.1.0"
})
}catch(e){
console.warn('automatic error reporting failed to load',e)
}
try{
if(window.location.search.includes('remotejs')){
const channel=new URLSearchParams(window.location.search).get('remotejs')
const script=document.createElement('script')
script.src='https://remotejs.com/agent/agent.js'
script.setAttribute("data-consolejs-channel",channel)
document.head.appendChild(script)
}
}catch(e){
console.warn('remotejs load failed',e)
}
</script>
<script>
const client_id='1gsnqnvtrguxysilfqp5gkb1snswmf'
const queue=[]
const audio=document.querySelector('audio')
let dracula=null;
let tba,polly,config,tokens,event_socket,socket_session,params,timer_id
window.onload=async function(){
tba=await import('https://tba.sugoidogo.com/tba.mjs')
polly=await import('https://tts.sugoidogo.com/twitch-polly.mjs')
tokens=await tba.get_tokens(client_id)
params=tba.get_url_params()
config=await fetch('https://ts.sugoidogo.com/'+params.redeem_id,{headers:tokens.auth_headers})
.then(response=>response.json())
console.log(config)
if(config.type=='dracula'){
dracula=(await import('https://sugoidogo.github.io/dracula_flow/dracula_flow.mjs')).default
}
connect_socket()
}
function connect_socket(){
timer_id=null
if(event_socket){
event_socket.close()
}
event_socket=new WebSocket('wss://eventsub.wss.twitch.tv/ws')
event_socket.onmessage=function(event){
const message=JSON.parse(event.data)
console.log(message.metadata.message_type)
switch (message.metadata.message_type) {
case "session_welcome":{
socket_session=message.payload.session
create_subscription()
start_timer(socket_session.keepalive_timeout_seconds)
break
}
case "session_keepalive":{
start_timer(socket_session.keepalive_timeout_seconds)
break
}
case "notification":{
const event=message.payload.event
get_redeems(event.id)
.then(data=>handle_events(data))
break
}
default:{
console.warn(message)
}
}
}
}
async function handle_events(events){
if(!events){
console.warn('handle_events skipped, no events provided')
return
}
console.debug(events)
for(const event of events){
console.debug(event)
if(event.status=='UNFULFILLED'){
if(config.type=='dracula'){
speak(await dracula(1),event.id)
}else if(config.preset || config.type=='preset'){
speak(config.presetText,event.id)
}else{
speak(event.user_input,event.id)
}
}
if(event.status=='CANCELED'){
cancel(event.id)
}
}
}
function start_timer(seconds){
if(timer_id){
clearTimeout(timer_id)
}
timer_id=setTimeout(connect_socket,seconds*2000)
}
function create_subscription(){
const headers={'content-type':'application/json'}
Object.assign(headers,tokens.auth_headers)
const events=[
{
"type": "channel.channel_points_custom_reward_redemption.add",
"version": "1",
"condition": {
"broadcaster_user_id": tokens.user_id,
"reward_id": params.reward_id
},
"transport": {
"method": "websocket",
"session_id":socket_session.id
}
},
{
"type": "channel.channel_points_custom_reward_redemption.update",
"version": "1",
"condition": {
"broadcaster_user_id": tokens.user_id,
"reward_id": params.reward_id
},
"transport": {
"method": "websocket",
"session_id":socket_session.id
}
}
]
for(const event of events){
const body=JSON.stringify(event)
fetch('https://api.twitch.tv/helix/eventsub/subscriptions',{
headers:headers,
method:'POST',
body:body
})
}
}
function speak(Text,id){
TextType='text'
Engine='standard'
Voice=config.voice
if(config.ssml){
TextType='ssml'
}
if(config.neural){
Engine='neural'
}
polly.SynthesizeSpeech(tokens.auth_headers,Text,Voice,TextType,Engine)
.then(response=>{
if(response.ok){
return response.blob()
}else{
throw response
}
})
.then(blob=>{
const url=URL.createObjectURL(blob)
queue.push({src:url,id:id})
if(queue.length==1){
audio.src=url
}
}).catch(error=>{
console.warn('refunding',error)
refund(id)
})
}
audio.onended=function(){
audio.src=null
queue.shift()
if(queue.length>0){
audio.src=queue[0].src
}
}
function cancel(reward_id){
if(queue.length>0 && queue[0].id==reward_id){
audio.onended()
}else{
for(const reward of queue){
if(reward.id==reward_id){
queue.splice(queue.indexOf(reward),1)
}
}
}
}
function refund(reward_id){
const url=new URL('https://api.twitch.tv/helix/channel_points/custom_rewards/redemptions')
url.search=new URLSearchParams({
id:reward_id,
broadcaster_id:tokens.user_id,
reward_id:params.redeem_id
})
const body=JSON.stringify({status:'CANCELED'})
const headers={'content-type':'application/json'}
Object.assign(headers,tokens.auth_headers)
fetch(url,{
method:'PATCH',
headers:headers,
body:body
})
}
let get_redeems_try_count=0
function get_redeems(id=null){
get_redeems_try_count++
const url=new URL('https://api.twitch.tv/helix/channel_points/custom_rewards/redemptions')
url.searchParams.append('broadcaster_id',tokens.user_id)
url.searchParams.append('reward_id',params.redeem_id)
url.searchParams.append('sort','OLDEST')
if(id){
url.searchParams.append('id',id)
}else{
url.searchParams.append('status','UNFULFILLED')
const cursor=localStorage.getItem('cursor')
if(cursor){
url.searchParams.append('after',cursor)
}
}
return fetch(url,{headers:tokens.auth_headers})
.then(response=>response.json())
.then(json=>{
if('pagination' in json){
localStorage.setItem('cursor',json.pagination.cursor)
}
if(((!json.data) || json.data.length===0) && get_redeems_try_count<3){
return get_redeems(id)
}
return json.data
})
}
</script>