fix localizing post URL #1

This commit is contained in:
teddit 2020-12-02 21:02:54 +01:00
parent 744617a49b
commit 620a400e89
1 changed files with 28 additions and 0 deletions

View File

@ -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;