Skip to content

Commit 70ca8a4

Browse files
authored
Merge pull request #50 from penge/context-menu
Add context menu to block site
2 parents 6d0d030 + d3c1a3c commit 70ca8a4

7 files changed

Lines changed: 126 additions & 58 deletions

File tree

manifest.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
}
1515
},
1616
"options_page": "options.html",
17-
"permissions": ["storage", "webNavigation"],
17+
"permissions": ["storage", "webNavigation", "contextMenus"],
1818
"background": {
1919
"service_worker": "background.mjs",
2020
"type": "module"

src/background.ts

Lines changed: 4 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,8 @@
11
import initStorage from "./storage/init";
2-
import storage from "./storage";
3-
import findRule from "./helpers/find-rule";
4-
import * as counterHelper from "./helpers/counter";
5-
import getBlockedUrl from "./helpers/get-blocked-url";
2+
import blockSite from "./helpers/block-site";
3+
import createContextMenu from "./helpers/create-context-menu";
64

7-
chrome.runtime.onInstalled.addListener(() => {
8-
initStorage();
9-
});
5+
initStorage(createContextMenu);
106

117
chrome.action.onClicked.addListener(() => {
128
chrome.runtime.openOptionsPage();
@@ -18,42 +14,5 @@ chrome.webNavigation.onBeforeNavigate.addListener((details) => {
1814
return;
1915
}
2016

21-
storage.get(["enabled", "blocked"], ({ enabled, blocked }) => {
22-
if (!enabled || blocked.length === 0) {
23-
return;
24-
}
25-
26-
const foundRule = findRule(url, blocked);
27-
if (!foundRule || foundRule.type === "allow") {
28-
storage.get(["counter"], ({ counter }) => {
29-
counterHelper.flushObsoleteEntries({ blocked, counter });
30-
storage.set({ counter });
31-
});
32-
return;
33-
}
34-
35-
storage.get(["counter", "counterShow", "counterPeriod", "resolution"], ({ counter, counterShow, counterPeriod, resolution }) => {
36-
counterHelper.flushObsoleteEntries({ blocked, counter });
37-
const count = counterHelper.add(foundRule.path, timeStamp, {
38-
counter,
39-
countFromTimeStamp: counterHelper.counterPeriodToTimeStamp(counterPeriod, new Date().getTime()),
40-
});
41-
storage.set({ counter });
42-
43-
switch (resolution) {
44-
case "CLOSE_TAB":
45-
chrome.tabs.remove(tabId);
46-
break;
47-
case "SHOW_BLOCKED_INFO_PAGE": {
48-
chrome.tabs.update(tabId, {
49-
url: getBlockedUrl({
50-
url,
51-
rule: foundRule.path,
52-
countParams: counterShow ? { count, period: counterPeriod } : undefined },
53-
)},
54-
);
55-
break;
56-
}}
57-
});
58-
});
17+
blockSite({ tabId, url, timeStamp });
5918
});

src/helpers/block-site.ts

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import storage from "../storage";
2+
import findRule from "./find-rule";
3+
import * as counterHelper from "./counter";
4+
import getBlockedUrl from "./get-blocked-url";
5+
6+
interface BlockSiteOptions {
7+
tabId: number
8+
url: string
9+
timeStamp: number
10+
}
11+
12+
export default (options: BlockSiteOptions) => {
13+
const { tabId, url, timeStamp } = options;
14+
15+
storage.get(["enabled", "blocked"], ({ enabled, blocked }) => {
16+
if (!enabled || blocked.length === 0) {
17+
return;
18+
}
19+
20+
const foundRule = findRule(url, blocked);
21+
if (!foundRule || foundRule.type === "allow") {
22+
storage.get(["counter"], ({ counter }) => {
23+
counterHelper.flushObsoleteEntries({ blocked, counter });
24+
storage.set({ counter });
25+
});
26+
return;
27+
}
28+
29+
storage.get(["counter", "counterShow", "counterPeriod", "resolution"], ({ counter, counterShow, counterPeriod, resolution }) => {
30+
counterHelper.flushObsoleteEntries({ blocked, counter });
31+
const count = counterHelper.add(foundRule.path, timeStamp, {
32+
counter,
33+
countFromTimeStamp: counterHelper.counterPeriodToTimeStamp(counterPeriod, new Date().getTime()),
34+
});
35+
storage.set({ counter });
36+
37+
switch (resolution) {
38+
case "CLOSE_TAB":
39+
chrome.tabs.remove(tabId);
40+
break;
41+
case "SHOW_BLOCKED_INFO_PAGE": {
42+
chrome.tabs.update(tabId, {
43+
url: getBlockedUrl({
44+
url,
45+
rule: foundRule.path,
46+
countParams: counterShow ? { count, period: counterPeriod } : undefined },
47+
)},
48+
);
49+
break;
50+
}}
51+
});
52+
});
53+
};

src/helpers/create-context-menu.ts

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import storage from "../storage";
2+
import blockSite from "./block-site";
3+
import normalizeUrl from "./normalize-url";
4+
5+
const createContextMenu = () => {
6+
const parentId = chrome.contextMenus.create({
7+
id: "block_site",
8+
title: "Block Site",
9+
documentUrlPatterns: ["https://*/*", "http://*/*"],
10+
});
11+
12+
const blockThisSiteId = "block_this_site";
13+
chrome.contextMenus.create({
14+
parentId,
15+
id: blockThisSiteId,
16+
title: "Block this site",
17+
});
18+
19+
chrome.contextMenus.onClicked.addListener((info, tab) => {
20+
const tabId = tab?.id;
21+
if (!tabId || info.menuItemId !== blockThisSiteId) {
22+
return;
23+
}
24+
25+
storage.get(["blocked"], ({ blocked }) => {
26+
const url = info.pageUrl;
27+
const normalizedUrl = normalizeUrl(url);
28+
const updatedBlocked = [...blocked, normalizedUrl];
29+
30+
storage.set({ blocked: updatedBlocked }, () => {
31+
const timeStamp = Date.now();
32+
blockSite({ tabId, url, timeStamp });
33+
});
34+
});
35+
});
36+
};
37+
38+
export default () => {
39+
storage.get(["enabled"], ({ enabled }) => {
40+
chrome.contextMenus.removeAll(() => {
41+
if (enabled) {
42+
createContextMenu();
43+
}
44+
});
45+
46+
chrome.storage.local.onChanged.addListener((changes) => {
47+
if (changes["enabled"]) {
48+
chrome.contextMenus.removeAll(() => {
49+
if (changes["enabled"].newValue) {
50+
createContextMenu();
51+
}
52+
});
53+
}
54+
});
55+
});
56+
};

src/options.ts

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -81,13 +81,11 @@ window.addEventListener("DOMContentLoaded", () => {
8181
document.body.classList.add("ready");
8282
});
8383

84-
chrome.storage.onChanged.addListener((changes, namespace) => {
85-
if (namespace === "local") {
86-
keys.forEach((key) => {
87-
if (changes[key]) {
88-
UI.init({ [key]: changes[key].newValue });
89-
}
90-
});
91-
}
84+
chrome.storage.local.onChanged.addListener((changes) => {
85+
keys.forEach((key) => {
86+
if (changes[key]) {
87+
UI.init({ [key]: changes[key].newValue });
88+
}
89+
});
9290
});
9391
});

src/storage/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ import { Schema } from "./schema";
22

33
export * from "./schema";
44

5-
const set = <T extends Partial<Schema>>(items: T) => {
6-
chrome.storage.local.set(items);
5+
const set = <T extends Partial<Schema>>(items: T, callback?: () => void) => {
6+
chrome.storage.local.set(items, callback);
77
};
88

99
const get = <T extends keyof Schema>(keys: T[], callback: (items: Pick<Schema, T>) => void) => {

src/storage/init.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,13 @@ export const getRevisitedSchema = (local: Partial<Schema> | Record<string, unkno
1515
return revisitedSchema;
1616
};
1717

18-
export default () => {
18+
export default (callback: () => void) => {
1919
storage.getAll((local) => {
2020
const revisitedSchema = getRevisitedSchema(local);
2121
if (Object.keys(revisitedSchema).length) {
22-
storage.set(revisitedSchema);
22+
storage.set(revisitedSchema, callback);
23+
return;
2324
}
25+
callback();
2426
});
2527
};

0 commit comments

Comments
 (0)