From 5bcf2586305bb9f246497558f46f2fa15f686364 Mon Sep 17 00:00:00 2001 From: yo momma Date: Fri, 30 Jan 2026 09:52:51 +0000 Subject: Fix: restore hotlinks + cache-bust pichat.js --- static/js/src/text.js | 104 ++++++++++++++++++++++++++++++++++++++++------- static/js/src/youtube.js | 2 +- 2 files changed, 90 insertions(+), 16 deletions(-) (limited to 'static/js/src') diff --git a/static/js/src/text.js b/static/js/src/text.js index 6fe5c3c..d99efbb 100755 --- a/static/js/src/text.js +++ b/static/js/src/text.js @@ -14,21 +14,74 @@ function escapeHtml(txt) { URLRegex = /((\b(http\:\/\/|https\:\/\/|ftp\:\/\/)|(www\.))+(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?)/gi; -PicRegex = /\.(jpg|jpeg|png|gif|bmp|svg|fid)$/i; +PicRegex = /\.(jpg|jpeg|png|gif|bmp|svg|webp|fid)$/i; +VideoRegex = /\.(mp4|webm|mov|m4v|gifv)$/i; RecipRegex = /(^|\s)@\w+/g; TopicRegex = /(^|\s)#\w+/g; +function splitTrailingPunctuation(url) { + if (!url) return { "url": url, "suffix": "" }; + // Common punctuation that follows pasted links in chat. + var match = url.match(/[)\]\}\.,!\?;:'"]+$/); + if (!match) return { "url": url, "suffix": "" }; + return { "url": url.slice(0, -match[0].length), "suffix": match[0] }; +} + +function imgurIdFromUri(uri) { + var host = (uri.host || "").toLowerCase(); + if (!host || !host.match(/(^|\.)imgur\.com$/i)) return null; + if (!uri.file) return null; + + var fileLower = uri.file.toLowerCase(); + if (PicRegex.test(fileLower) || VideoRegex.test(fileLower)) return null; + + if (!uri.file.match(/^[A-Za-z0-9]+$/)) return null; + return uri.file; +} + +function imgurCandidateUrls(id) { + var base = "https://i.imgur.com/" + id; + return [base + ".jpg", base + ".png", base + ".gif", base + ".jpeg"]; +} + +function imgurHotlinkFallback(img) { + try { + var candidates = (img.getAttribute("data-imgur-candidates") || "").split("|"); + if (!candidates.length || !candidates[0]) return; + + var idx = parseInt(img.getAttribute("data-imgur-idx") || "0", 10); + var next = idx + 1; + if (next >= candidates.length) { + var link = img.parentNode; + if (link && link.tagName == "A") { + var href = link.getAttribute("href") || ""; + while (link.firstChild) link.removeChild(link.firstChild); + link.appendChild(document.createTextNode(href)); + } + return; + } + + img.setAttribute("data-imgur-idx", "" + next); + img.src = candidates[next]; + } catch (e) {} +} + function getImagesAsArray(text) { var imgs = [] var urls = text.match(URLRegex) if (urls === null) return imgs for (var i = 0; i). + var imgurId = imgurIdFromUri(uri); + if (imgurId) { + LastMsgContainsImage = true; + var candidates = imgurCandidateUrls(imgurId); + var first = candidates[0]; + return "" + + "" + + "" + split.suffix; + } + + linkUrl = hrefUrl; - var uri = parseUri(url) var type = getUriType(uri) if (type == 'image') { LastMsgContainsImage = true; - return ""; + return "" + split.suffix; + } else if (type == 'video') { + LastMsgContainsImage = true; + var videoUrl = linkUrl.replace(/\.gifv([?#].*)?$/i, ".mp4$1"); + return "" + split.suffix; } else if (type == 'youtube') { Youtube.startAnimation(); - return "" + - "" + - "" + return "" + + "" + + "" + split.suffix; } else if (type == 'midi') { - return ' '+uri.file+'' + return ' ' + uri.file + '' + split.suffix; } else if (type == 'wav') { - return ' '+uri.file+'' + return ' ' + uri.file + '' + split.suffix; } else - return "" + url + ""; + return "" + split.url + "" + split.suffix; } @@ -130,6 +201,9 @@ function linkReplace(url) { function getUriType(uri){ if (PicRegex.test(uri.file.toLowerCase())) return "image"; + + if (VideoRegex.test(uri.file.toLowerCase())) + return "video"; var domain = parseDomain(uri.host) diff --git a/static/js/src/youtube.js b/static/js/src/youtube.js index 2b6a977..ccb2cd4 100755 --- a/static/js/src/youtube.js +++ b/static/js/src/youtube.js @@ -19,7 +19,7 @@ Youtube = { var img = $(this); // yt thumb url example: https://i.ytimg.com/vi/0123456789A/1.jpg var src = img.attr("src") || "" - var match = src.match(/\\/vi\\/([^/]{11})\\/(\\d)\\.jpg/i) + var match = src.match(/\/vi\/([^/]{11})\/(\d)\.jpg/i) if (!match) return var v = match[1] var num = match[2] -- cgit v1.2.3-70-g09d2