chemicaljs 1.7.4 → 1.8.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/client/chemical.components.js +131 -121
- package/client/chemical.js +358 -296
- package/client/chemical.sw.js +32 -21
- package/config/meteor/meteor.config.js +13 -0
- package/config/scramjet/scramjet.config.js +8 -8
- package/config/uv/uv.config.js +9 -9
- package/index.js +460 -272
- package/package.json +7 -3
package/client/chemical.js
CHANGED
|
@@ -1,354 +1,416 @@
|
|
|
1
1
|
window.chemical = {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
2
|
+
loaded: false,
|
|
3
|
+
transport: document.currentScript.dataset.transport || "libcurl",
|
|
4
|
+
wisp:
|
|
5
|
+
document.currentScript.dataset.wisp ||
|
|
6
|
+
(location.protocol === "https:" ? "wss" : "ws") +
|
|
7
|
+
"://" +
|
|
8
|
+
location.host +
|
|
9
|
+
"/wisp/",
|
|
10
|
+
};
|
|
6
11
|
|
|
7
12
|
function rammerheadEncode(baseUrl, decode = false) {
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
const generateDictionary = function () {
|
|
14
|
-
let str = "";
|
|
15
|
-
const split = baseDictionary.split("");
|
|
16
|
-
while (split.length > 0) {
|
|
17
|
-
str += split.splice(Math.floor(Math.random() * split.length), 1)[0];
|
|
18
|
-
}
|
|
19
|
-
return str;
|
|
20
|
-
};
|
|
13
|
+
const mod = (n, m) => ((n % m) + m) % m;
|
|
14
|
+
const baseDictionary =
|
|
15
|
+
"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz~-";
|
|
16
|
+
const shuffledIndicator = "_rhs";
|
|
21
17
|
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
18
|
+
const generateDictionary = function () {
|
|
19
|
+
let str = "";
|
|
20
|
+
const split = baseDictionary.split("");
|
|
21
|
+
while (split.length > 0) {
|
|
22
|
+
str += split.splice(Math.floor(Math.random() * split.length), 1)[0];
|
|
23
|
+
}
|
|
24
|
+
return str;
|
|
25
|
+
};
|
|
26
26
|
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
let shuffledStr = "";
|
|
32
|
-
for (let i = 0; i < str.length; i++) {
|
|
33
|
-
const char = str.charAt(i);
|
|
34
|
-
const idx = baseDictionary.indexOf(char);
|
|
35
|
-
if (char === "%" && str.length - i >= 3) {
|
|
36
|
-
shuffledStr += char;
|
|
37
|
-
shuffledStr += str.charAt(++i);
|
|
38
|
-
shuffledStr += str.charAt(++i);
|
|
39
|
-
} else if (idx === -1) {
|
|
40
|
-
shuffledStr += char;
|
|
41
|
-
} else {
|
|
42
|
-
shuffledStr += this.dictionary.charAt(
|
|
43
|
-
mod(idx + i, baseDictionary.length)
|
|
44
|
-
);
|
|
45
|
-
}
|
|
46
|
-
}
|
|
47
|
-
return shuffledIndicator + shuffledStr;
|
|
48
|
-
}
|
|
27
|
+
class StrShuffler {
|
|
28
|
+
constructor(dictionary = generateDictionary()) {
|
|
29
|
+
this.dictionary = dictionary;
|
|
30
|
+
}
|
|
49
31
|
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
unshuffledStr += baseDictionary.charAt(
|
|
69
|
-
mod(idx - i, baseDictionary.length)
|
|
70
|
-
);
|
|
71
|
-
}
|
|
72
|
-
}
|
|
73
|
-
return unshuffledStr;
|
|
32
|
+
shuffle(str) {
|
|
33
|
+
if (str.startsWith(shuffledIndicator)) {
|
|
34
|
+
return str;
|
|
35
|
+
}
|
|
36
|
+
let shuffledStr = "";
|
|
37
|
+
for (let i = 0; i < str.length; i++) {
|
|
38
|
+
const char = str.charAt(i);
|
|
39
|
+
const idx = baseDictionary.indexOf(char);
|
|
40
|
+
if (char === "%" && str.length - i >= 3) {
|
|
41
|
+
shuffledStr += char;
|
|
42
|
+
shuffledStr += str.charAt(++i);
|
|
43
|
+
shuffledStr += str.charAt(++i);
|
|
44
|
+
} else if (idx === -1) {
|
|
45
|
+
shuffledStr += char;
|
|
46
|
+
} else {
|
|
47
|
+
shuffledStr += this.dictionary.charAt(
|
|
48
|
+
mod(idx + i, baseDictionary.length)
|
|
49
|
+
);
|
|
74
50
|
}
|
|
51
|
+
}
|
|
52
|
+
return shuffledIndicator + shuffledStr;
|
|
75
53
|
}
|
|
76
54
|
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
55
|
+
unshuffle(str) {
|
|
56
|
+
if (!str.startsWith(shuffledIndicator)) {
|
|
57
|
+
return str;
|
|
58
|
+
}
|
|
81
59
|
|
|
82
|
-
|
|
83
|
-
if (!shush) console.log("Cannot communicate with the server");
|
|
84
|
-
};
|
|
85
|
-
request.onload = function () {
|
|
86
|
-
if (request.status === 200) {
|
|
87
|
-
callback(request.responseText);
|
|
88
|
-
} else {
|
|
89
|
-
if (!shush)
|
|
90
|
-
console.log(
|
|
91
|
-
'unexpected server response to not match "200". Server says "' +
|
|
92
|
-
request.responseText +
|
|
93
|
-
'"'
|
|
94
|
-
);
|
|
95
|
-
}
|
|
96
|
-
};
|
|
97
|
-
}
|
|
60
|
+
str = str.slice(shuffledIndicator.length);
|
|
98
61
|
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
callback(JSON.parse(res));
|
|
114
|
-
});
|
|
62
|
+
let unshuffledStr = "";
|
|
63
|
+
for (let i = 0; i < str.length; i++) {
|
|
64
|
+
const char = str.charAt(i);
|
|
65
|
+
const idx = this.dictionary.indexOf(char);
|
|
66
|
+
if (char === "%" && str.length - i >= 3) {
|
|
67
|
+
unshuffledStr += char;
|
|
68
|
+
unshuffledStr += str.charAt(++i);
|
|
69
|
+
unshuffledStr += str.charAt(++i);
|
|
70
|
+
} else if (idx === -1) {
|
|
71
|
+
unshuffledStr += char;
|
|
72
|
+
} else {
|
|
73
|
+
unshuffledStr += baseDictionary.charAt(
|
|
74
|
+
mod(idx - i, baseDictionary.length)
|
|
75
|
+
);
|
|
115
76
|
}
|
|
116
|
-
|
|
77
|
+
}
|
|
78
|
+
return unshuffledStr;
|
|
79
|
+
}
|
|
80
|
+
}
|
|
117
81
|
|
|
118
|
-
|
|
119
|
-
var
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
var data = JSON.parse(rawData);
|
|
126
|
-
if (!Array.isArray(data)) throw "getout";
|
|
127
|
-
return data;
|
|
128
|
-
} catch (e) {
|
|
129
|
-
return [];
|
|
130
|
-
}
|
|
131
|
-
},
|
|
132
|
-
set(data) {
|
|
133
|
-
if (!data || !Array.isArray(data)) throw new TypeError("must be array");
|
|
134
|
-
localStorage.setItem(localStorageKey, JSON.stringify(data));
|
|
135
|
-
},
|
|
136
|
-
getDefault() {
|
|
137
|
-
var sessionId = localStorage.getItem(localStorageKeyDefault);
|
|
138
|
-
if (sessionId) {
|
|
139
|
-
var data = sessionIdsStore.get();
|
|
140
|
-
data.filter(function (e) {
|
|
141
|
-
return e.id === sessionId;
|
|
142
|
-
});
|
|
143
|
-
if (data.length) return data[0];
|
|
144
|
-
}
|
|
145
|
-
return null;
|
|
146
|
-
},
|
|
147
|
-
setDefault(id) {
|
|
148
|
-
localStorage.setItem(localStorageKeyDefault, id);
|
|
149
|
-
}
|
|
82
|
+
function get(url, callback, shush = false) {
|
|
83
|
+
var request = new XMLHttpRequest();
|
|
84
|
+
request.open("GET", url, true);
|
|
85
|
+
request.send();
|
|
86
|
+
|
|
87
|
+
request.onerror = function () {
|
|
88
|
+
if (!shush) console.log("Cannot communicate with the server");
|
|
150
89
|
};
|
|
90
|
+
request.onload = function () {
|
|
91
|
+
if (request.status === 200) {
|
|
92
|
+
callback(request.responseText);
|
|
93
|
+
} else {
|
|
94
|
+
if (!shush)
|
|
95
|
+
console.log(
|
|
96
|
+
'unexpected server response to not match "200". Server says "' +
|
|
97
|
+
request.responseText +
|
|
98
|
+
'"'
|
|
99
|
+
);
|
|
100
|
+
}
|
|
101
|
+
};
|
|
102
|
+
}
|
|
151
103
|
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
104
|
+
var api = {
|
|
105
|
+
newsession(callback) {
|
|
106
|
+
get("/newsession", callback);
|
|
107
|
+
},
|
|
108
|
+
sessionexists(id, callback) {
|
|
109
|
+
get("/sessionexists?id=" + encodeURIComponent(id), function (res) {
|
|
110
|
+
if (res === "exists") return callback(true);
|
|
111
|
+
if (res === "not found") return callback(false);
|
|
112
|
+
console.log("unexpected response from server. received" + res);
|
|
113
|
+
});
|
|
114
|
+
},
|
|
115
|
+
shuffleDict(id, callback) {
|
|
116
|
+
console.log("Shuffling", id);
|
|
117
|
+
get("/api/shuffleDict?id=" + encodeURIComponent(id), function (res) {
|
|
118
|
+
callback(JSON.parse(res));
|
|
119
|
+
});
|
|
120
|
+
},
|
|
121
|
+
};
|
|
157
122
|
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
123
|
+
var localStorageKey = "rammerhead_sessionids";
|
|
124
|
+
var localStorageKeyDefault = "rammerhead_default_sessionid";
|
|
125
|
+
var sessionIdsStore = {
|
|
126
|
+
get() {
|
|
127
|
+
var rawData = localStorage.getItem(localStorageKey);
|
|
128
|
+
if (!rawData) return [];
|
|
129
|
+
try {
|
|
130
|
+
var data = JSON.parse(rawData);
|
|
131
|
+
if (!Array.isArray(data)) throw "getout";
|
|
132
|
+
return data;
|
|
133
|
+
} catch (e) {
|
|
134
|
+
return [];
|
|
135
|
+
}
|
|
136
|
+
},
|
|
137
|
+
set(data) {
|
|
138
|
+
if (!data || !Array.isArray(data)) throw new TypeError("must be array");
|
|
139
|
+
localStorage.setItem(localStorageKey, JSON.stringify(data));
|
|
140
|
+
},
|
|
141
|
+
getDefault() {
|
|
142
|
+
var sessionId = localStorage.getItem(localStorageKeyDefault);
|
|
143
|
+
if (sessionId) {
|
|
144
|
+
var data = sessionIdsStore.get();
|
|
145
|
+
data.filter(function (e) {
|
|
146
|
+
return e.id === sessionId;
|
|
175
147
|
});
|
|
176
|
-
|
|
148
|
+
if (data.length) return data[0];
|
|
149
|
+
}
|
|
150
|
+
return null;
|
|
151
|
+
},
|
|
152
|
+
setDefault(id) {
|
|
153
|
+
localStorage.setItem(localStorageKeyDefault, id);
|
|
154
|
+
},
|
|
155
|
+
};
|
|
177
156
|
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
157
|
+
function addSession(id) {
|
|
158
|
+
var data = sessionIdsStore.get();
|
|
159
|
+
data.unshift({ id: id, createdOn: new Date().toLocaleString() });
|
|
160
|
+
sessionIdsStore.set(data);
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
function getSessionId() {
|
|
164
|
+
return new Promise((resolve) => {
|
|
165
|
+
var id = localStorage.getItem("session-string");
|
|
166
|
+
api.sessionexists(id, function (value) {
|
|
167
|
+
if (!value) {
|
|
168
|
+
console.log("Session validation failed");
|
|
169
|
+
api.newsession(function (id) {
|
|
170
|
+
addSession(id);
|
|
171
|
+
localStorage.setItem("session-string", id);
|
|
172
|
+
console.log(id);
|
|
173
|
+
console.log("^ new id");
|
|
174
|
+
resolve(id);
|
|
175
|
+
});
|
|
176
|
+
} else {
|
|
177
|
+
resolve(id);
|
|
178
|
+
}
|
|
179
|
+
});
|
|
192
180
|
});
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
var ProxyHref;
|
|
184
|
+
|
|
185
|
+
return getSessionId().then((id) => {
|
|
186
|
+
return new Promise((resolve) => {
|
|
187
|
+
api.shuffleDict(id, function (shuffleDict) {
|
|
188
|
+
var shuffler = new StrShuffler(shuffleDict);
|
|
189
|
+
if (decode) {
|
|
190
|
+
ProxyHref = shuffler.unshuffle(baseUrl.split(id + "/")[1]);
|
|
191
|
+
} else {
|
|
192
|
+
ProxyHref = "/" + id + "/" + shuffler.shuffle(baseUrl);
|
|
193
|
+
}
|
|
194
|
+
resolve(ProxyHref);
|
|
195
|
+
});
|
|
196
|
+
});
|
|
197
|
+
});
|
|
193
198
|
}
|
|
194
199
|
|
|
195
200
|
async function encodeService(url, service) {
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
201
|
+
switch (service) {
|
|
202
|
+
case "uv":
|
|
203
|
+
if (uvEnabled) {
|
|
204
|
+
return (
|
|
205
|
+
window.location.origin +
|
|
206
|
+
__uv$config.prefix +
|
|
207
|
+
__uv$config.encodeUrl(url)
|
|
208
|
+
);
|
|
209
|
+
}
|
|
210
|
+
break;
|
|
211
|
+
case "rammerhead":
|
|
212
|
+
if (rammerheadEnabled) {
|
|
213
|
+
return window.location.origin + (await rammerheadEncode(url));
|
|
214
|
+
}
|
|
215
|
+
break;
|
|
216
|
+
case "scramjet":
|
|
217
|
+
if (scramjetEnabled) {
|
|
218
|
+
return (
|
|
219
|
+
window.location.origin +
|
|
220
|
+
__scramjet$config.prefix +
|
|
221
|
+
__scramjet$config.codec.encode(url)
|
|
222
|
+
);
|
|
223
|
+
}
|
|
224
|
+
break;
|
|
225
|
+
case "meteor":
|
|
226
|
+
if (meteorEnabled) {
|
|
227
|
+
return (
|
|
228
|
+
window.location.origin +
|
|
229
|
+
$meteor_config.prefix +
|
|
230
|
+
$meteor_config.codec.encode(url)
|
|
231
|
+
);
|
|
232
|
+
}
|
|
233
|
+
break;
|
|
234
|
+
}
|
|
221
235
|
}
|
|
222
236
|
|
|
223
237
|
window.chemical.encode = async function (url, config) {
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
238
|
+
if (!config || typeof config !== "object" || Array.isArray(config)) {
|
|
239
|
+
config = {
|
|
240
|
+
service: defaultService,
|
|
241
|
+
autoHttps: false,
|
|
242
|
+
};
|
|
243
|
+
}
|
|
230
244
|
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
245
|
+
if (config.service == undefined) {
|
|
246
|
+
config.service = defaultService;
|
|
247
|
+
}
|
|
234
248
|
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
249
|
+
if (config.autoHttps == undefined) {
|
|
250
|
+
config.autoHttps = false;
|
|
251
|
+
}
|
|
238
252
|
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
253
|
+
if (url.match(/^https?:\/\//)) {
|
|
254
|
+
return await encodeService(url, config.service);
|
|
255
|
+
} else if (
|
|
256
|
+
config.autoHttps == true &&
|
|
257
|
+
url.includes(".") &&
|
|
258
|
+
!url.includes(" ")
|
|
259
|
+
) {
|
|
260
|
+
return await encodeService("https://" + url, config.service);
|
|
261
|
+
} else if (config.searchEngine) {
|
|
262
|
+
return await encodeService(
|
|
263
|
+
config.searchEngine.replace("%s", encodeURIComponent(url)),
|
|
264
|
+
config.service
|
|
265
|
+
);
|
|
266
|
+
} else {
|
|
267
|
+
return await encodeService(url, config.service);
|
|
268
|
+
}
|
|
269
|
+
};
|
|
249
270
|
|
|
250
271
|
window.chemical.decode = async function (url, config) {
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
272
|
+
if (!config || typeof config !== "object" || Array.isArray(config)) {
|
|
273
|
+
config = {
|
|
274
|
+
service: defaultService,
|
|
275
|
+
};
|
|
276
|
+
}
|
|
256
277
|
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
278
|
+
switch (config.service) {
|
|
279
|
+
case "uv":
|
|
280
|
+
if (uvEnabled) {
|
|
281
|
+
return __uv$config.decodeUrl(url.split(__uv$config.prefix)[1]);
|
|
282
|
+
}
|
|
283
|
+
break;
|
|
284
|
+
case "rammerhead":
|
|
285
|
+
if (rammerheadEnabled) {
|
|
286
|
+
return await rammerheadEncode(
|
|
287
|
+
url.split(window.location.origin)[1],
|
|
288
|
+
true
|
|
289
|
+
);
|
|
290
|
+
}
|
|
291
|
+
break;
|
|
292
|
+
case "scramjet":
|
|
293
|
+
if (scramjetEnabled) {
|
|
294
|
+
return __scramjet$config.codec.decode(
|
|
295
|
+
url.split(__scramjet$config.prefix)[1]
|
|
296
|
+
);
|
|
297
|
+
}
|
|
298
|
+
break;
|
|
299
|
+
case "meteor":
|
|
300
|
+
if (meteorEnabled) {
|
|
301
|
+
return $meteor_config.codec.decode(url.split($meteor_config.prefix)[1]);
|
|
302
|
+
}
|
|
303
|
+
break;
|
|
304
|
+
}
|
|
305
|
+
};
|
|
275
306
|
|
|
276
307
|
function getTransport(transport) {
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
308
|
+
switch (transport) {
|
|
309
|
+
default:
|
|
310
|
+
case "libcurl":
|
|
311
|
+
return "/libcurl/index.mjs";
|
|
312
|
+
break;
|
|
313
|
+
case "epoxy":
|
|
314
|
+
return "/epoxy/index.mjs";
|
|
315
|
+
break;
|
|
316
|
+
}
|
|
286
317
|
}
|
|
287
318
|
|
|
288
|
-
window.chemical.setTransport = async function (
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
319
|
+
window.chemical.setTransport = async function (
|
|
320
|
+
newTransport = window.chemical.transport
|
|
321
|
+
) {
|
|
322
|
+
await window.chemical.connection.setTransport(getTransport(newTransport), [
|
|
323
|
+
{ wisp: window.chemical.wisp },
|
|
324
|
+
]);
|
|
325
|
+
window.chemical.transport = newTransport;
|
|
326
|
+
};
|
|
292
327
|
|
|
293
328
|
window.chemical.setWisp = async function (wisp = window.chemical.wisp) {
|
|
294
|
-
|
|
295
|
-
window.chemical.
|
|
296
|
-
}
|
|
329
|
+
await window.chemical.connection.setTransport(
|
|
330
|
+
getTransport(window.chemical.transport),
|
|
331
|
+
[{ wisp: wisp }]
|
|
332
|
+
);
|
|
333
|
+
window.chemical.wisp = wisp;
|
|
334
|
+
};
|
|
297
335
|
|
|
298
336
|
async function registerSW() {
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
337
|
+
if ("serviceWorker" in navigator) {
|
|
338
|
+
await navigator.serviceWorker.register("/chemical.sw.js");
|
|
339
|
+
} else {
|
|
340
|
+
console.error("Service worker failed to register.");
|
|
341
|
+
}
|
|
304
342
|
}
|
|
305
343
|
|
|
306
344
|
async function loadScript(src) {
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
345
|
+
await new Promise((resolve, reject) => {
|
|
346
|
+
const script = document.createElement("script");
|
|
347
|
+
script.src = src;
|
|
348
|
+
script.onload = () => {
|
|
349
|
+
resolve();
|
|
350
|
+
};
|
|
351
|
+
script.onerror = () => {
|
|
352
|
+
reject();
|
|
353
|
+
};
|
|
354
|
+
document.head.appendChild(script);
|
|
355
|
+
});
|
|
318
356
|
}
|
|
319
357
|
|
|
320
358
|
function setupFetch() {
|
|
321
|
-
|
|
322
|
-
|
|
359
|
+
const client = new window.BareMux.BareClient();
|
|
360
|
+
window.chemical.fetch = client.fetch.bind(client);
|
|
323
361
|
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
362
|
+
window.chemical.getSuggestions = async function (query) {
|
|
363
|
+
if (!query) {
|
|
364
|
+
return [];
|
|
365
|
+
}
|
|
328
366
|
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
}
|
|
367
|
+
try {
|
|
368
|
+
const DDGSuggestions = await window.chemical.fetch(
|
|
369
|
+
"https://duckduckgo.com/ac/?q=" + query + "&type=list"
|
|
370
|
+
);
|
|
371
|
+
const suggestions = await DDGSuggestions.json();
|
|
372
|
+
return suggestions[1].slice(0, 9);
|
|
373
|
+
} catch (err) {
|
|
374
|
+
console.error(err);
|
|
375
|
+
return [];
|
|
339
376
|
}
|
|
377
|
+
};
|
|
378
|
+
|
|
379
|
+
window.chemical.createDataURL = async function (url) {
|
|
380
|
+
return new Promise(async (resolve, reject) => {
|
|
381
|
+
try {
|
|
382
|
+
const response = await window.chemical.fetch(url);
|
|
383
|
+
const blob = await response.blob();
|
|
384
|
+
const reader = new FileReader();
|
|
385
|
+
|
|
386
|
+
reader.onloadend = function () {
|
|
387
|
+
resolve(reader.result);
|
|
388
|
+
};
|
|
389
|
+
|
|
390
|
+
reader.readAsDataURL(blob);
|
|
391
|
+
} catch {
|
|
392
|
+
resolve(undefined);
|
|
393
|
+
}
|
|
394
|
+
});
|
|
395
|
+
};
|
|
340
396
|
}
|
|
341
397
|
|
|
342
398
|
await loadScript("/baremux/index.js");
|
|
343
399
|
if (uvEnabled) {
|
|
344
|
-
|
|
345
|
-
|
|
400
|
+
await loadScript("/uv/uv.bundle.js");
|
|
401
|
+
await loadScript("/uv/uv.config.js");
|
|
346
402
|
}
|
|
347
403
|
if (scramjetEnabled) {
|
|
348
|
-
|
|
349
|
-
|
|
404
|
+
await loadScript("/scramjet/scramjet.codecs.js");
|
|
405
|
+
await loadScript("/scramjet/scramjet.config.js");
|
|
406
|
+
}
|
|
407
|
+
if (meteorEnabled) {
|
|
408
|
+
await loadScript("/meteor/meteor.codecs.js");
|
|
409
|
+
await loadScript("/meteor/meteor.config.js");
|
|
350
410
|
}
|
|
351
|
-
window.chemical.connection = new window.BareMux.BareMuxConnection(
|
|
411
|
+
window.chemical.connection = new window.BareMux.BareMuxConnection(
|
|
412
|
+
"/baremux/worker.js"
|
|
413
|
+
);
|
|
352
414
|
await window.chemical.setTransport();
|
|
353
415
|
setupFetch();
|
|
354
416
|
await registerSW();
|