-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathutils.js
132 lines (126 loc) · 4.07 KB
/
utils.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/* Copyright (C) 2023-2024 Diego Miguel Lozano <hello@diegomiguel.me>
*
* This program is free software: you can redistribute it and//or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
* For license information on the libraries used, see LICENSE.
*/
export { PreferencePrefix, Defaults, fetchSettings, getBangKey, getBangName };
// Prefixes added to the storage keys to differentiate between different types
// of settings.
const PreferencePrefix = Object.freeze({
BANG: "#bang#",
BANG_SYMBOL: "#symbol#",
SEARCH_ENGINE: "#engine#",
});
const Defaults = Object.freeze({
BANG_SYMBOL: "!",
});
async function fetchSettings(update = false) {
if (!update) {
// Check if any settings are loaded. We look for, e.g., the bang symbol key.
// If no settings are loaded, we fetch them and retry. A more elegant
// solution would be `StorageArea.getBytesInUse()`, but it's not supported
// for `storage.session`.
const updated = await browser.storage.session
.get(PreferencePrefix.BANG_SYMBOL)
.then(
function onGot(item) {
// Any matches?
return Object.hasOwn(item, PreferencePrefix.BANG_SYMBOL);
},
function onError(error) {
// TODO: Handle error.
},
);
if (updated) {
return null;
}
}
const settings = {};
let defaultBangs = [];
const bangApis = [
"https://raw.githubusercontent.com/kagisearch/bangs/main/data/bangs.json",
"https://duckduckgo.com/bang.js",
];
for (const api of bangApis) {
try {
const res = await fetch(new Request(api));
defaultBangs = await res.json();
break;
} catch (error) {
// retry
}
}
for (const bang of defaultBangs) {
// Kagi does not specify the origin for its own bangs, so we add it.
if (bang.d === "kagi.com" && bang.u.startsWith("/")) {
bang.u = `https://kagi.com${bang.u}`;
}
const bangTargets = [
{
url: bang.u,
baseUrl:
Object.hasOwn(bang, "fmt") && !bang.fmt.includes("open_base_path")
? null
: new URL(bang.u).origin,
urlEncodeQuery: Object.hasOwn(bang, "fmt")
? bang.fmt.includes("url_encode_placeholder")
: true,
},
];
settings[getBangKey(bang.t)] = bangTargets;
}
// Exceptions for DDG (unfortunately, default bangs do not expose this info).
settings[getBangKey("wayback")][0].urlEncodeQuery = false;
settings[getBangKey("waybackmachine")][0].urlEncodeQuery = false;
// Fetch custom bangs.
await browser.storage.sync.get().then(
function onGot(storedSettings) {
for (const [key, item] of Object.entries(storedSettings)) {
// In the session storage, for the bangs we only need the targets.
settings[key] = key.startsWith(PreferencePrefix.BANG)
? item.targets
: item;
}
if (
!Object.hasOwn(settings, PreferencePrefix.BANG_SYMBOL) ||
!settings[PreferencePrefix.BANG_SYMBOL]
) {
settings[PreferencePrefix.BANG_SYMBOL] = Defaults.BANG_SYMBOL;
}
browser.storage.session.clear().then(
function onCleared() {
browser.storage.session.set(settings);
},
function onError(error) {},
);
},
function onError(error) {
// TODO: Handle error.
},
);
return null;
}
function getBangKey(bang) {
if (bang == null) {
return null;
}
return `${PreferencePrefix.BANG}${bang.toLowerCase()}`;
}
function getBangName(bangKey) {
if (bangKey == null) {
return null;
}
return bangKey.slice(PreferencePrefix.BANG.length);
}