Skip to content

Commit

Permalink
fix: solve the issue with the web page content being read to decide i…
Browse files Browse the repository at this point in the history
…f the browsed page should be blocked or not (#1)
  • Loading branch information
JeremieLitzler committed Aug 7, 2024
1 parent e261cad commit 6ab2e8e
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 5 deletions.
26 changes: 22 additions & 4 deletions background.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,16 @@ async function getAllBlockedSites() {
});
}

function extractDomain(url) {
let domain;
try {
domain = new URL(url).hostname;
} catch (e) {
domain = url;
}
return domain.replace(/^www\./, '');
}

chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
if (request.action === 'isDbReady') {
getDB()
Expand Down Expand Up @@ -145,14 +155,22 @@ chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
});

chrome.webNavigation.onBeforeNavigate.addListener((details) => {
// Only process main frame navigation
if (details.frameId !== 0) return;

getAllBlockedSites().then((blockedSites) => {
const currentTime = new Date();
const currentMinutes =
currentTime.getHours() * 60 + currentTime.getMinutes();

const matchingSites = blockedSites.filter((site) =>
details.url.includes(site.site)
);
const urlDomain = extractDomain(details.url);

const matchingSites = blockedSites.filter((site) => {
const blockedDomain = extractDomain(site.site);
return (
urlDomain === blockedDomain || urlDomain.endsWith(`.${blockedDomain}`)
);
});
const shouldBlock = matchingSites.some((site) => {
const startMinutes = timeToMinutes(site.startTime);
const endMinutes = timeToMinutes(site.endTime);
Expand All @@ -161,7 +179,7 @@ chrome.webNavigation.onBeforeNavigate.addListener((details) => {

if (shouldBlock) {
chrome.tabs.update(details.tabId, {
url: chrome.runtime.getURL('blocked.html'),
url: chrome.runtime.getURL(`blocked.html?source=${details.url}`),
});
}
});
Expand Down
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"manifest_version": 3,
"name": "Website Blocker",
"version": "0.0.0.42",
"version": "0.0.0.44",
"description": "Blocks specified websites during set times",
"permissions": ["storage", "tabs", "webNavigation"],
"host_permissions": ["<all_urls>"],
Expand Down

0 comments on commit 6ab2e8e

Please sign in to comment.