-
Notifications
You must be signed in to change notification settings - Fork 12
/
background.js
93 lines (71 loc) · 2.4 KB
/
background.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
85
86
87
88
89
90
91
92
93
//https://developer.mozilla.org/en-US/Add-ons/WebExtensions/API/browsingData/removeCache
/*
Default settings. If there is nothing in storage, use these values.
*/
var defaultSettings = {
reload: true,
notification: true,
dataTypes: ["cache"]
};
/*
On startup, check whether we have stored settings.
If we don't, then store the default settings.
*/
function checkStoredSettings(storedSettings) {
if (storedSettings.notification == null
|| storedSettings.reload == null
|| storedSettings.dataTypes == null) {
browser.storage.local.set(defaultSettings);
}
}
const gettingStoredSettings = browser.storage.local.get();
gettingStoredSettings.then(checkStoredSettings, onError);
function clearCache(storedSettings) {
const reload = storedSettings.reload;
const notification = storedSettings.notification;
/*
Convert from an array of strings, representing data types,
to an object suitable for passing into browsingData.remove().
*/
function getTypes(selectedTypes) {
var dataTypes = {};
for (var item of selectedTypes) {
dataTypes[item] = true;
}
return dataTypes;
}
const dataTypes = getTypes(storedSettings.dataTypes);
function onCleared() {
//https://developer.mozilla.org/en-US/Add-ons/WebExtensions/API/tabs/reload
if (reload) {
browser.tabs.reload()
}
//https://developer.mozilla.org/en-US/Add-ons/WebExtensions/API/notifications
if (notification) {
if(Object.keys(dataTypes).length === 0) {
browser.notifications.create({
"type": "basic",
"title": "Clear Cache",
"message": "All data types have been disabled. Enable at least one data type in options",
"iconUrl": browser.runtime.getURL('/icons/broom.svg')
}).then(function() {});
} else {
var dataTypesString = Object.keys(dataTypes).join(", ");
browser.notifications.create({
"type": "basic",
"title": "Clear Cache",
"message": `Cleared ${dataTypesString}\n`,
"iconUrl": browser.runtime.getURL('/icons/broom.svg')
}).then(function() {});
}
}
}
browser.browsingData.remove({}, dataTypes).then(onCleared, onError);
}
function onError(error) {
console.error(error);
}
browser.browserAction.onClicked.addListener(() => {
const gettingStoredSettings = browser.storage.local.get();
gettingStoredSettings.then(clearCache, onError);
});