-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
215 lines (198 loc) · 8.19 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
<style>
* {
font-size: 3vmin;
}
video {
max-width: 100vw;
max-height: 50vh;
}
</style>
<div id="status">Loading...</div>
<div id="permission">
<button onclick="allow()">Allow</button>
<button onclick="deny()">Deny</button>
</div>
<span id="connectionSettings" hidden>
<table>
<tr>
<td>Websocket URL:</td>
<td><input id="obsurl" type="url"></td>
</tr>
<tr>
<td>Websocket Password:</td>
<td><input id="obspassword" type="password"></td>
</tr>
</table>
<button id="connect" onclick="connect()">Connect</button>
<button id="useQR" onclick="useQrCode()">Use QR Code</button>
<br>
<span id="qr" hidden>
<video id="qrvideo" hidden></video>
<br>
<span>Make sure this device and the one running OBS are on the same network.</span>
<br>
<button onclick="screenCapture()">Screen Capture</button>
<span id="loadingCameras">Loading video device list...</span>
</span>
</span>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/obs-ws.min.js"
integrity="sha256-UbkntZrYTokZf8tldcF9nmOn5CNgcmDP+MNsUcKUpEE=" crossorigin="anonymous">
</script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/qr-scanner.umd.min.js"
integrity="sha256-jstRoCLjzwXwLN2o0DMkkwRHtqYCQMxrRkktAHwZekU=" crossorigin="anonymous">
</script>
<script>
const params = new URLSearchParams(window.location.search)
const redirect_uri = params.get('redirect_uri')
document.querySelector('#status').innerHTML = 'The website at:<br>' + redirect_uri + '<br>wants to connect to your OBS Websocket.<br>This will give them FULL CONTROL OF OBS.'
document.querySelector('#permission').hidden = false
const obs = new OBSWebSocket()
async function connect() {
document.querySelector('#status').innerHTML = 'Connecting to OBS...'
const url=document.querySelector('#obsurl').value
const password=document.querySelector('#obspassword').value
try {
await obs.connect(url,password)
} catch (error) {
const status=document.querySelector('#status')
if(status.innerHTML='Connecting to OBS...'){
status.innerHTML=error
}
return false
}
return true
}
let interval
const video = document.querySelector('video')
async function useQrCode() {
console.log('starting QR Code scanner')
interval = setInterval(qrScan, 1000)
const qr = document.querySelector('#qr')
qr.hidden = false
video.srcObject = await navigator.mediaDevices.getUserMedia({ video: { facingMode: "environment" } })
let mediaDevices = await navigator.mediaDevices.enumerateDevices()
mediaDevices = mediaDevices.filter(function (device) {
return device.kind == "videoinput"
})
for (const device of mediaDevices) {
const button = document.createElement('button')
button.innerHTML = device.label
button.onclick = function () { switchCamera(device) }
qr.appendChild(button)
}
document.querySelector('#loadingCameras').hidden = true
}
async function screenCapture() {
video.srcObject = null
const screenCaptureDevice = await navigator.mediaDevices.getDisplayMedia()
video.srcObject = screenCaptureDevice
video.play()
video.hidden = false
}
async function switchCamera(device) {
video.srcObject = null
const camera = await navigator.mediaDevices.getUserMedia({ video: { deviceId: { exact: device.deviceId } } })
video.srcObject = camera
video.play()
video.hidden = false
}
function qrScan() {
QrScanner.scanImage(video, { returnDetailedScanResult: true })
.then(onQrScan).catch(function (error) { console.warn(error) })
}
async function onQrScan(result) {
var data = result.data.split('/')
if (data.shift() != 'obsws:') {
console.log('found non-OBS QR Code')
return false
}
clearInterval(interval)
console.log('found OBS QR Code')
data.shift()
const fullurl = 'ws://' + data.shift()
const port = fullurl.split(':').pop()
const localurl = 'ws://localhost:' + port
const password = data.shift()
const obsurl = document.querySelector('#obsurl')
const obspassword = document.querySelector('#obspassword')
obspassword.value = password
obsurl.value = localurl
console.log('attempting localhost connection')
if (await connect()) {
return true
}
obsurl.value = fullurl
console.log('attempting local network connection')
if (await connect()) {
return true
}
return false
}
obs.addListener('ConnectionError', function onConnectionError(error){
document.querySelector('#status').innerHTML=error
})
obs.addListener('ConnectionClosed', function onConnectionClosed(error) {
switch (error.code) {
case 1000:
console.log('unknown error, trying again')
connect()
break
case 1006:
document.querySelector('#status').innerHTML = 'obs websocket not found.' +
'<br>Is <a href="https://github.com/obsproject/obs-websocket/releases/latest">obs-websocket</a> installed?' +
'<br>Is obs open? Check your websocket settings in OBS under the Tools menu.' +
'<br>Is the info below correct? Consider using the QR scanner.'
break
case 4009:
document.querySelector('#status').innerHTML = error.message.split(' ').slice(-3).join(' ').toLowerCase().replace('authentication', 'Password') +
'<br>You can view or change your websocket password in OBS under the Tools menu.'
break
default:
document.querySelector('#status').innerHTML = 'connection closed: Error ' + error.code + ': ' + error.message
break
}
})
obs.addListener('ConnectionOpened', function onConnectedOpened() {
console.log('connected to socket, saving url')
document.querySelector('#status').innerHTML = 'Connected'
localStorage.setItem('obsurl', document.querySelector('#obsurl').value)
})
obs.addListener('Identified', function onIdentified() {
console.log('authentication passed, saving password')
document.querySelector('#status').innerHTML = 'Password Passed'
localStorage.setItem('obspassword', document.querySelector('#obspassword').value)
//obs.disconnect()
redirect()
})
async function redirect() {
try {
document.querySelector('#status').innerHTML = 'Redirecting...'
const url = new URL(redirect_uri)
url.searchParams.append('obsurl', document.querySelector('#obsurl').value)
url.searchParams.append('obspassword', document.querySelector('#obspassword').value)
url.searchParams.append('userChoice', 'allow')
window.location.replace(url.href)
} catch (e) {
document.querySelector('#status').innerHTML = 'Error during redirect: '+e
console.error(e)
}
}
function allow() {
console.log('user allowed connection, attempting to use saved info')
document.querySelector('#permission').hidden = true
document.querySelector('#connectionSettings').hidden = false
document.querySelector('#obsurl').value = localStorage.getItem('obsurl') || 'ws://localhost:4455'
document.querySelector('#obspassword').value = localStorage.getItem('obspassword') || ''
connect()
}
function deny() {
console.log('user denied connection, returning to redirect_uri')
const url = new URL(redirect_uri)
url.searchParams.append('userChoice', 'deny')
window.location.replace(url.href)
}
async function bypass(){
const url=document.querySelector('#obsurl').value
await navigator.clipboard.writeText(url)
}
</script>