Play API prototype (#8)

* Play API prototype

* Web app

* Drag and drop support

* Handle different urls

* Fix redirect

* Increase max connections

* changelog

* Limit timeout

* Use fetch

* Redirects

* Formatting
This commit is contained in:
Brahim Hadriche authored and GitHub committed 2022-11-21 18:47:12 -05:00
1 parent 192eec9519
commit 651bb54dee
15 files changed
+180 -39

No files matched your search

+101 -30
View File
@@ -7,10 +7,15 @@
<title>Playlet</title>
</head>
<body>
<body ondrop="OnDrop(event);" ondragover="OnDragover(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>
<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="visibility: hidden" id="login-button" onclick="Login()">Login</button>
<button style="visibility: hidden" id="logout-button">Logout</button>
@@ -31,20 +36,6 @@
target="_blank">https://github.com/iBicha/playlet</a></h5>
<script>
function RequestGet(url, cb) {
const req = new XMLHttpRequest();
req.addEventListener("load", function () {
try {
cb(null, JSON.parse(this.responseText));
}
catch (err) {
cb(err)
}
});
req.open("GET", url);
req.send();
}
function Login() {
if (!window.appState) {
alert("Error with login, please refresh the page.")
@@ -52,6 +43,21 @@
window.location = window.appState.invidious.auth_url
}
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
}
fetch(`/api/command/play?v=${v}`)
.catch(function (err) {
alert(err)
});
}
function CreateGithubIssueUrl() {
title = "[Feedback] Playlet"
body = `### Feedback
@@ -69,24 +75,89 @@ ${JSON.stringify(window.appState.device, null, 2)}
return `https://github.com/iBicha/playlet/issues/new?title=${encodeURIComponent(title)}&body=${encodeURIComponent(body)}`
}
RequestGet("/api/state", function (err, response) {
if (err) {
alert(err)
return
function IsValidHttpUrl(string) {
let url;
try {
url = new URL(string);
} catch (_) {
return false;
}
window.appState = response
console.log(response)
document.getElementById("app-version").innerText = `Version: ${response.app.version}`
document.getElementById("logged-in").innerText = `Logged In: ${response.invidious.logged_in}`
document.getElementById("host").innerText = response.invidious.host
document.getElementById("host").href = response.invidious.host
return url.protocol === "http:" || url.protocol === "https:";
}
document.getElementById("login-button").style.visibility = response.invidious.logged_in ? "hidden" : "visible";
// TODO: log out feature
// document.getElementById("logout-button").style.visibility = response.invidious.logged_in ? "hidden" : "visible";
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
}
}
document.getElementById("feedback-url").href = CreateGithubIssueUrl()
})
// regular url
url = new URL(url)
const urlSearchParams = new URLSearchParams(url.search);
return urlSearchParams.get('v')
}
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}`
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.visibility = appState.invidious.logged_in ? "hidden" : "visible";
// TODO: log out feature
// document.getElementById("logout-button").style.visibility = appState.invidious.logged_in ? "hidden" : "visible";
document.getElementById("feedback-url").href = CreateGithubIssueUrl()
})
.catch(function (err) {
alert(err)
});
}
function OnDrop(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)) {
document.getElementById('video-id').value = dataString
}
});
}
}
event.preventDefault();
}
function OnDragover(event) {
event.preventDefault();
}
Initialize();
</script>
</body>