-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
317 lines (307 loc) · 10.6 KB
/
index.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
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
<title>SugoiTTS</title>
<h1>SugoiTTS</h1>
<div id="status">Loading...</div>
<br>
<div id="content" hidden>
<form name="new">
<label>
Redeem Name:
<input type="text" name="name" required>
<button>Create Redeem</button>
</label>
</form>
<form name="config">
<div id="redeems"></div>
<button>save changes</button>
</form>
</div>
<a id="prompt" href="https://www.twitch.tv/subs/sugoidogo">Subscribe to unlock SSML and Neural Voices</a>
<template id="redeem">
<form class="redeem">
Redeem Name:
<span id="title"></span>
<br>
<label>
Voice:
<input type="search" list="voices" name="voice" required>
</label>
<br>
<label>
<input type="radio" name="type" value="preset">
Preset Text:
</label>
<textarea name="presetText"></textarea>
<button type="button" onclick="testText(this.parentElement)">Test</button>
<br>
<label>
<input type="radio" name="type" value="dracula">
Dracula Flow
</label>
<label>
<input type="radio" name="type" value="input" checked>
Viewer input
</label>
<br>
<label>
<input type="checkbox" name="ssml" disabled>
Use SSML
</label>
<br>
<label>
<input type="checkbox" name="neural" disabled>
Use Neural Voice
</label>
<br>
<a target="_blank"></a>
<br>
<button type="button" id="delete" onclick="remove(this.parentElement)">delete</button>
<br>
</form>
</template>
<datalist id="voices"></datalist>
<datalist id="languages"></datalist>
<style>
.redeem {
border-left: 1px solid black;
padding-left: 10px;
}
textarea {
vertical-align: top;
height: 1.5em;
width: 20em;
}
</style>
<script>
import('https://cdn.jsdelivr.net/npm/@sentry/browser@8/+esm').then(sentry=>{
sentry.init({
dsn:'https://[email protected]/8354',
environment:location.hostname,
release:"1.1.0",
integrations: [
sentry.feedbackIntegration({colorScheme:"light"})
]
})
}).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)
}
const client_id='1gsnqnvtrguxysilfqp5gkb1snswmf'
const scope='channel:manage:redemptions channel:read:redemptions'
const status_div=document.querySelector('div#status')
const new_form=document.querySelector('form[name=new]')
const redeem_template=document.querySelector('template#redeem').content.firstElementChild
const redeem_list=document.querySelector('div#redeems')
const voice_ids=[]
const ts_url='https://ts.sugoidogo.com/'
const tba_url='https://tba.sugoidogo.com/'
const twitch_polly_url='https://tts.sugoidogo.com/'
let tokens
function add_redeem(id,title,config){
const redeem_form=redeem_template.cloneNode(true)
redeem_form.name=id
redeem_form.querySelector('span#title').innerHTML=title
const a=redeem_form.querySelector('a')
let url=(location.origin+location.pathname).split('/')
url.pop()
url.push('overlay.html')
url=url.join('/')
url=new URL(url)
url.search=new URLSearchParams({
'refresh_token':tokens.refresh_token,
'redeem_id':id
})
a.href=url.href
a.innerHTML=url.href
for(const name in config){
if(name=='preset'){
redeem_form['type'].value='preset'
continue
}
redeem_form[name].value=config[name]
if(redeem_form[name].type=='checkbox'){
redeem_form[name].checked=name in config
}
}
redeem_list.appendChild(redeem_form)
}
function save(){
for(const form of redeem_list.children){
config=Object.fromEntries(new FormData(form))
id=form.name
fetch(ts_url+id,{
headers:tokens.auth_headers,
method:'POST',
body:JSON.stringify(config)
}).then(response=>{
if(!response.ok){
response.text().then(text=>{
status_div.innerHTML="Couldn't save settings<br>"+text
})
}
}).then(function(){
const url=new URL('https://api.twitch.tv/helix/channel_points/custom_rewards')
url.searchParams.append('broadcaster_id',tokens.user_id)
url.searchParams.append('id',id)
const body=JSON.stringify({'is_user_input_required':config.type=='preset'})
const headers={'Content-Type':'application/json'}
Object.assign(headers,tokens.auth_headers)
fetch(url,{headers:headers,method:'PATCH',body:body})
})
}
}
function remove(redeem_form){
const id=redeem_form.name
redeem_form.remove()
fetch(ts_url+id,{
headers:tokens.auth_headers,
method:'DELETE'
})
const url=new URL('https://api.twitch.tv/helix/channel_points/custom_rewards')
url.search=new URLSearchParams({
broadcaster_id:tokens.user_id,
id:id
})
fetch(url,{
headers:tokens.auth_headers,
method:'DELETE'
}).then(response=>{
if(!response.ok){
response.text().then(text=>{
status_div.innerHTML="Couldn't delete redeem<br>"+text
})
}
})
}
new_form.onsubmit=function(event){
event.preventDefault()
const redeem_name=event.target.name.value
const url=new URL('https://api.twitch.tv/helix/channel_points/custom_rewards')
url.searchParams.append('broadcaster_id',tokens.user_id)
const headers={'Content-Type':'application/json'}
Object.assign(headers,tokens.auth_headers)
body=JSON.stringify({
title:redeem_name,
cost:1,
is_enabled:false,
is_user_input_required:true
})
fetch(url,{
headers:headers,
method:'POST',
body:body
})
.then(response=>response.json())
.then(json=>{
const redeem=json['data'][0]
add_redeem(redeem.id,redeem.title,{voice:'Brian'})
event.target.name.value=''
save()
}).catch(error=>{
status_div.innerHTML='Something went wrong, check the console for more details'
console.log(error)
})
}
document.querySelector('form[name=config]').onsubmit=function(event){
event.preventDefault()
save()
}
import(tba_url+'tba.mjs')
.then(tba=>{
status_div.innerHTML='Signing in to Twitch...'
return tba.get_tokens(client_id,scope)
}).then(response=>{
tokens=response
//if(parseInt(tokens.tier)>=1000){
redeem_template.querySelector('[name=ssml]').disabled=false
//}
//if(parseInt(tokens.tier)>=2000){
redeem_template.querySelector('[name=neural]').disabled=false
document.querySelector('#prompt').hidden=true
//}
})
.then(()=>import(twitch_polly_url+'twitch-polly.mjs'))
.then(polly=>{
status_div.innerHTML='Loading voices...'
return polly.DescribeVoices(auth_headers=tokens.auth_headers)
}).then(voices=>{
const voice_list=document.querySelector('datalist#voices')
for(voice of voices){
const option=document.createElement('option')
option.value=voice.Id
option.innerHTML=voice.LanguageName+' '+voice.Gender+'\r\n'+voice.SupportedEngines
voice_list.appendChild(option)
voice_ids.push(voice.Id)
}
status_div.innerHTML='Loading redeems...'
const url=new URL('https://api.twitch.tv/helix/channel_points/custom_rewards')
url.search=new URLSearchParams({
broadcaster_id:tokens.user_id,
only_manageable_rewards:true
})
return fetch(url,{headers:auth_headers})
}).then(response=>response.json())
.then(json=>{
status_div.innerHTML='Loading settings...'
promises=[]
redeems=json['data']
for(const redeem of redeems){
const id=redeem.id
const title=redeem.title
const promise=fetch(ts_url+id,{headers:tokens.auth_headers})
.then(response=>response.json())
.then(config=>add_redeem(id,title,config))
.catch(error=>{
console.warn(error)
add_redeem(id,title,{voice:'Brian'})
})
promises.push(promise)
}
document.querySelector("div#content").hidden=false
return Promise.allSettled(promises)
}).then(()=>{
status_div.innerHTML='Ready!'
}).catch((error)=>{
status_div.innerHTML='Something went wrong during loading, check the console for more details'
console.error(error)
})
async function testText(form){
const polly=await import('https://tts.sugoidogo.com/twitch-polly.mjs')
const config=Object.fromEntries(new FormData(form))
console.log(config)
let TextType='text'
let Engine='standard'
let Voice=config.voice
if(config.ssml){
TextType='ssml'
}
if(config.neural){
Engine='neural'
}
polly.SynthesizeSpeech(tokens.auth_headers,config.presetText,Voice,TextType,Engine)
.then(async response=>{
if(response.ok){
return response.blob()
}else{
throw await response.text()
}
})
.then(blob=>{
const url=URL.createObjectURL(blob)
const audio=document.createElement('audio')
audio.src=url
audio.autoplay=true
document.body.appendChild(audio)
}).catch(error=>{
window.alert(error)
console.error(error)
})
}
</script>