// this doesn't properly deal with eg, .gov.uk .co.ck etc function parseDomain(host){ var chunks = host.split(".") if (chunks.length == 1) return chunks[0] else return chunks[chunks.length - 2] } function escapeHtml(txt) { if (!txt) { return ""; } // txt = annoyingCaps(txt) return $("").text(txt).html() } URLRegex = /((\b(http\:\/\/|https\:\/\/|ftp\:\/\/)|(www\.))+(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?)/gi; 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' + text + ' '; } function recipientReplace(atText, recips) { recips = recips || []; var space = ''; if (atText[0] == ' ') { atText = atText.slice(1); space = ' '; } var nick = atText.slice(1).toLowerCase(); var matchedRecip; for (var i = 0; i < recips.length; i++) { if (recips[i].toLowerCase() == nick) { matchedRecip = recips[i]; break; } } if (matchedRecip) { return space + '@' + matchedRecip + ''; } else { return space + atText; } } function linkify(text, recips) { LastMsgContainsImage = false; var recipWrapper = function(text) { return recipientReplace(text, recips); }; return text .replace(URLRegex, linkReplace) .replace(RecipRegex, recipWrapper) .replace(TopicRegex, topicReplace); } // use this in escapeHtml to turn everyone's text lIkE tHiS function annoyingCaps(text){ var chunks = text.split(" ") 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 type = getUriType(uri) if (type == 'image') { LastMsgContainsImage = true; 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 "" + "" + "" + split.suffix; } else if (type == 'midi') { return ' ' + uri.file + '' + split.suffix; } else if (type == 'wav') { return ' ' + uri.file + '' + split.suffix; } else return "" + split.url + "" + split.suffix; } function getUriType(uri){ if (PicRegex.test(uri.file.toLowerCase())) return "image"; if (VideoRegex.test(uri.file.toLowerCase())) return "video"; var domain = parseDomain(uri.host) if (domain == "gstatic" && uri.path == "/images" && 'q' in uri.queryKey) return "image"; // actual image url = uri.queryKey['q'].split(":").slice(2).join(":") but often the original image is broken... if (domain == "youtube" && ('v' in uri.queryKey || uri.anchor.indexOf('v') != -1)) return "youtube"; if (uri.path.substr(-4) == ".mid" || uri.path.substr(-5) == ".midi") return "midi" if (uri.path.substr(-4) == ".wav") return "wav" return "link"; } function linkifyWithoutImage(text) { LastMsgContainsImage = false return text.replace(URLRegex, linkReplaceWithoutImage); } function linkReplaceWithoutImage(url){ var urlWithoutParams = url.replace(/\?.*$/i, ""); linkUrl = normalizeUrl(url); return "" + url + "" }