-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlinkhandler.js
38 lines (34 loc) · 1.13 KB
/
linkhandler.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
export async function processLink(link) {
try {
const url = new URL(link);
if (url.hostname === 'www.youtube.com' && url.pathname === '/watch') {
const videoId = url.searchParams.get('v');
if (videoId) {
return `https://cdpn.io/pen/debug/oNPzxKo?v=${videoId}`;
}
} else if (url.hostname === 'youtu.be') {
const videoId = url.pathname.slice(1); // youtu.be/VIDEO_ID
if (videoId) {
return `https://cdpn.io/pen/debug/oNPzxKo?v=${videoId}`;
}
} else {
throw new Error('Invalid YouTube link');
}
} catch (error) {
throw new Error('Invalid URL format');
}
}
export async function replaceLinks(link) {
const element = document.querySelector(
'#rcp-fe-viewport-root > section.rcp-fe-viewport-overlay > div:nth-child(3) > lol-uikit-full-page-modal > span > div > iframe'
);
if (!element) {
throw new Error('Iframe element not found');
}
if (link.includes('cdpn.io')) {
element.src = link;
element.setAttribute('autoplay', '1');
} else {
throw new Error('Invalid YouTube link');
}
}