-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
74 lines (68 loc) · 2.16 KB
/
index.html
File metadata and controls
74 lines (68 loc) · 2.16 KB
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
<head>
<title>HAL-9000</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="icon" href="/image" type="image/x-icon">
</head>
<style>
body{
background-color: black;
font-family:'Courier New', Courier, monospace;
overflow: hidden;
}
div {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
img{
display: block;
margin-left: auto;
margin-right: auto;
width: 50%;
}
</style>
<body>
<div>
<img style="width: 50%" src="/image"><br>
<p id="p" style="font-size: xx-small;color: red;width: 100%;text-align: center;">Click HAL to ask it something. <button onclick="document.getElementById('p').innerHTML ='';localStorage.setItem('hide_tip','true')" style="font-size: xx-small;">OK</button></p>
</div>
</body>
<script>
const params = new URLSearchParams(window.location.search)
const isText = params.has('text')
const alertAnswer = params.has('alert')
document.querySelector('img').addEventListener('click', async () => {
if(isText){
const inp = prompt("Ask HAL something")
let answer = ""
if(!inp){
answer = "I'm sorry, Dave. I'm afraid I can't do that."
}else{
const res = await fetch(`/ask?q=${inp}`, { method:"POST" })
answer = (await res.json()).answer
}
alertAnswer ? alert(answer) : speechSynthesis.speak(new SpeechSynthesisUtterance(answer))
}else{
if(!window.webkitSpeechRecognition && !window.SpeechRecognition) return alert("Your browser does not support speech recognition. Add ?text to the url to use text input.")
const recognition = new webkitSpeechRecognition() || new SpeechRecognition();
recognition.lang = navigator.language || navigator.userLanguage;
recognition.start();
setTimeout(() => {
regcognition.stop()
}, 10000);
recognition.onresult = async (event) => {
const inp = event.results[0][0].transcript
let answer = ""
if(!inp){
answer = "I'm sorry, Dave. I'm afraid I can't do that."
}else{
const res = await fetch(`/ask?q=${inp}`, { method:"POST" })
answer = (await res.json()).answer
}
alertAnswer ? alert(answer) : speechSynthesis.speak(new SpeechSynthesisUtterance(answer))
}
}
})
if(localStorage.getItem('hide_tip') === 'true' && !params.has('tip')) document.getElementById('p').innerHTML = ''
</script>