| 33 | ]; |
| 34 | |
| 35 | export const triggerEmbed = async (node?: Element | undefined) => { |
| 36 | if (!node) return; |
| 37 | |
| 38 | let apiURL = `${WEBEMBED_URL}/api/embed`; |
| 39 | const embedURL = node.getAttribute('href'); |
| 40 | |
| 41 | if (!embedURL) return; |
| 42 | |
| 43 | let isSupported = false; |
| 44 | |
| 45 | // eslint-disable-next-line no-restricted-syntax |
| 46 | for (const link of SUPPORTED_EMBEDS) { |
| 47 | if (embedURL.includes(link)) { |
| 48 | isSupported = true; |
| 49 | break; |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | try { |
| 54 | const _url = new URL(apiURL); |
| 55 | _url.searchParams.set('maxwidth', '800'); |
| 56 | _url.searchParams.set('url', encodeURIComponent(embedURL)); |
| 57 | |
| 58 | if (embedURL.includes('twitch.tv')) { |
| 59 | _url.searchParams.set('customHost', encodeURIComponent(window.location.hostname)); |
| 60 | } |
| 61 | |
| 62 | if (embedURL.includes('open.spotify.com')) { |
| 63 | _url.searchParams.set('width', '300'); |
| 64 | _url.searchParams.set('height', '380'); |
| 65 | } |
| 66 | |
| 67 | if (embedURL.includes('vimeo.com')) { |
| 68 | _url.searchParams.set('width', '640'); |
| 69 | } |
| 70 | |
| 71 | if (!isSupported) { |
| 72 | _url.searchParams.set('forceFallback', '1'); |
| 73 | } |
| 74 | |
| 75 | apiURL = _url.toString(); |
| 76 | } catch (error) { |
| 77 | return; |
| 78 | } |
| 79 | |
| 80 | const hnReq = new HNRequest(apiURL, { |
| 81 | credentials: 'same-origin', |
| 82 | }); |
| 83 | await hnReq.exec(); |
| 84 | const status = await hnReq.rawResponse?.status; |
| 85 | if (status !== 200) { |
| 86 | return; |
| 87 | } |
| 88 | |
| 89 | let data = await hnReq.rawResponse?.json(); |
| 90 | |
| 91 | if (data.data.error) { |
| 92 | return; |