-
Notifications
You must be signed in to change notification settings - Fork 40
/
ignore.js
84 lines (72 loc) · 2.05 KB
/
ignore.js
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import browser from './browser-api'
import * as utilities from './utilities'
import { extractDomainFromUrl } from './utilities'
export class Ignore {
/**
* Clears the list of ignored domains.
* @returns {Promise<undefined>}
*/
async clear () {
await browser.storage.local.set({ ignoredHosts: [] })
}
/**
* Returns the list of all ignored domains.
* @returns {Promise<string[]>}
*/
async getAll () {
const { ignoredHosts } =
await browser.storage.local.get({ ignoredHosts: [] })
return ignoredHosts
}
/**
* Adds a given URL to the list of ignored.
* @param url URL to ignore.
* @returns {Promise<boolean>}
*/
async add (url) {
const hostname = extractDomainFromUrl(url)
const { ignoredHosts } =
await browser.storage.local.get({ ignoredHosts: [] })
if (!ignoredHosts.includes(hostname)) {
ignoredHosts.push(hostname)
console.warn(`Adding ${hostname} to ignore`)
await browser.storage.local.set({ ignoredHosts })
}
return true
}
async set (ignoredHosts = []) {
await browser.storage.local.set({ ignoredHosts })
}
/**
* Removes a URL/Hostname from the list of ignored.
* @param url URL to remove.
* @returns {Promise<boolean>}
*/
async remove (url) {
const hostname = extractDomainFromUrl(url)
const { ignoredHosts } =
await browser.storage.local.get({ ignoredHosts: [] })
if (ignoredHosts.includes(hostname)) {
const index = ignoredHosts.indexOf(hostname)
ignoredHosts.splice(index, 1)
await browser.storage.local.set({ ignoredHosts })
console.warn(`Removing ${hostname} from ignore`)
}
return true
}
/**
* Checks if a given URL is ignored..
* @param url URL.
* @returns {Promise<boolean>}
*/
async contains (url) {
const ignoredHosts = await this.getAll()
const hostname = utilities.extractDomainFromUrl(url)
if (ignoredHosts.includes(hostname)) {
console.warn(`Ignoring host: ${hostname}`)
return true
}
return false
}
}
export default new Ignore()