2022-04-01 01:40:39 +00:00
|
|
|
/*
|
|
|
|
Loaded into popup index.html
|
|
|
|
*/
|
|
|
|
|
|
|
|
let browserType = getBrowser();
|
|
|
|
|
|
|
|
// boilerplate to dedect browser type api
|
|
|
|
function getBrowser() {
|
|
|
|
if (typeof chrome !== "undefined") {
|
|
|
|
if (typeof browser !== "undefined") {
|
|
|
|
return browser;
|
|
|
|
} else {
|
|
|
|
return chrome;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
console.log("failed to dedect browser");
|
|
|
|
throw "browser detection error"
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-04-03 13:54:29 +00:00
|
|
|
|
2022-04-01 01:40:39 +00:00
|
|
|
// store access details
|
|
|
|
document.getElementById("save-login").addEventListener("click", function () {
|
2022-11-28 03:21:13 +00:00
|
|
|
let url = document.getElementById("full-url").value;
|
2022-11-27 23:17:11 +00:00
|
|
|
if (!url.includes('://')) {
|
|
|
|
url = 'http://' + url;
|
|
|
|
}
|
|
|
|
let parsed = new URL(url);
|
2022-04-01 01:40:39 +00:00
|
|
|
let toStore = {
|
|
|
|
"access": {
|
2022-11-28 04:01:06 +00:00
|
|
|
"url": `${parsed.protocol}//${parsed.hostname}`,
|
|
|
|
"port": parsed.port || (parsed.protocol === 'https' ? '443' : '80'),
|
2022-04-01 01:40:39 +00:00
|
|
|
"apiKey": document.getElementById("api-key").value
|
|
|
|
}
|
|
|
|
};
|
|
|
|
browserType.storage.local.set(toStore, function() {
|
|
|
|
console.log("Stored connection details: " + JSON.stringify(toStore));
|
2022-04-03 13:54:29 +00:00
|
|
|
pingBackend();
|
2022-04-01 01:40:39 +00:00
|
|
|
});
|
|
|
|
})
|
|
|
|
|
2022-04-01 08:30:49 +00:00
|
|
|
|
2022-04-03 13:54:29 +00:00
|
|
|
// verify connection status
|
|
|
|
document.getElementById("status-icon").addEventListener("click", function() {
|
|
|
|
pingBackend();
|
|
|
|
})
|
|
|
|
|
|
|
|
|
2022-06-20 10:35:57 +00:00
|
|
|
// send cookie
|
2022-06-25 13:15:19 +00:00
|
|
|
document.getElementById("sendCookies").addEventListener("click", function() {
|
2022-06-20 10:35:57 +00:00
|
|
|
sendCookie();
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
function sendCookie() {
|
|
|
|
console.log("popup send cookie");
|
|
|
|
|
|
|
|
function handleResponse(message) {
|
2022-06-25 13:15:19 +00:00
|
|
|
console.log("handle cookie response: " + JSON.stringify(message));
|
|
|
|
let cookie_validated = message.cookie_validated;
|
|
|
|
document.getElementById("sendCookiesStatus").innerText = "validated: " + cookie_validated
|
2022-06-20 10:35:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function handleError(error) {
|
|
|
|
console.log(`Error: ${error}`);
|
|
|
|
}
|
|
|
|
|
2022-06-25 13:15:19 +00:00
|
|
|
let checked = document.getElementById("sendCookies").checked;
|
|
|
|
let toStore = {
|
|
|
|
"sendCookies": {
|
|
|
|
"checked": checked
|
|
|
|
}
|
|
|
|
};
|
|
|
|
browserType.storage.local.set(toStore, function() {
|
|
|
|
console.log("stored option: " + JSON.stringify(toStore));
|
|
|
|
})
|
|
|
|
if (checked === false) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
let sending = browserType.runtime.sendMessage({"sendCookie": true});
|
2022-06-20 10:35:57 +00:00
|
|
|
sending.then(handleResponse, handleError);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-04-03 13:54:29 +00:00
|
|
|
// send ping message to TA backend
|
|
|
|
function pingBackend() {
|
|
|
|
|
|
|
|
function handleResponse(message) {
|
|
|
|
if (message.response === "pong") {
|
|
|
|
setStatusIcon(true);
|
|
|
|
console.log("connection validated")
|
|
|
|
}
|
|
|
|
}
|
2022-11-28 03:21:13 +00:00
|
|
|
|
2022-04-03 13:54:29 +00:00
|
|
|
function handleError(error) {
|
|
|
|
console.log(`Error: ${error}`);
|
|
|
|
setStatusIcon(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
console.log("ping TA server")
|
|
|
|
let sending = browserType.runtime.sendMessage({"verify": true});
|
|
|
|
sending.then(handleResponse, handleError);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2022-05-30 11:32:12 +00:00
|
|
|
// add url to image
|
|
|
|
function addUrl(access) {
|
|
|
|
const url = `${access.url}:${access.port}`;
|
|
|
|
document.getElementById("ta-url").setAttribute("href", url);
|
|
|
|
}
|
|
|
|
|
2022-04-03 13:54:29 +00:00
|
|
|
|
2022-06-25 13:15:19 +00:00
|
|
|
function setCookieState() {
|
|
|
|
|
|
|
|
function handleResponse(message) {
|
|
|
|
console.log(message);
|
|
|
|
document.getElementById("sendCookies").checked = message.cookie_enabled;
|
|
|
|
if (message.validated_str) {
|
|
|
|
document.getElementById("sendCookiesStatus").innerText = message.validated_str;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function handleError(error) {
|
|
|
|
console.log(`Error: ${error}`);
|
|
|
|
}
|
|
|
|
|
|
|
|
console.log("set cookie state");
|
|
|
|
let sending = browserType.runtime.sendMessage({"cookieState": true});
|
|
|
|
sending.then(handleResponse, handleError)
|
|
|
|
document.getElementById("sendCookies").checked = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-04-03 13:54:29 +00:00
|
|
|
// change status icon based on connection status
|
2022-04-01 08:30:49 +00:00
|
|
|
function setStatusIcon(connected) {
|
|
|
|
|
|
|
|
let statusIcon = document.getElementById("status-icon")
|
|
|
|
if (connected == true) {
|
2022-04-03 13:54:29 +00:00
|
|
|
statusIcon.innerHTML = "☑";
|
|
|
|
statusIcon.style.color = "green";
|
2022-04-01 08:30:49 +00:00
|
|
|
} else {
|
|
|
|
statusIcon.innerHTML = "☒";
|
|
|
|
statusIcon.style.color = "red";
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-04-01 01:40:39 +00:00
|
|
|
// fill in form
|
|
|
|
document.addEventListener("DOMContentLoaded", async () => {
|
|
|
|
|
|
|
|
function onGot(item) {
|
|
|
|
if (!item.access) {
|
|
|
|
console.log("no access details found");
|
2022-04-01 08:30:49 +00:00
|
|
|
setStatusIcon(false);
|
2022-04-01 01:40:39 +00:00
|
|
|
return
|
|
|
|
}
|
2022-11-27 23:17:11 +00:00
|
|
|
let { url, port } = item.access;
|
|
|
|
let fullUrl = url;
|
|
|
|
if (!(url.startsWith('http://') && port === '80')) {
|
|
|
|
fullUrl += `:${port}`;
|
|
|
|
}
|
|
|
|
document.getElementById("full-url").value = fullUrl;
|
2022-04-01 01:40:39 +00:00
|
|
|
document.getElementById("api-key").value = item.access.apiKey;
|
2022-04-03 13:54:29 +00:00
|
|
|
pingBackend();
|
2022-05-30 11:32:12 +00:00
|
|
|
addUrl(item.access);
|
2022-04-01 01:40:39 +00:00
|
|
|
};
|
2022-06-25 13:15:19 +00:00
|
|
|
|
|
|
|
function setCookiesOptions(result) {
|
|
|
|
if (!result.sendCookies || result.sendCookies.checked === false) {
|
|
|
|
console.log("sync cookies not set");
|
|
|
|
return
|
|
|
|
}
|
|
|
|
console.log("set options: " + JSON.stringify(result));
|
|
|
|
setCookieState();
|
|
|
|
|
|
|
|
}
|
2022-11-28 03:21:13 +00:00
|
|
|
|
2022-04-01 01:40:39 +00:00
|
|
|
function onError(error) {
|
|
|
|
console.log(`Error: ${error}`);
|
|
|
|
};
|
|
|
|
|
|
|
|
browserType.storage.local.get("access", function(result) {
|
|
|
|
onGot(result)
|
|
|
|
});
|
|
|
|
|
2022-06-25 13:15:19 +00:00
|
|
|
browserType.storage.local.get("sendCookies", function(result) {
|
|
|
|
setCookiesOptions(result)
|
|
|
|
})
|
|
|
|
|
2022-04-01 01:40:39 +00:00
|
|
|
})
|