58 lines
1.9 KiB
JavaScript
58 lines
1.9 KiB
JavaScript
'use strict';
|
|
(function () {
|
|
const $ = id => document.getElementById(id);
|
|
let adminUrl = '';
|
|
function currentOriginUrl(value) {
|
|
const target = new URL(value, location.origin);
|
|
return new URL(target.pathname + target.hash, location.origin).href;
|
|
}
|
|
function setLinks(admin) {
|
|
adminUrl = currentOriginUrl(admin);
|
|
const anchor = $('adminUrl');
|
|
anchor.href = adminUrl;
|
|
anchor.textContent = adminUrl;
|
|
$('createResult').hidden = false;
|
|
}
|
|
async function copy(value, button) {
|
|
try {
|
|
await navigator.clipboard.writeText(value);
|
|
const original = button.textContent;
|
|
button.textContent = 'Copied';
|
|
setTimeout(() => { button.textContent = original; }, 1500);
|
|
} catch (_) {
|
|
$('createError').textContent = 'Select the link and copy it.';
|
|
}
|
|
}
|
|
$('createForm').onsubmit = async event => {
|
|
event.preventDefault();
|
|
$('createError').textContent = '';
|
|
$('createButton').disabled = true;
|
|
$('createResult').hidden = true;
|
|
try {
|
|
const response = await fetch('/api/rooms', {
|
|
method: 'POST',
|
|
headers: {'Content-Type': 'application/json'},
|
|
credentials: 'same-origin',
|
|
body: JSON.stringify({label: $('label').value.trim()}),
|
|
signal: AbortSignal.timeout(15000)
|
|
});
|
|
const data = await response.json().catch(() => ({}));
|
|
if (!response.ok) {
|
|
$('createError').textContent = data.detail || (response.status === 429
|
|
? 'Too many rooms were created from this connection. Try again later.'
|
|
: 'Could not create an admin room.');
|
|
return;
|
|
}
|
|
setLinks(data.adminUrl);
|
|
$('openRoom').href = adminUrl;
|
|
$('openRoom').textContent = 'Open Caption Admin Room';
|
|
$('createResult').scrollIntoView({block: 'nearest'});
|
|
} catch (_) {
|
|
$('createError').textContent = 'The caption service could not be reached. Try again.';
|
|
} finally {
|
|
$('createButton').disabled = false;
|
|
}
|
|
};
|
|
$('copyAdmin').onclick = () => copy(adminUrl, $('copyAdmin'));
|
|
})();
|