Add connection checks
This commit is contained in:
parent
7157587c1a
commit
44a0b5c464
25
popup.js
25
popup.js
@ -104,12 +104,29 @@ function switchConnectForm() {
|
|||||||
showConnect = !showConnect;
|
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() {
|
async function initConnect() {
|
||||||
const data = await chrome.storage.local.get(['puzzleScoreboard']);
|
const data = await chrome.storage.local.get(['puzzleScoreboard']);
|
||||||
if (data?.puzzleScoreboard) {
|
if (data?.puzzleScoreboard) {
|
||||||
puzzleScoreboard = data.puzzleScoreboard;
|
puzzleScoreboard = data.puzzleScoreboard;
|
||||||
|
const connection = await scoreboardConnect(puzzleScoreboard);
|
||||||
const connectButtonEl = document.getElementById('connect-button')
|
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')
|
const connectTextEl = document.getElementById('connect-text')
|
||||||
connectTextEl.style.display = 'none';
|
connectTextEl.style.display = 'none';
|
||||||
const connectSubmitButtonEl = document.getElementById('connect-submit-button')
|
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 connectButtonEl = document.getElementById('connect-button')
|
||||||
const connectTextEl = document.getElementById('connect-text')
|
const connectTextEl = document.getElementById('connect-text')
|
||||||
const connectSubmitButtonEl = document.getElementById('connect-submit-button')
|
const connectSubmitButtonEl = document.getElementById('connect-submit-button')
|
||||||
@ -130,10 +147,12 @@ function connectToPuzzleScoreboard() {
|
|||||||
} else {
|
} else {
|
||||||
puzzleScoreboard = connectTextEl.value;
|
puzzleScoreboard = connectTextEl.value;
|
||||||
chrome.storage.local.set({ puzzleScoreboard: puzzleScoreboard });
|
chrome.storage.local.set({ puzzleScoreboard: puzzleScoreboard });
|
||||||
|
const connection = await scoreboardConnect(puzzleScoreboard);
|
||||||
const connectFormEl = document.getElementById('connect-form')
|
const connectFormEl = document.getElementById('connect-form')
|
||||||
connectFormEl.style.display = 'none';
|
connectFormEl.style.display = 'none';
|
||||||
showConnect = false;
|
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';
|
connectTextEl.style.display = 'none';
|
||||||
connectSubmitButtonEl.innerHTML = 'disconnect';
|
connectSubmitButtonEl.innerHTML = 'disconnect';
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user