Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
titaniumnetwork-dev
GitHub Repository: titaniumnetwork-dev/Ultraviolet-App
Path: blob/main/public/search.js
120 views
1
"use strict";
2
/**
3
*
4
* @param {string} input
5
* @param {string} template Template for a search query.
6
* @returns {string} Fully qualified URL
7
*/
8
function search(input, template) {
9
try {
10
// input is a valid URL:
11
// eg: https://example.com, https://example.com/test?q=param
12
return new URL(input).toString();
13
} catch (err) {
14
// input was not a valid URL
15
}
16
17
try {
18
// input is a valid URL when http:// is added to the start:
19
// eg: example.com, https://example.com/test?q=param
20
const url = new URL(`http://${input}`);
21
// only if the hostname has a TLD/subdomain
22
if (url.hostname.includes(".")) return url.toString();
23
} catch (err) {
24
// input was not valid URL
25
}
26
27
// input may have been a valid URL, however the hostname was invalid
28
29
// Attempts to convert the input to a fully qualified URL have failed
30
// Treat the input as a search query
31
return template.replace("%s", encodeURIComponent(input));
32
}
33
34