Skip to content

Commit

Permalink
Merge pull request #53 from penge/block-history-navigation
Browse files Browse the repository at this point in the history
Allow to block URLs navigated by browser history
  • Loading branch information
penge authored Jun 25, 2023
2 parents 6bf40aa + 7a9a929 commit 0c05f30
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 45 deletions.
7 changes: 6 additions & 1 deletion manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,12 @@
}
},
"options_page": "options.html",
"permissions": ["storage", "webNavigation", "contextMenus"],
"permissions": [
"storage",
"tabs",
"webNavigation",
"contextMenus"
],
"background": {
"service_worker": "background.mjs",
"type": "module"
Expand Down
46 changes: 42 additions & 4 deletions src/background.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,56 @@
import initStorage from "./storage/init";
import blockSite from "./helpers/block-site";
import storage from "./storage";
import createContextMenu from "./helpers/create-context-menu";
import blockSite from "./helpers/block-site";

let __enabled: boolean;
let __blocked: string[];

initStorage(() => {
createContextMenu();

storage.get(["enabled", "blocked"], ({ enabled, blocked }) => {
__enabled = enabled;
__blocked = blocked;
});

initStorage(createContextMenu);
chrome.storage.local.onChanged.addListener((changes) => {
if (changes["enabled"]) {
__enabled = changes["enabled"].newValue as boolean;
}

if (changes["blocked"]) {
__blocked = changes["blocked"].newValue as string[];
}
});
});

chrome.action.onClicked.addListener(() => {
chrome.runtime.openOptionsPage();
});

chrome.webNavigation.onBeforeNavigate.addListener((details) => {
const { tabId, url, timeStamp, frameId } = details;
if (!__enabled || !__blocked.length) {
return;
}

const { tabId, url, frameId } = details;
if (!url || !url.startsWith("http") || frameId !== 0) {
return;
}

blockSite({ tabId, url, timeStamp });
blockSite({ blocked: __blocked, tabId, url });
});

chrome.tabs.onUpdated.addListener((tabId, changeInfo) => {
if (!tabId || !__enabled || !__blocked.length) {
return;
}

const { status, url } = changeInfo;
if (status !== "loading" || !url || !url.startsWith("http")) {
return;
}

blockSite({ blocked: __blocked, tabId, url });
});
69 changes: 34 additions & 35 deletions src/helpers/block-site.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,50 +4,49 @@ import * as counterHelper from "./counter";
import getBlockedUrl from "./get-blocked-url";

interface BlockSiteOptions {
blocked: string[]
tabId: number
url: string
timeStamp: number
}

export default (options: BlockSiteOptions) => {
const { tabId, url, timeStamp } = options;
const { blocked, tabId, url } = options;
if (!blocked.length || !tabId || !url.startsWith("http")) {
return;
}

storage.get(["enabled", "blocked"], ({ enabled, blocked }) => {
if (!enabled || blocked.length === 0) {
return;
}

const foundRule = findRule(url, blocked);
if (!foundRule || foundRule.type === "allow") {
storage.get(["counter"], ({ counter }) => {
counterHelper.flushObsoleteEntries({ blocked, counter });
storage.set({ counter });
});
return;
}

storage.get(["counter", "counterShow", "counterPeriod", "resolution"], ({ counter, counterShow, counterPeriod, resolution }) => {
const foundRule = findRule(url, blocked);
if (!foundRule || foundRule.type === "allow") {
storage.get(["counter"], ({ counter }) => {
counterHelper.flushObsoleteEntries({ blocked, counter });
const count = counterHelper.add(foundRule.path, timeStamp, {
counter,
countFromTimeStamp: counterHelper.counterPeriodToTimeStamp(counterPeriod, new Date().getTime()),
});
storage.set({ counter });
});
return;
}

switch (resolution) {
case "CLOSE_TAB":
chrome.tabs.remove(tabId);
break;
case "SHOW_BLOCKED_INFO_PAGE": {
chrome.tabs.update(tabId, {
url: getBlockedUrl({
url,
rule: foundRule.path,
countParams: counterShow ? { count, period: counterPeriod } : undefined },
)},
);
break;
}}
storage.get(["counter", "counterShow", "counterPeriod", "resolution"], ({ counter, counterShow, counterPeriod, resolution }) => {
counterHelper.flushObsoleteEntries({ blocked, counter });

const timeStamp = Date.now();
const count = counterHelper.add(foundRule.path, timeStamp, {
counter,
countFromTimeStamp: counterHelper.counterPeriodToTimeStamp(counterPeriod, new Date().getTime()),
});
storage.set({ counter });

switch (resolution) {
case "CLOSE_TAB":
chrome.tabs.remove(tabId);
break;
case "SHOW_BLOCKED_INFO_PAGE": {
chrome.tabs.update(tabId, {
url: getBlockedUrl({
url,
rule: foundRule.path,
countParams: counterShow ? { count, period: counterPeriod } : undefined },
)},
);
break;
}}
});
};
8 changes: 3 additions & 5 deletions src/helpers/create-context-menu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,8 @@ const createContextMenu = () => {
const normalizedUrl = normalizeUrl(url);
const updatedBlocked = [...blocked, normalizedUrl];

storage.set({ blocked: updatedBlocked }, () => {
const timeStamp = Date.now();
blockSite({ tabId, url, timeStamp });
});
storage.set({ blocked: updatedBlocked });
blockSite({ blocked: updatedBlocked, tabId, url });
});
});
};
Expand All @@ -46,7 +44,7 @@ export default () => {
chrome.storage.local.onChanged.addListener((changes) => {
if (changes["enabled"]) {
chrome.contextMenus.removeAll(() => {
if (changes["enabled"].newValue) {
if (changes["enabled"].newValue as boolean) {
createContextMenu();
}
});
Expand Down

0 comments on commit 0c05f30

Please sign in to comment.