diff --git a/popup.js b/popup.js
index 7e1d540..91f8681 100644
--- a/popup.js
+++ b/popup.js
@@ -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: ${connection.name}`;
+ else connectButtonEl.innerHTML = `${connection.error}`;
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: ${connection.name}`;
+ else connectButtonEl.innerHTML = `${connection.error}`;
connectTextEl.style.display = 'none';
connectSubmitButtonEl.innerHTML = 'disconnect';
}