aboutsummaryrefslogtreecommitdiff
path: root/static/index.js
diff options
context:
space:
mode:
Diffstat (limited to 'static/index.js')
-rw-r--r--static/index.js36
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 */