1
0
mirror of https://github.com/krateng/maloja.git synced 2023-08-10 21:12:55 +03:00
maloja/scrobblers/chromium-generic/settings.js

114 lines
3.4 KiB
JavaScript
Raw Normal View History

document.addEventListener("DOMContentLoaded",function() {
document.getElementById("serverurl").addEventListener("input",updateServer);
document.getElementById("apikey").addEventListener("input",updateAPIKey);
2019-04-07 15:07:50 +03:00
document.getElementById("serverurl").addEventListener("change",checkServer);
document.getElementById("apikey").addEventListener("change",checkServer);
2019-04-07 15:07:50 +03:00
document.getElementById("serverurl").addEventListener("focusout",checkServer);
document.getElementById("apikey").addEventListener("focusout",checkServer);
2019-04-07 18:14:50 +03:00
chrome.runtime.onMessage.addListener(onInternalMessage);
chrome.storage.local.get({"serverurl":"http://localhost:42010"},function(result) {
document.getElementById("serverurl").value = result["serverurl"]
2019-04-07 18:14:50 +03:00
checkServerMaybe()
});
chrome.storage.local.get({"apikey":"BlackPinkInYourArea"},function(result) {
document.getElementById("apikey").value = result["apikey"]
2019-04-07 18:14:50 +03:00
checkServerMaybe()
});
2019-04-07 15:07:50 +03:00
2019-04-07 18:14:50 +03:00
chrome.runtime.sendMessage({"type":"query"})
2019-04-07 15:07:50 +03:00
});
2019-04-07 18:14:50 +03:00
//this makes sure only the second call actually makes a request (the first request is pointless
//when the other element isn't filled yet and might actually overwrite the correct result because
//of a race condition)
var done = 0
function checkServerMaybe() {
done++;
if (done == 2) {
checkServer()
}
}
function onInternalMessage(request,sender) {
if (request.type == "response") {
players = request.content
html = "";
for (var i=0;i<players.length;i++) {
if (players[i][1]) {
html += "<li>" + players[i][0] + ": " + players[i][1] + " - " + players[i][2]
}
else {
html += "<li>" + players[i][0] + ": Playing nothing"
}
}
document.getElementById("playinglist").innerHTML = html;
}
}
function updateServer() {
2019-04-07 15:07:50 +03:00
text = document.getElementById("serverurl").value
2019-04-07 15:07:50 +03:00
chrome.storage.local.set({"serverurl":text})
}
function updateAPIKey() {
text = document.getElementById("apikey").value
chrome.storage.local.set({"apikey":text})
}
function checkServer() {
url = document.getElementById("serverurl").value + "/db/test?key=" + document.getElementById("apikey").value
2019-04-07 15:07:50 +03:00
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = createCheckmarks;
try {
xhttp.open("GET",url,true);
xhttp.send();
}
catch (e) {
//document.getElementById("checkmark_url").innerHTML = "❌"
//document.getElementById("checkmark_key").innerHTML = "❌"
document.getElementById("serverurl").style.backgroundColor = "red"
document.getElementById("apikey").style.backgroundColor = "red"
}
2019-04-07 15:07:50 +03:00
}
function createCheckmarks() {
if (this.readyState == 4) {
if ((this.status == 204) || (this.status == 205)) {
//document.getElementById("checkmark_url").innerHTML = "✔️"
//document.getElementById("checkmark_key").innerHTML = "✔️"
document.getElementById("serverurl").style.backgroundColor = "lawngreen"
document.getElementById("apikey").style.backgroundColor = "lawngreen"
}
else if (this.status == 403) {
//document.getElementById("checkmark_url").innerHTML = "✔️"
//document.getElementById("checkmark_key").innerHTML = "❌"
document.getElementById("serverurl").style.backgroundColor = "lawngreen"
document.getElementById("apikey").style.backgroundColor = "red"
}
else {
//document.getElementById("checkmark_url").innerHTML = "❌"
//document.getElementById("checkmark_key").innerHTML = "❌"
document.getElementById("serverurl").style.backgroundColor = "red"
document.getElementById("apikey").style.backgroundColor = "red"
}
}
}