From b1c98a24387c9f3f77680c5c7b1731f7fb0a5469 Mon Sep 17 00:00:00 2001 From: Jeremy Karlsson Date: Thu, 2 Feb 2023 14:12:28 +0100 Subject: [PATCH 1/3] Use the new embed player via TIDALs OEmbed API. --- src/Element/TidalEmbed.tsx | 43 +++++++++++++++++++++----------------- 1 file changed, 24 insertions(+), 19 deletions(-) diff --git a/src/Element/TidalEmbed.tsx b/src/Element/TidalEmbed.tsx index 49be6820..aa08f53a 100644 --- a/src/Element/TidalEmbed.tsx +++ b/src/Element/TidalEmbed.tsx @@ -1,27 +1,32 @@ -import { useEffect, useMemo } from "react"; +import { useEffect, useState } from "react"; import { TidalRegex } from "Const"; +async function oembedLookup (link: string) { + // Regex + re-construct to handle both tidal.com/type/id and tidal.com/browse/type/id links. + const regexResult = TidalRegex.exec(link); + + if (!regexResult) { + return undefined; + } + + const [, productType, productId] = regexResult; + const oembedApi = `https://oembed.stage.tidal.com/?url=https://tidal.com/browse/${productType}/${productId}`; + + const apiResponse = await fetch(oembedApi); + const json = await apiResponse.json(); + + return json.html; +} + const TidalEmbed = ({ link }: { link: string }) => { - const data = useMemo(() => { - const match = link.match(TidalRegex); - if (match?.length != 3) { - return null; - } - let type = match[1][0]; - let id = match[2]; - return { type, id }; + const [embed, setEmbed] = useState(); + + useEffect(() => { + oembedLookup(link).then(setEmbed); }, [link]); - const ScriptSrc = "https://embed.tidal.com/tidal-embed.js"; - useEffect(() => { - let sTag = document.createElement("script"); - sTag.src = ScriptSrc; - sTag.async = true; - document.head.appendChild(sTag); - }, []); - - if (!data) return e.stopPropagation()} className="ext">{link}; - return
; + if (!embed) return e.stopPropagation()} className="ext">{link}; + return
; } export default TidalEmbed; \ No newline at end of file From 43c074fa4c60c3414f4c2745bc985e5de93db896 Mon Sep 17 00:00:00 2001 From: Jeremy Karlsson Date: Fri, 3 Feb 2023 15:11:22 +0100 Subject: [PATCH 2/3] Copy source from delivered iframe and use height from OEmbed API. --- src/Element/TidalEmbed.tsx | 33 +++++++++++++++++++++++++-------- 1 file changed, 25 insertions(+), 8 deletions(-) diff --git a/src/Element/TidalEmbed.tsx b/src/Element/TidalEmbed.tsx index aa08f53a..b1e7685e 100644 --- a/src/Element/TidalEmbed.tsx +++ b/src/Element/TidalEmbed.tsx @@ -1,32 +1,49 @@ import { useEffect, useState } from "react"; import { TidalRegex } from "Const"; +// Re-use dom parser across instances of TidalEmbed +const domParser = new DOMParser(); + async function oembedLookup (link: string) { // Regex + re-construct to handle both tidal.com/type/id and tidal.com/browse/type/id links. const regexResult = TidalRegex.exec(link); if (!regexResult) { - return undefined; + return Promise.reject('Not a TIDAL link.'); } const [, productType, productId] = regexResult; - const oembedApi = `https://oembed.stage.tidal.com/?url=https://tidal.com/browse/${productType}/${productId}`; + const oembedApi = `https://oembed.tidal.com/?url=https://tidal.com/browse/${productType}/${productId}`; const apiResponse = await fetch(oembedApi); const json = await apiResponse.json(); - return json.html; + const doc = domParser.parseFromString(json.html, 'text/html'); + const iframe = doc.querySelector('iframe'); + + if (!iframe) { + return Promise.reject('No iframe delivered.'); + } + + return { + source: iframe.getAttribute('src'), + height: json.height + }; } const TidalEmbed = ({ link }: { link: string }) => { - const [embed, setEmbed] = useState(); + const [source, setSource] = useState(); + const [height, setHeight] = useState(); useEffect(() => { - oembedLookup(link).then(setEmbed); + oembedLookup(link).then(data => { + setSource(data.source || undefined); + setHeight(data.height); + }).catch(console.error); }, [link]); - if (!embed) return e.stopPropagation()} className="ext">{link}; - return
; + if (!source) return e.stopPropagation()} className="ext">{link}; + return