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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
|
/**
* @license
* (C) Copyright Contributors to the SearXNG project.
* (C) Copyright Contributors to the searx project (2014 - 2021).
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
window.searxng = ((w, d) => {
// not invented here toolkit with bugs fixed elsewhere
// purposes : be just good enough and as small as possible
// from https://plainjs.com/javascript/events/live-binding-event-handlers-14/
if (w.Element) {
((ElementPrototype) => {
ElementPrototype.matches =
ElementPrototype.matches ||
ElementPrototype.matchesSelector ||
ElementPrototype.webkitMatchesSelector ||
ElementPrototype.msMatchesSelector ||
function (selector) {
const nodes = (this.parentNode || this.document).querySelectorAll(selector);
let i = -1;
while (nodes[++i] && nodes[i] !== this);
return !!nodes[i];
};
})(Element.prototype);
}
function callbackSafe(callback, el, e) {
try {
callback.call(el, e);
} catch (exception) {
console.log(exception);
}
}
const searxng = window.searxng || {};
searxng.on = (obj, eventType, callback, useCapture) => {
useCapture = useCapture || false;
if (typeof obj !== "string") {
// obj HTMLElement, HTMLDocument
obj.addEventListener(eventType, callback, useCapture);
} else {
// obj is a selector
d.addEventListener(
eventType,
(e) => {
let el = e.target || e.srcElement;
let found = false;
while (el?.matches && el !== d) {
found = el.matches(obj);
if (found) break;
el = el.parentElement;
}
if (found) {
callbackSafe(callback, el, e);
}
},
useCapture
);
}
};
searxng.ready = (callback) => {
if (document.readyState !== "loading") {
callback.call(w);
} else {
w.addEventListener("DOMContentLoaded", callback.bind(w));
}
};
searxng.http = (method, url, data = null) =>
new Promise((resolve, reject) => {
try {
const req = new XMLHttpRequest();
req.open(method, url, true);
req.timeout = 20000;
// On load
req.onload = () => {
if (req.status === 200) {
resolve(req.response, req.responseType);
} else {
reject(Error(req.statusText));
}
};
// Handle network errors
req.onerror = () => {
reject(Error("Network Error"));
};
req.onabort = () => {
reject(Error("Transaction is aborted"));
};
req.ontimeout = () => {
reject(Error("Timeout"));
};
// Make the request
if (data) {
req.send(data);
} else {
req.send();
}
} catch (ex) {
reject(ex);
}
});
searxng.loadStyle = (src) => {
const path = `${searxng.settings.theme_static_path}/${src}`;
const id = `style_${src.replace(".", "_")}`;
let s = d.getElementById(id);
if (s === null) {
s = d.createElement("link");
s.setAttribute("id", id);
s.setAttribute("rel", "stylesheet");
s.setAttribute("type", "text/css");
s.setAttribute("href", path);
d.body.appendChild(s);
}
};
searxng.loadScript = (src, callback) => {
const path = `${searxng.settings.theme_static_path}/${src}`;
const id = `script_${src.replace(".", "_")}`;
let s = d.getElementById(id);
if (s === null) {
s = d.createElement("script");
s.setAttribute("id", id);
s.setAttribute("src", path);
s.onload = callback;
s.onerror = () => {
s.setAttribute("error", "1");
};
d.body.appendChild(s);
} else if (!s.hasAttribute("error")) {
try {
callback.apply(s, []);
} catch (exception) {
console.log(exception);
}
} else {
console.log(`callback not executed : script '${path}' not loaded.`);
}
};
searxng.insertBefore = (newNode, referenceNode) => {
referenceNode.parentNode.insertBefore(newNode, referenceNode);
};
searxng.insertAfter = (newNode, referenceNode) => {
referenceNode.parentNode.insertAfter(newNode, referenceNode.nextSibling);
};
searxng.on(".close", "click", function () {
this.parentNode.classList.add("invisible");
});
function getEndpoint() {
for (const className of d.getElementsByTagName("body")[0].classList.values()) {
if (className.endsWith("_endpoint")) {
return className.split("_")[0];
}
}
return "";
}
searxng.endpoint = getEndpoint();
return searxng;
})(window, document);
|