Skip to content

Commit

Permalink
Update background.js
Browse files Browse the repository at this point in the history
  • Loading branch information
kenhendricks00 authored Nov 1, 2024
1 parent dde4797 commit 5d44045
Showing 1 changed file with 17 additions and 10 deletions.
27 changes: 17 additions & 10 deletions platform/chromium/js/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,15 @@ function extractUrlsFromBookmarks(html) {

// Helper function to normalize URLs (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 {
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 @@ -42,21 +47,23 @@ function extractRootUrl(url) {
return `${urlObj.protocol}//${urlObj.hostname}`; // Extract protocol and hostname
}

// Function to generate a regex from a list of domains/URLs
// Function to generate a regex from a list of domains/URLs, anchoring to start and end of the URL
function generateRegexFromList(list) {
const escapedList = list.map((domain) =>
domain.replace(/[.*+?^${}()|[\]\\]/g, "\\$&")
const escapedList = list.map(
(domain) => domain.replace(/[.*+?^${}()|[\]\\]/g, "\\$&") // Escape special characters
);
return new RegExp(`(${escapedList.join("|")})`, "i");
// Anchor each domain with `(^|\\.)` at the start to ensure subdomain or exact match only
return new RegExp(`(^|\\.)(${escapedList.join("|")})($|\\/)`, "i");
}

// Helper function to extract URLs from filter lists (ignoring comments and empty lines)
function extractUrlsFromFilterList(text) {
return text
.split("\n")
.map((line) => line.trim())
.filter((line) => line && !line.startsWith("!")) // Ignore comments
.map((line) => normalizeUrl(line));
.filter((line) => line && !line.startsWith("!")) // Ignore comments and empty lines
.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 5d44045

Please sign in to comment.