From 3a78833baa2430c8d373ff3ad43c27bea4e42068 Mon Sep 17 00:00:00 2001
From: Alex <56329333+404-Program-not-found@users.noreply.github.com>
Date: Wed, 13 Apr 2022 13:43:32 +1200
Subject: [PATCH] fix: link handling in search
---
NUXT/layouts/default.vue | 22 ++++++----------------
NUXT/plugins/utils.js | 19 +++++++++++++++----
2 files changed, 21 insertions(+), 20 deletions(-)
diff --git a/NUXT/layouts/default.vue b/NUXT/layouts/default.vue
index 9490380..59cac76 100644
--- a/NUXT/layouts/default.vue
+++ b/NUXT/layouts/default.vue
@@ -38,7 +38,7 @@
text
tile
dense
- class="searchButton text-left text-capitalize"
+ class="searchButton text-left"
@click="youtubeSearch(item)"
>
mdi-magnify
@@ -108,21 +108,10 @@ export default {
// --- External URL Handling --- //
CapacitorApp.addListener("appUrlOpen", (event) => {
- const slug = new URL(event.url);
this.$logger("ExternalURL", event.url);
// We only push to the route if there is a url present
- if (slug) {
- const host = slug.hostname.toLowerCase().replace(/^www\./, "");
- let result;
- if (host == "youtube.com") {
- result = slug;
- } else if (host == "youtu.be") {
- result = new URL("/watch", window.location.origin);
- result.searchParams.set("v", slug.pathname.split("/")[1]);
- }
- if (result instanceof URL)
- this.$router.push(result.pathname + result.search);
- }
+ linkParser(event.url);
+ if (result) this.$router.push(result.pathname + result.search);
});
// --- Import Twemoji ---///
@@ -144,8 +133,8 @@ export default {
if (isLink) {
this.response = [
{
- text: `Watch video from ID: ${isLink}`,
- id: isLink,
+ text: `Watch Video from ID: ${isLink.searchParams.get("v")}`,
+ id: isLink.searchParams.get("v"),
},
];
return;
@@ -230,6 +219,7 @@ div {
diff --git a/NUXT/plugins/utils.js b/NUXT/plugins/utils.js
index bec8eb7..f193169 100644
--- a/NUXT/plugins/utils.js
+++ b/NUXT/plugins/utils.js
@@ -42,10 +42,21 @@ function getMutationByKey(key, mutations) {
return mutations.find((mutation) => mutation.entityKey === key).payload;
}
function linkParser(url) {
- console.log("linkParpar", url)
- const regExp = /^.*((youtu.be\/)|(v\/)|(\/u\/\w\/)|(embed\/)|(watch\?))\??v?=?([^#&?]*).*/;
- const match = url.match(regExp);
- return (match && match[7].length == 11) ? match[7] : false;
+ let result;
+ if (url) {
+ try {
+ const slug = new URL(url);
+ const host = slug.hostname.toLowerCase().replace(/^www\./, "");
+ if (host == "youtube.com") {
+ result = slug;
+ } else if (host == "youtu.be") {
+ result = new URL("/watch", window.location.origin);
+ result.searchParams.set("v", slug.pathname.split("/")[1]);
+ }
+ } finally {
+ return result instanceof URL ? result : false
+ }
+ }
}
const delay = (ms) => new Promise((res) => setTimeout(res, ms));