forked from WorldBrain/Memex
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.ts
42 lines (37 loc) · 1.22 KB
/
utils.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import browser from 'webextension-polyfill'
import { INPAGE_UI_BLACKLIST_STORAGE_KEY } from './constants'
import type { BlacklistEntry } from './types'
export const getUrlBlacklist = async (): Promise<BlacklistEntry[]> => {
const storageValues = await browser.storage.local.get(
INPAGE_UI_BLACKLIST_STORAGE_KEY,
)
return storageValues[INPAGE_UI_BLACKLIST_STORAGE_KEY] ?? []
}
export const checkPageBlacklisted = async (
fullPageUrl: string,
): Promise<boolean> => {
const urlBlacklist = await getUrlBlacklist()
for (const { expression } of urlBlacklist) {
if (fullPageUrl.match(expression)) {
return true
}
}
return false
}
export const addUrlToBlacklist = async (pageUrl: string): Promise<void> => {
const urlBlacklist = await getUrlBlacklist()
pageUrl = pageUrl.replace(/\s+/g, '').replace('.', '\\.')
const isAlreadyAdded = urlBlacklist.reduce(
(prev, curr) => prev || curr.expression === pageUrl,
false,
)
if (isAlreadyAdded) {
return
}
await browser.storage.local.set({
[INPAGE_UI_BLACKLIST_STORAGE_KEY]: [
...urlBlacklist,
{ expression: pageUrl, dateAdded: Date.now() },
],
})
}