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 () {
|
|
|
|
let toStore = {
|
|
|
|
"access": {
|
|
|
|
"url": document.getElementById("url").value,
|
|
|
|
"port": document.getElementById("port").value,
|
|
|
|
"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();
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
// send ping message to TA backend
|
|
|
|
function pingBackend() {
|
|
|
|
|
|
|
|
function handleResponse(message) {
|
|
|
|
if (message.response === "pong") {
|
|
|
|
setStatusIcon(true);
|
|
|
|
console.log("connection validated")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 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-03 13:54:29 +00:00
|
|
|
// download link
|
|
|
|
document.getElementById("download").addEventListener("click", function () {
|
|
|
|
|
|
|
|
let button = document.getElementById("downloadButton");
|
|
|
|
let payload = {
|
|
|
|
"download": {
|
|
|
|
"url": button.getAttribute("data-id")
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
function handleResponse(message) {
|
|
|
|
console.log("popup.js response: " + JSON.stringify(message));
|
2022-04-03 14:42:58 +00:00
|
|
|
browserType.storage.local.remove("youtube").then(response => {
|
|
|
|
let download = document.getElementById("download");
|
|
|
|
download.innerHTML = ""
|
|
|
|
let message = document.createElement("p");
|
|
|
|
message.innerText = "Link sent to Tube Archivist"
|
|
|
|
download.appendChild(message)
|
|
|
|
download.appendChild(document.createElement("hr"));
|
|
|
|
})
|
2022-04-03 13:54:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function handleError(error) {
|
|
|
|
console.log(`Error: ${error}`);
|
|
|
|
}
|
|
|
|
|
|
|
|
let sending = browserType.runtime.sendMessage(payload);
|
|
|
|
sending.then(handleResponse, handleError)
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
document.getElementById("url").value = item.access.url;
|
|
|
|
document.getElementById("port").value = item.access.port;
|
|
|
|
document.getElementById("api-key").value = item.access.apiKey;
|
2022-04-03 13:54:29 +00:00
|
|
|
pingBackend();
|
2022-04-01 01:40:39 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
function onError(error) {
|
|
|
|
console.log(`Error: ${error}`);
|
|
|
|
};
|
|
|
|
|
|
|
|
browserType.storage.local.get("access", function(result) {
|
|
|
|
onGot(result)
|
|
|
|
});
|
|
|
|
|
|
|
|
browserType.storage.local.get("youtube", function(result) {
|
2022-04-03 15:38:25 +00:00
|
|
|
if (result.youtube) {
|
|
|
|
downlodButton(result);
|
|
|
|
}
|
2022-04-01 01:40:39 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
function downlodButton(result) {
|
2022-04-03 13:54:29 +00:00
|
|
|
|
2022-04-01 01:40:39 +00:00
|
|
|
let download = document.getElementById("download");
|
|
|
|
let title = document.createElement("p");
|
|
|
|
title.innerText = result.youtube.title;
|
|
|
|
|
|
|
|
let button = document.createElement("button");
|
|
|
|
button.innerText = "download";
|
|
|
|
button.id = "downloadButton";
|
|
|
|
button.setAttribute("data-id", result.youtube.url);
|
|
|
|
|
|
|
|
download.appendChild(title);
|
|
|
|
download.appendChild(button);
|
|
|
|
download.appendChild(document.createElement("hr"));
|
2022-04-03 13:54:29 +00:00
|
|
|
|
2022-04-01 01:40:39 +00:00
|
|
|
}
|