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

100 lines
3.0 KiB
JavaScript
Raw Normal View History

2020-09-04 05:45:38 +03:00
var config_defaults = {
serverurl:"http://localhost:42010",
apikey:"BlackPinkInYourArea"
}
document.addEventListener("DOMContentLoaded",function() {
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);
document.getElementById("serverurl").addEventListener("input",saveConfig);
document.getElementById("apikey").addEventListener("input",saveConfig);
2019-04-07 15:07:50 +03:00
2019-04-07 18:14:50 +03:00
chrome.runtime.onMessage.addListener(onInternalMessage);
2020-09-04 05:45:38 +03:00
chrome.storage.local.get(config_defaults,function(result){
for (var key in result) {
document.getElementById(key).value = result[key];
}
checkServer();
})
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
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;
}
}
2020-09-04 05:45:38 +03:00
function saveConfig() {
for (var key in config_defaults) {
var value = document.getElementById(key).value;
chrome.storage.local.set({ [key]: value });
}
}
function checkServer() {
2019-05-12 11:20:47 +03:00
url = document.getElementById("serverurl").value + "/api/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) {
2020-09-01 16:22:57 +03:00
if ((this.status >= 200) && (this.status < 300)) {
//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"
}
}
}