From 977306bcc996b373857fbb68b64d09eae3e78a0d Mon Sep 17 00:00:00 2001 From: Kenneth Hendricks <50819541+kenhendricks00@users.noreply.github.com> Date: Sat, 2 Nov 2024 13:48:40 -0400 Subject: [PATCH] Update background.js Fixed recognizing unsafe and potentially unsafe links --- platform/firefox/js/background.js | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/platform/firefox/js/background.js b/platform/firefox/js/background.js index 523fb1b..446dce4 100644 --- a/platform/firefox/js/background.js +++ b/platform/firefox/js/background.js @@ -9,7 +9,7 @@ const starredListURL = let unsafeSitesRegex = null; let potentiallyUnsafeSitesRegex = null; let safeSites = []; -let starredSites = ["https://fmhy.net"]; +let starredSites = ["https://fmhy.net", "https://fmhy.pages.dev/"]; // Helper function to extract URLs from markdown text function extractUrlsFromMarkdown(markdown) { @@ -28,12 +28,21 @@ function extractUrlsFromBookmarks(html) { return urls; } -// Helper function to normalize URLs (removes trailing slashes, query parameters, and fragments) +// Helper function to normalize URLs (adds protocol if missing, removes trailing slashes, query parameters, and fragments) function normalizeUrl(url) { - const urlObj = new URL(url); - urlObj.search = ""; // Remove query parameters - urlObj.hash = ""; // Remove fragments - return urlObj.href.replace(/\/+$/, ""); // Remove trailing slash only + try { + // Check if the URL starts with "http" or "https", if not, prepend "https://" + if (!/^https?:\/\//i.test(url)) { + url = `https://${url}`; + } + const urlObj = new URL(url); + urlObj.search = ""; // Remove query parameters + urlObj.hash = ""; // Remove fragments + return urlObj.href.replace(/\/+$/, ""); // Remove trailing slash only + } catch (error) { + console.warn(`Invalid URL skipped: ${url}`); + return null; // Return null for invalid URLs + } } // Helper function to extract root domain from URL @@ -56,7 +65,8 @@ function extractUrlsFromFilterList(text) { .split("\n") .map((line) => line.trim()) .filter((line) => line && !line.startsWith("!")) // Ignore comments - .map((line) => normalizeUrl(line)); + .map((line) => normalizeUrl(line)) + .filter((url) => url !== null); // Filter out null values from invalid URLs } // Fetch the unsafe and potentially unsafe filter lists and generate regex