Skip to content

Commit

Permalink
Update background.js
Browse files Browse the repository at this point in the history
Fixed recognizing unsafe and potentially unsafe links
  • Loading branch information
kenhendricks00 authored Nov 2, 2024
1 parent 58d3520 commit 977306b
Showing 1 changed file with 17 additions and 7 deletions.
24 changes: 17 additions & 7 deletions platform/firefox/js/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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
Expand All @@ -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
Expand Down

0 comments on commit 977306b

Please sign in to comment.