Add connection checks

This commit is contained in:
sto 2025-06-21 18:35:33 +02:00
parent 7157587c1a
commit 44a0b5c464

View File

@ -104,12 +104,29 @@ function switchConnectForm() {
showConnect = !showConnect;
}
async function scoreboardConnect(puzzleScoreboard) {
if (puzzleScoreboard.includes('/message')) {
const resp = await fetch(puzzleScoreboard.replace('/message', '/connect'), {
method: "post",
headers: {
'Content-Type': 'application/json',
},
});
if (resp.status != 200) return {connected: false, error: 'Invalid contest token'};
const json = await resp.json();
return {connected: true, name: json?.name};
}
return {connected: false, error: 'Invalid connection link'};
}
async function initConnect() {
const data = await chrome.storage.local.get(['puzzleScoreboard']);
if (data?.puzzleScoreboard) {
puzzleScoreboard = data.puzzleScoreboard;
const connection = await scoreboardConnect(puzzleScoreboard);
const connectButtonEl = document.getElementById('connect-button')
connectButtonEl.innerHTML = `Connected: ${puzzleScoreboard.split('/message')?.at(0)}`;
if (connection.connected) connectButtonEl.innerHTML = `Connected: <span style="color: green">${connection.name}</span>`;
else connectButtonEl.innerHTML = `<span style="color: red">${connection.error}</span>`;
const connectTextEl = document.getElementById('connect-text')
connectTextEl.style.display = 'none';
const connectSubmitButtonEl = document.getElementById('connect-submit-button')
@ -117,7 +134,7 @@ async function initConnect() {
}
}
function connectToPuzzleScoreboard() {
async function connectToPuzzleScoreboard() {
const connectButtonEl = document.getElementById('connect-button')
const connectTextEl = document.getElementById('connect-text')
const connectSubmitButtonEl = document.getElementById('connect-submit-button')
@ -130,10 +147,12 @@ function connectToPuzzleScoreboard() {
} else {
puzzleScoreboard = connectTextEl.value;
chrome.storage.local.set({ puzzleScoreboard: puzzleScoreboard });
const connection = await scoreboardConnect(puzzleScoreboard);
const connectFormEl = document.getElementById('connect-form')
connectFormEl.style.display = 'none';
showConnect = false;
connectButtonEl.innerHTML = `Connected: ${puzzleScoreboard.split('/message')?.at(0)}`;
if (connection.connected) connectButtonEl.innerHTML = `Connected: <span style="color: green">${connection.name}</span>`;
else connectButtonEl.innerHTML = `<span style="color: red">${connection.error}</span>`;
connectTextEl.style.display = 'none';
connectSubmitButtonEl.innerHTML = 'disconnect';
}