From 620a400e8927a73c962b74b4897c862b5e7cf32a Mon Sep 17 00:00:00 2001 From: teddit Date: Wed, 2 Dec 2020 21:02:54 +0100 Subject: [PATCH] fix localizing post URL #1 --- views/includes/head.pug | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/views/includes/head.pug b/views/includes/head.pug index db3c6ad..29b7af2 100644 --- a/views/includes/head.pug +++ b/views/includes/head.pug @@ -75,6 +75,18 @@ meta(name="viewport", content="width=device-width, initial-scale=1.0") if(u.host === 'www.reddit.com' || u.host === 'reddit.com') { url = url.replace(u.host, 'teddit.net') } + if(u.host === 'i.redd.it' || u.host === 'v.redd.it') { + let image_exts = ['png', 'jpg', 'jpeg'] + let video_exts = ['mp4', 'gif', 'gifv'] + let file_ext = getFileExtension(url) + if(image_exts.includes(file_ext)) + url = url.replace(`${u.host}/`, 'teddit.net/pics/w:null_') + if(video_exts.includes(file_ext)) + url = url.replace(u.host, 'teddit.net/vids') + '.mp4' + if(!video_exts.includes(file_ext) && !image_exts.includes(file_ext)) + url = url.replace(u.host, 'teddit.net/vids') + '.mp4' + } + } catch(e) { } return url } @@ -95,6 +107,22 @@ meta(name="viewport", content="width=device-width, initial-scale=1.0") return new Date(time * 1000).toISOString().substr(14,5) } + function getFileExtension(url) { + try { + url = new URL(url) + let pathname = url.pathname + let file_ext = pathname.substring(pathname.lastIndexOf('.') + 1) + if(file_ext) { + return file_ext + } else { + return '' + } + } catch (error) { + console.error(`Invalid url supplied to getFileExtension(). URL: ${url}`, error) + return '' + } + } + function cleanTitle(s) { if(s) { var re = /&(?:amp|#38|lt|#60|gt|#62|apos|#39|quot|#34);/g;