From 9f06826beda51d97650f22e477a0a35f0b772038 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Szopi=C5=84ski?= Date: Thu, 16 Jul 2020 13:15:38 +0200 Subject: [PATCH] Replace regex with URL parser --- background.js | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/background.js b/background.js index 09a1c2f..9c28008 100644 --- a/background.js +++ b/background.js @@ -2,15 +2,12 @@ const oldReddit = "https://old.reddit.com"; chrome.webRequest.onBeforeRequest.addListener( function(details) { - // Exclude poll pages - if (details.url.match(/^https?:\/\/(www\.)*reddit.com\/poll/)) { - return; - } - - return { - redirectUrl: - oldReddit + details.url.match(/^https?:\/\/[^\/]+([\S\s]*)/)[1] - }; + let urlParser = document.createElement("a"); + urlParser.href = details.url; + + if (urlParser.hostname == "old.reddit.com") return; + + return {redirectUrl: oldReddit + urlParser.pathname}; }, { urls: [