diff options
| author | Tim Keller <tjk@tjkeller.xyz> | 2026-05-14 15:57:12 -0500 |
|---|---|---|
| committer | Tim Keller <tjk@tjkeller.xyz> | 2026-05-14 15:57:12 -0500 |
| commit | dc55d1217837ee776f6b7138bf1f12eb28abe62d (patch) | |
| tree | c1599a2973a1984e4cdfe8350fe06376d82a287c /static/index.js | |
| parent | a07fdacd4b1c23b9a4b112537e05260ae9c50906 (diff) | |
| download | embedtube-dc55d1217837ee776f6b7138bf1f12eb28abe62d.tar.xz embedtube-dc55d1217837ee776f6b7138bf1f12eb28abe62d.zip | |
refactor and fix createAnchors function
Diffstat (limited to 'static/index.js')
| -rw-r--r-- | static/index.js | 36 |
1 files changed, 21 insertions, 15 deletions
diff --git a/static/index.js b/static/index.js index e1c1f41..6505a7e 100644 --- a/static/index.js +++ b/static/index.js @@ -2,7 +2,7 @@ let player const titleBar = document.getElementById("title") -function onPlayerReady(event) { +function onPlayerReady(_e) { document.title = titleBar.textContent = player.videoTitle } @@ -39,20 +39,26 @@ function toggleVis(e) { } function createAnchors(text) { - const urlRegex = /(https?:\/\/[a-zA-Z0-9#$%&-./=?@_~]+[a-zA-Z0-9/]|@[a-zA-Z0-9.-_]+|[0-9]+:[0-9]+)/gi; - return text.replace(urlRegex, url => { - let href = url - if (url[0] == "@") { - href = `https://youtube.com/${url}` - } else if (parseInt(url[0]) !== NaN) { - const ts = url.split(":").reverse() - let t = 0 - for (let i = 0; i < ts.length; i++) - t += ts[i] * Math.pow(60, i) - href = `https://youtube.com/watch?v=${player.playerInfo.videoData.video_id}&t=${t}` - } - return `<a href="${href}" target="_blank" rel="noopener noreferrer">${url}</a>` - }) + function _a(href, innerText) { + return `<a href="${href}" rel="noopener noreferrer">${innerText ?? href}</a>` + } + const urlRegex = /https?:\/\/[a-z0-9#$%&-./=?@_~]+[a-z0-9/]/gi + const channelRegex = /@[a-z0-9.-_]+/gi + const timestampRegex = /[0-9]+:[0-9]+/g + + return text + .replace(urlRegex, url => { + const ytRegex = /^(https?:\/\/)?(www\.)?(youtu(be\.com|\.be|be-nocookie\.com))\//gi + const href = url.replace(ytRegex, `${window.location.origin}/`) + return _a(href, url) + }) + .replace(channelRegex, channel => { + return _a(`https://youtube.com/${channel}`, channel) + }) + .replace(timestampRegex, ts => { + const t = ts.split(":").reverse().reduce((a, v, i) => a + v * Math.pow(60, i), 0) /* calculate seconds from timestamp */ + return _a(`${window.location.origin}/watch?v=${player.playerInfo.videoData.video_id}&t=${t}`, ts) + }) } /* setup video details buttons */ |
