From 68a89b1c0cf234f9269bed465887ac3ec9da6196 Mon Sep 17 00:00:00 2001 From: Jason Kim Date: Thu, 22 Apr 2021 21:00:52 -0700 Subject: [PATCH 1/7] feat: add redd.it to reddit targets Although it doesn't work for teddit, add redd.it to the list of URLs to redirect. --- src/assets/javascripts/helpers/reddit.js | 1 + 1 file changed, 1 insertion(+) diff --git a/src/assets/javascripts/helpers/reddit.js b/src/assets/javascripts/helpers/reddit.js index a21b328..53b309a 100644 --- a/src/assets/javascripts/helpers/reddit.js +++ b/src/assets/javascripts/helpers/reddit.js @@ -4,6 +4,7 @@ const targets = [ "new.reddit.com", "amp.reddit.com", "i.redd.it", + "redd.it", ]; const redirects = [ // libreddit: privacy w/ modern UI From 9fc3fccf655f947da848a3571dc3fa7a10d9a727 Mon Sep 17 00:00:00 2001 From: Jason Kim Date: Thu, 22 Apr 2021 21:07:32 -0700 Subject: [PATCH 2/7] fix: don't redirect redd.it for teddit redd.it redirects don't work for teddit, so don't do it. --- src/pages/background/background.js | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/pages/background/background.js b/src/pages/background/background.js index 87bc8dd..92502dc 100644 --- a/src/pages/background/background.js +++ b/src/pages/background/background.js @@ -482,6 +482,12 @@ function redirectReddit(url, initiator, type) { } else { return null; } + } else if (url.host === "redd.it") { + if (redditInstance.includes("teddit")) { + // As of 2021-04-22, redirects for teddit on redd.it links don't work: + // they take you to the home page. + return null; + } } return `${redditInstance}${url.pathname}${url.search}`; } From bb6894cec54f746d76d8209ca25198881cf9d29b Mon Sep 17 00:00:00 2001 From: Jason Kim Date: Thu, 22 Apr 2021 22:16:41 -0700 Subject: [PATCH 4/7] feat: support teddit by adding /comments hint Redirecting tedd.it links to teddit instances works when the link starts with "/comments". Add "/comments" to the path for teddit. --- src/pages/background/background.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/pages/background/background.js b/src/pages/background/background.js index 92502dc..82f65a2 100644 --- a/src/pages/background/background.js +++ b/src/pages/background/background.js @@ -484,9 +484,9 @@ function redirectReddit(url, initiator, type) { } } else if (url.host === "redd.it") { if (redditInstance.includes("teddit")) { - // As of 2021-04-22, redirects for teddit on redd.it links don't work: - // they take you to the home page. - return null; + // As of 2021-04-22, redirects for teddit redd.it links don't work out of + // the box. Prefixing the path with "/comments" seems to help. + return `${redditInstance}/comments${url.pathname}${url.search}`; } } return `${redditInstance}${url.pathname}${url.search}`; From 21ee6b8542af700dc3d81520c7e62bc2657cb2af Mon Sep 17 00:00:00 2001 From: Jason Kim Date: Thu, 22 Apr 2021 22:43:30 -0700 Subject: [PATCH 5/7] fix: add "/comments" prefix only if it's missing Although I have never seen it in the wild, it is possible to navigate to "redd.it/comments/...". This should redirect to "teddit.net/comments/..." in the case of instance teddit.net. However, the current code redirects it to "teddit.net/comments/comments/...". Fix it by avoiding adding the prefix if it's already there. --- src/pages/background/background.js | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/src/pages/background/background.js b/src/pages/background/background.js index 82f65a2..d3ec06c 100644 --- a/src/pages/background/background.js +++ b/src/pages/background/background.js @@ -483,9 +483,15 @@ function redirectReddit(url, initiator, type) { return null; } } else if (url.host === "redd.it") { - if (redditInstance.includes("teddit")) { - // As of 2021-04-22, redirects for teddit redd.it links don't work out of - // the box. Prefixing the path with "/comments" seems to help. + if ( + redditInstance.includes("teddit") && + !url.pathname.startsWith("/comments/") + ) { + // As of 2021-04-22, redirects for teddit redd.it links don't work unless + // the path starts with "/comments". It appears that all links that + // don't start with "/comments" are interchangeable with the ones + // that do start with "/comments", so manually add that prefix if it is + // missing. return `${redditInstance}/comments${url.pathname}${url.search}`; } } From e3df6c4333c107240a45d6fbad933b54caa11139 Mon Sep 17 00:00:00 2001 From: Jason Kim Date: Thu, 22 Apr 2021 22:54:00 -0700 Subject: [PATCH 6/7] fix: add comments prefix if no nested path The previous fix failed to consider links like "redd.it/r/.../comments/...". Those don't really exist in the wild, and they don't work (when redirects are turned off). Still, play it safe and don't add "/comments" prefix unless the path has height 1. Now, redirects should work for - redd.it/foo - redd.it/comments/foo - redd.it/r/bar/comments/foo even though the only kind of native link that works is - redd.it/foo --- src/pages/background/background.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/pages/background/background.js b/src/pages/background/background.js index d3ec06c..fd0d55a 100644 --- a/src/pages/background/background.js +++ b/src/pages/background/background.js @@ -485,7 +485,7 @@ function redirectReddit(url, initiator, type) { } else if (url.host === "redd.it") { if ( redditInstance.includes("teddit") && - !url.pathname.startsWith("/comments/") + !url.pathname.match(/^\/\S+\//) ) { // As of 2021-04-22, redirects for teddit redd.it links don't work unless // the path starts with "/comments". It appears that all links that From 8e11c4885074b2029ec3705415f0191826eaf3ef Mon Sep 17 00:00:00 2001 From: Jason Kim Date: Thu, 22 Apr 2021 23:32:31 -0700 Subject: [PATCH 7/7] fix: add comments prefix for /foo/ Links that end in trailing slash but don't have "/comments/" should add "/comments" prefix but don't because the regex match succeeds. Fix the regular expression to be more robust and add prefix for paths like - /foo/ - /////foo - /foo///// - ////foo//// and not add prefix for paths like - /comments/foo - /////comments/foo - /comments/////foo - ////comments////foo - ////comments////foo///// --- src/pages/background/background.js | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/src/pages/background/background.js b/src/pages/background/background.js index fd0d55a..4fa3693 100644 --- a/src/pages/background/background.js +++ b/src/pages/background/background.js @@ -485,13 +485,16 @@ function redirectReddit(url, initiator, type) { } else if (url.host === "redd.it") { if ( redditInstance.includes("teddit") && - !url.pathname.match(/^\/\S+\//) + !url.pathname.match(/^\/+[^\/]+\/+[^\/]/) ) { - // As of 2021-04-22, redirects for teddit redd.it links don't work unless - // the path starts with "/comments". It appears that all links that - // don't start with "/comments" are interchangeable with the ones - // that do start with "/comments", so manually add that prefix if it is - // missing. + // As of 2021-04-22, redirects for teddit redd.it/foo links don't work. + // It appears that adding "/comments" as a prefix works, so manually add + // that prefix if it is missing. Even though redd.it/comments/foo links + // don't seem to work or exist, guard against affecting those kinds of + // paths. + // + // Note the difference between redd.it/comments/foo (doesn't work) and + // teddit.net/comments/foo (works). return `${redditInstance}/comments${url.pathname}${url.search}`; } }