Files
playlet-piped/playlet-web/public/index.v1.html
T
Brahim Hadriche ed7c5c618b Feature/web socket (#25)
* Add basic handshake

* Basic message exchange

* Base for a simple socket server

* Web socket events

* changelog

* Small tweak
2022-12-08 17:31:43 -05:00

367 lines
11 KiB
HTML

<!DOCTYPE html>
<html lang="en">
<head>
<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="OnBodyDrop(event);" ondragover="OnBodyDragover(event);">
<h2>Playlet - Youtube Player for Roku</h2>
<h4 id="app-version">Version: </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;">
<h4 id="logged-in">Logged In: </h4>
<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>
</br>
<button onclick="ClearSearchHistory()">Clear Search History</button>
<h5>Instructions</h5>
<ul>
<li>Make sure you are logged in the Invidious instance before logging in on this page</li>
<li>If this is your first time using this invidious Instance, you might want to create an account</li>
<li>Once you are logged in to your current instance, click "Login" on this page, and authorize the token</li>
<li>Once logged in, this page should show "Logged In: true"</li>
<li>After login, the home page should refresh automatically. You should then be able to see your Subscription feed
and Playlists, if you have any.</li>
</ul>
<p>
This is a barebone page for now, stay tuned for a redesigned page!
</p>
<h5>Feedback is welcome over at: <a id="feedback-url" href="https://github.com/iBicha/playlet"
target="_blank">https://github.com/iBicha/playlet</a></h5>
<script>
function Login() {
if (!window.appState) {
alert("Error with login, please refresh the page.")
}
window.location = window.appState.invidious.auth_url
}
function Logout() {
PostJson('/api/command', { command: "logout" })
.then(function (response) {
window.location.reload();
})
.catch(function (err) {
alert(err)
});
}
function PlayVideo() {
let v = document.getElementById('video-id').value
if (IsValidHttpUrl(v)) {
v = GetVideoId(v)
}
if (!v || v.length < 8) {
alert("Please set video id")
return
}
PostJson('/api/command', { command: "play", videoId: v })
.catch(function (err) {
alert(err)
});
}
function ClearSearchHistory() {
PostJson('/api/command', { command: "clear-search-history" })
.then(function (response) {
alert("Search history cleared");
})
.catch(function (err) {
alert(err)
});
}
function CreateGithubIssueUrl() {
title = "[Feedback] Playlet"
body = `### Feedback
_insert feedback here_
#### App Info
\`\`\`
${JSON.stringify(window.appState.app, null, 2)}
\`\`\`
#### Device Info
\`\`\`
${JSON.stringify(window.appState.device, null, 2)}
\`\`\``;
return `https://github.com/iBicha/playlet/issues/new?title=${encodeURIComponent(title)}&body=${encodeURIComponent(body)}`
}
function IsValidHttpUrl(string) {
let url;
try {
url = new URL(string);
} catch (_) {
return false;
}
return url.protocol === "http:" || url.protocol === "https:";
}
function GetVideoId(url) {
// Share/Short url
const YoutubeUrls = [
"https://youtu.be/",
"http://youtu.be/",
"https://www.youtu.be/",
"http://www.youtu.be/",
"https://youtube.com/shorts/",
"http://youtube.com/shorts/",
"https://www.youtube.com/shorts/",
"http://www.youtube.com/shorts/",
]
for (var i in YoutubeUrls) {
let youtubeUrl = YoutubeUrls[i];
if (url.startsWith(youtubeUrl)) {
url = url.replace(youtubeUrl, "")
if (url.includes("?")) {
url = url.substring(0, url.indexOf("?"))
}
return url
}
}
// regular url
url = new URL(url)
const urlSearchParams = new URLSearchParams(url.search);
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);
PostJson('/invidious/instances', instances)
.then(function (response) {
alert("Instances updated. Restart Playlet to reflect changes.")
window.location.reload();
})
.catch(function (err) {
alert(err)
});
}
function PostJson(url, payload) {
return fetch(url,
{
headers: {
'Content-Type': 'application/json'
},
method: "POST",
body: JSON.stringify(payload)
})
}
function Initialize() {
fetch("/api/state")
.then(function (response) {
return response.json();
})
.then(function (appState) {
window.appState = appState
console.log(appState)
document.getElementById("app-version").innerText = `Version: ${appState.app.version}`
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()
})
.catch(function (err) {
alert(err)
});
SetupWebSocket();
}
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) && GetVideoId(dataString)) {
document.getElementById('video-id').value = dataString
}
});
}
}
event.preventDefault();
}
function OnBodyDragover(event) {
event.preventDefault();
}
function SetupWebSocket() {
var socket = new WebSocket(`ws://${window.location.host}`);
socket.onopen = function (event) {
console.log("WebSocket connected!")
const message = "Hello?"
console.log(`Sending: "${message}"`)
socket.send(message);
};
socket.onclose = function (event) {
console.log("WebSocket closed!")
};
socket.onerror = function (event) {
console.log('WebSocket error: ', event);
};
socket.onmessage = function (event) {
console.log(`Received: "${event.data}"`);
}
}
Initialize();
</script>
</body>
</html>