Support copy pasting links in web app

This commit is contained in:
Brahim Hadriche committed 2023-10-27 16:32:11 -04:00
1 parent af020b509a
commit bde4523c00
2 files changed
+35 -18

No files matched your search

+1
View File
@@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Added
- Pagination support in web app search (load more button)
- Video links pasted into the web app triggers the dialog to cast the video
## [0.14.0] - 2023-10-25
+34 -18
View File
@@ -30,13 +30,22 @@
onMount(async () => {
document.body.addEventListener("drop", onDrop);
document.body.addEventListener("dragover", onDragOver);
document.body.addEventListener("paste", onPaste);
});
onDestroy(() => {
document.body.removeEventListener("drop", onDrop);
document.body.removeEventListener("dragover", onDragOver);
document.body.removeEventListener("paste", onPaste);
});
async function onPaste(event) {
modal.close();
const dataString = event.clipboardData.getData("text/plain");
await processUrlText(dataString);
}
async function onDrop(event) {
event.preventDefault();
modal.close();
@@ -48,20 +57,24 @@
let dataString = (await new Promise((resolve) =>
item.getAsString(resolve)
)) as string;
if (isValidHttpUrl(dataString)) {
const videoInfo = parseYouTubeUrl(dataString);
if (videoInfo.videoId) {
searchForVideoById(videoInfo.videoId, videoInfo.timestamp);
return;
} else {
// TODO:P2 make a HEAD request and check for a redirect, then
// resolve the redirect url.
const urlInfo: any = await invidiousApi.resolveUrl(dataString);
if (urlInfo && urlInfo.pageType === "WEB_PAGE_TYPE_CHANNEL") {
searchForChannelById(urlInfo.ucid);
return;
}
}
await processUrlText(dataString);
}
}
}
async function processUrlText(dataString: string) {
if (isValidHttpUrl(dataString)) {
const videoInfo = parseYouTubeUrl(dataString);
if (videoInfo.videoId) {
searchForVideoById(videoInfo.videoId, videoInfo.timestamp);
return;
} else {
// TODO:P2 make a HEAD request and check for a redirect, then
// resolve the redirect url.
const urlInfo: any = await invidiousApi.resolveUrl(dataString);
if (urlInfo && urlInfo.pageType === "WEB_PAGE_TYPE_CHANNEL") {
searchForChannelById(urlInfo.ucid);
return;
}
}
}
@@ -101,14 +114,17 @@
event.preventDefault();
}
function isValidHttpUrl(string) {
let url;
function isValidHttpUrl(url) {
if (!url) {
return false;
}
let urlObj;
try {
url = new URL(string);
urlObj = new URL(url);
} catch (_) {
return false;
}
return url.protocol === "http:" || url.protocol === "https:";
return urlObj.protocol === "http:" || urlObj.protocol === "https:";
}
function parseYouTubeUrl(url) {