From cb2359f8b5a1838d14837c5946803d499b7bf78d Mon Sep 17 00:00:00 2001 From: Cadence Ember Date: Sun, 21 Jun 2020 00:52:32 +1200 Subject: [PATCH] Better support for go to url --- src/renderer/store/modules/utils.js | 39 +++++++++++++++++++++-------- 1 file changed, 29 insertions(+), 10 deletions(-) diff --git a/src/renderer/store/modules/utils.js b/src/renderer/store/modules/utils.js index d5fac977..c46b8a13 100644 --- a/src/renderer/store/modules/utils.js +++ b/src/renderer/store/modules/utils.js @@ -53,18 +53,37 @@ const actions = { return state.colorClasses[randomInt] }, - getVideoIdFromUrl ({ state }, url) { - console.log('checking for id') - console.log(url) - const rx = /^.*(?:(?:(you|hook)tu\.?be\/|v\/|vi\/|u\/\w\/|embed\/)|(?:(?:watch)?\?v(?:i)?=|&v(?:i)?=))([^#&?]*).*/ - - const match = url.match(rx) - - if (match) { - return match[2] - } else { + getVideoIdFromUrl (_, url) { + /** @type {URL} */ + let urlObject + try { + urlObject = new URL(url) + } catch (e) { return false } + + const extractors = [ + // anything with /watch?v= + function() { + if (urlObject.pathname === "/watch" && urlObject.searchParams.has("v")) { + return urlObject.searchParams.get("v") + } + }, + // youtu.be + function() { + if (urlObject.host === "youtu.be" && urlObject.pathname.match(/^\/[A-Za-z0-9_-]+$/)) { + return urlObject.pathname.slice(1) + } + }, + // cloudtube + function() { + if (urlObject.host.match(/^cadence\.(gq|moe)$/) && urlObject.pathname.match(/^\/cloudtube\/video\/[A-Za-z0-9_-]+$/)) { + return urlObject.pathname.slice("/cloudtube/video/".length) + } + } + ] + + return extractors.reduce((a, c) => a || c(), null) || false } }