Ground work for invidious instance selection (#11)

* Ground work for invidious instance selection

* Display logged in instance

* Show instances

* Reorder instances

* Instance management

* Add todos

* Changelog

* Add instances link, trim trailing slash

* Small tweaks
This commit is contained in:
Brahim Hadriche authored and GitHub committed 2022-11-24 18:40:27 -05:00
1 parent 1d9dc3ecda
commit a248318c54
14 files changed
+411 -130

No files matched your search

+162 -10
View File
@@ -5,13 +5,26 @@
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Playlet</title>
<style>
table,
th,
td {
border: 1px solid;
cursor: all-scroll;
}
table {
-webkit-user-select: none;
-ms-user-select: none;
user-select: none;
}
</style>
</head>
<body ondrop="OnDrop(event);" ondragover="OnDragover(event);">
<body ondrop="OnBodyDrop(event);" ondragover="OnBodyDragover(event);">
<h2>Playlet - Youtube Player for Roku</h2>
<h4 id="app-version">Version: </h4>
<h4>Invidious Instance: <a id="host" target="_blank">None</a></h4>
<h4>Invidious Instance: <a id="instance" target="_blank">None</a></h4>
<button onclick="PlayVideo()">Play Video</button>
<label for="video-id">Video ID:</label>
<input type="text" id="video-id" name="video-id" style="min-width:300px;">
@@ -20,6 +33,31 @@
<button style="display: none" id="login-button" onclick="Login()">Login</button>
<button style="display: none" id="logout-button" onclick="Logout()">Logout</button>
<br />
<table id="instances">
<thead>
<tr>
<th>Instances - find more at <a href="https://api.invidious.io/" target="_blank">api.invidious.io</a></th>
</tr>
</thead>
<tbody>
</tbody>
<tbody>
<tr id="instance-input-row">
<td>
<input type="text" id="instance-input" name="new-instance" oninput="OnNewInstanceTextChanged()"
style="min-width:300px;">
</td>
<td>
<button id="instance-add" disabled="true" onclick="AddInstance()">Add</button>
</td>
</tr>
</tbody>
</table>
</br>
<button style="display: none" id="update-instances" onclick="UpdateInstances()">Update Instances</button>
<h5>Instructions</h5>
<ul>
<li>Make sure you are logged in the Invidious instance before logging in on this page</li>
@@ -124,6 +162,66 @@ ${JSON.stringify(window.appState.device, null, 2)}
return urlSearchParams.get('v')
}
function OnNewInstanceTextChanged() {
document.getElementById("instance-add").disabled = !IsValidHttpUrl(document.getElementById("instance-input").value)
}
function AddInstance() {
instance = document.getElementById("instance-input").value
AddInstanceRow(instance)
document.getElementById("instance-input").value = ""
document.getElementById("update-instances").style.display = "block";
}
function AddInstanceRow(instance) {
instances = document.getElementById("instances").getElementsByTagName('tbody')[0];
var row = instances.insertRow();
row.draggable = true
row.addEventListener('dragstart', OnTableRowDragStart);
row.addEventListener('dragover', OnTableRowDragover);
row.addEventListener('drop', OnTableRowDrop);
var cell = row.insertCell();
style = instance === window.appState.invidious.selected_instance ? `style="font-weight:bold"` : ""
cell.innerHTML = `<a href="${instance}" ${style} target="_blank">${instance}</a>`
cell = row.insertCell();
cell.innerHTML = `<button onclick="RemoveInstance('${instance}')">Remove</button>`
}
function RemoveInstance(instance) {
instanceRows = Array.from(document.getElementById("instances")
.getElementsByTagName('tbody')[0]
.getElementsByTagName('tr'));
for (const i in instanceRows) {
currentInstance = instanceRows[i].getElementsByTagName('a')[0].innerText
if (currentInstance === instance) {
instanceRows[i].remove()
document.getElementById("update-instances").style.display = "block";
return;
}
}
}
function UpdateInstances() {
instances = Array.from(document.getElementById("instances")
.getElementsByTagName('tbody')[0]
.getElementsByTagName('td'))
.map(el => el.getElementsByTagName('a')[0])
.filter(a => !!a)
.map(a => a.innerText);
args = instances.map(instance => `instances=${instance}`).join("&")
fetch(`/invidious/set-instances?${args}`)
.then(function (response) {
alert("Instances updated. Restart Playlet to reflect changes.")
window.location.reload();
})
.catch(function (err) {
alert(err)
});
}
function Initialize() {
fetch("/api/state")
.then(function (response) {
@@ -134,11 +232,24 @@ ${JSON.stringify(window.appState.device, null, 2)}
console.log(appState)
document.getElementById("app-version").innerText = `Version: ${appState.app.version}`
document.getElementById("logged-in").innerText = `Logged In: ${appState.invidious.logged_in}`
document.getElementById("host").innerText = appState.invidious.host
document.getElementById("host").href = appState.invidious.host
document.getElementById("login-button").style.display = appState.invidious.logged_in ? "none" : "block";
if (appState.invidious.logged_in) {
document.getElementById("logged-in").innerHTML = `Logged In: true (<a href="${appState.invidious.logged_in_instance}" target="_blank">${appState.invidious.logged_in_instance}</a>)`
} else {
document.getElementById("logged-in").innerHTML = "Logged In: false"
}
instanceEl = document.getElementById("instance")
instanceEl.innerText = instanceEl.href = appState.invidious.selected_instance
for (var i in appState.invidious.instances) {
AddInstanceRow(appState.invidious.instances[i])
}
loginButton = document.getElementById("login-button")
loginButton.innerText = `Login (${appState.invidious.selected_instance})`
loginButton.style.display = appState.invidious.logged_in ? "none" : "block";
document.getElementById("logout-button").style.display = !appState.invidious.logged_in ? "none" : "block";
document.getElementById("feedback-url").href = CreateGithubIssueUrl()
@@ -148,12 +259,53 @@ ${JSON.stringify(window.appState.device, null, 2)}
});
}
function OnDrop(event) {
var tableRow;
function OnTableRowDragStart(event) {
target = event.target;
while (target && target.tagName !== 'TR') {
target = target.parentNode
}
if (target.id === "instance-input-row") {
return
}
tableRow = target
}
function OnTableRowDragover(event) {
event.preventDefault();
if (!tableRow) {
return
}
target = event.target;
while (target && target.tagName !== 'TR') {
target = target.parentNode
}
if (!target || target.id === "instance-input-row") {
return
}
let children = Array.from(target.parentNode.children);
if (children.indexOf(target) > children.indexOf(tableRow))
target.after(tableRow);
else
target.before(tableRow);
}
function OnTableRowDrop(event) {
event.preventDefault();
tableRow = undefined
document.getElementById("update-instances").style.display = "block";
}
function OnBodyDrop(event) {
for (var i in event.dataTransfer.items) {
let item = event.dataTransfer.items[i]
if (item.kind = 'string' && item.getAsString) {
item.getAsString((dataString) => {
if (IsValidHttpUrl(dataString)) {
if (IsValidHttpUrl(dataString) && GetVideoId(dataString)) {
document.getElementById('video-id').value = dataString
}
});
@@ -162,7 +314,7 @@ ${JSON.stringify(window.appState.device, null, 2)}
event.preventDefault();
}
function OnDragover(event) {
function OnBodyDragover(event) {
event.preventDefault();
}