From 45dc798eef4158df497aad030f77f60d84bcd7e9 Mon Sep 17 00:00:00 2001 From: Jean-Luc Tibaux Date: Sat, 12 Nov 2022 13:47:08 +0000 Subject: [PATCH 1/2] implement imgur replacement (#341) --- inc/commons.js | 11 +++++++++++ routes/overides.js | 9 +++++++++ routes/preferences.js | 6 ++++++ views/preferences.pug | 6 ++++++ 4 files changed, 32 insertions(+) diff --git a/inc/commons.js b/inc/commons.js index ab3a0cf..78f449e 100644 --- a/inc/commons.js +++ b/inc/commons.js @@ -194,6 +194,13 @@ module.exports = function(request, fs) { let twitterRegex = /(?<=href=")(https?:\/\/)(www\.)?twitter\.com(?=.+")/gm; let instagramRegex = /(?<=href=")(https?:\/\/)(www+\.)?instagram.com(?=.+")/gm; let quoraRegex = /(?<=href=")(https?:\/\/)([A-z.]+\.)?quora\.com(?=.+")/gm; + + /* + * regex pattern to replace imgur links (imgur.com, imgur.io, i.stack.imgur.com) + * source: https://github.com/libredirect/libredirect/blob/32c4a0211e3b721d46219c05cba93f1a42cf3773/src/config/config.json#L317 + * license: GNU GPL v3 License -> https://github.com/libredirect/libredirect/blob/32c4a0211e3b721d46219c05cba93f1a42cf3773/LICENSE + */ + let imgurRegex = /^https?:\/{2}([im]\.)?(stack\.)?imgur\.(com|io)/gm; let protocol = config.https_enabled || config.api_force_https ? 'https://' : 'http://' @@ -264,6 +271,10 @@ module.exports = function(request, fs) { if(typeof(user_preferences.domain_quora) != 'undefined') if(user_preferences.domain_quora) str = str.replace(quoraRegex, protocol + user_preferences.domain_quora) + + if(typeof(user_preferences.domain_imgur) != 'undefined') + if(user_preferences.domain_imgur) + str = str.replace(imgurRegex, protocol + user_preferences.domain_imgur) return str } diff --git a/routes/overides.js b/routes/overides.js index 1e9072b..1822918 100644 --- a/routes/overides.js +++ b/routes/overides.js @@ -110,6 +110,15 @@ overridingRoutes.all('*', (req, res, next) => { }); } + let domainImgur = req.query.domain_imgur; + if (domainImgur) { + req.cookies.domain_imgur = domainImgur; + res.cookie('domain_imgur', domainImgur, { + maxAge: 31536000, + httpOnly: true, + }); + } + let videosMuted = req.query.videos_muted; if (videosMuted) { req.cookies.videos_muted = videosMuted; diff --git a/routes/preferences.js b/routes/preferences.js index 42dc1f5..1d84c91 100644 --- a/routes/preferences.js +++ b/routes/preferences.js @@ -16,6 +16,7 @@ function resetPreferences(res) { res.clearCookie('domain_youtube'); res.clearCookie('domain_instagram'); res.clearCookie('domain_quora'); + res.clearCookie('domain_imgur'); res.clearCookie('videos_muted'); res.clearCookie('prefer_frontpage'); res.clearCookie('show_large_gallery_images'); @@ -94,6 +95,7 @@ preferenceRoutes.post('/saveprefs', (req, res, next) => { let domain_youtube = req.body.domain_youtube; let domain_instagram = req.body.domain_instagram; let domain_quora = req.body.domain_quora; + let domain_imgur = req.body.domain_imgur; let videos_muted = req.body.videos_muted; let prefer_frontpage = req.body.prefer_frontpage; let show_large_gallery_images = req.body.show_large_gallery_images; @@ -178,6 +180,10 @@ preferenceRoutes.post('/saveprefs', (req, res, next) => { maxAge: 365 * 24 * 60 * 60 * 1000, httpOnly: true, }); + res.cookie('domain_imgur', domain_imgur, { + maxAge: 365 * 24 * 60 * 60 * 1000, + httpOnly: true, + }); if (prefer_frontpage === 'on') prefer_frontpage = 'true'; res.cookie('prefer_frontpage', prefer_frontpage, { diff --git a/views/preferences.pug b/views/preferences.pug index 87d4a98..37be813 100644 --- a/views/preferences.pug +++ b/views/preferences.pug @@ -37,6 +37,12 @@ html input(type="text", name="domain_quora", id="domain_quora", value="" + user_preferences.domain_quora + "", placeholder="e.g. quetre.iket.me") else input(type="text", name="domain_quora", id="domain_quora", placeholder="e.g. quetre.iket.me") + .setting + label(for="domain_imgur") Replace Imgur links with Rimgo (blank to disable): + if(user_preferences.domain_imgur != '' && typeof(user_preferences.domain_imgur) != 'undefined') + input(type="text", name="domain_imgur", id="domain_imgur", value="" + user_preferences.domain_imgur + "", placeholder="e.g. rimgo.bcow.xyz") + else + input(type="text", name="domain_imgur", id="domain_imgur", placeholder="e.g. rimgo.bcow.xyz") legend Display .setting label(for="theme") Theme: From 50c369833b47f73cd25d496e2ab19d260d88be70 Mon Sep 17 00:00:00 2001 From: Jean-Luc Tibaux Date: Sat, 26 Nov 2022 20:22:01 +0000 Subject: [PATCH 2/2] improved imgur regex pattern as requested in #365 --- inc/commons.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/inc/commons.js b/inc/commons.js index 78f449e..6acbab6 100644 --- a/inc/commons.js +++ b/inc/commons.js @@ -200,7 +200,7 @@ module.exports = function(request, fs) { * source: https://github.com/libredirect/libredirect/blob/32c4a0211e3b721d46219c05cba93f1a42cf3773/src/config/config.json#L317 * license: GNU GPL v3 License -> https://github.com/libredirect/libredirect/blob/32c4a0211e3b721d46219c05cba93f1a42cf3773/LICENSE */ - let imgurRegex = /^https?:\/{2}([im]\.)?(stack\.)?imgur\.(com|io)/gm; + let imgurRegex = /(?<=href=")(https?:\/{2})([im]\.)?(stack\.)?imgur\.(com|io)(?=.+")/gm; let protocol = config.https_enabled || config.api_force_https ? 'https://' : 'http://'