From dc55d1217837ee776f6b7138bf1f12eb28abe62d Mon Sep 17 00:00:00 2001 From: Tim Keller Date: Thu, 14 May 2026 15:57:12 -0500 Subject: refactor and fix createAnchors function --- static/index.js | 36 +++++++++++++++++++++--------------- 1 file changed, 21 insertions(+), 15 deletions(-) (limited to 'static') 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 `${url}` - }) + function _a(href, innerText) { + return `${innerText ?? href}` + } + 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 */ -- cgit v1.2.3