|
1 | 1 | const bangs = [ |
2 | | - { |
3 | | - t: "al", |
4 | | - u: "https://kiwix.tristanhavelick.com/search?content=archlinux_en_all_nopic_2022-05&pattern={{{s}}}", |
5 | | - }, |
6 | | - { |
7 | | - t: "cl", |
8 | | - u: "https://denver.craigslist.org/search/?query={{{s}}}", |
9 | | - }, |
10 | | - { |
11 | | - t: "d", |
12 | | - u: "https://lite.duckduckgo.com/lite?q={{{s}}}&kl=us-en", |
13 | | - }, |
14 | | - { |
15 | | - t: "e", |
16 | | - u: "https://www.ebay.com/sch/i.html?_nkw={{{s}}}&rt=nc&LH_ItemCondition=4", |
17 | | - }, |
18 | | - { |
19 | | - t: "jw", |
20 | | - u: "https://www.justwatch.com/us/search?q={{{s}}}", |
21 | | - }, |
22 | | - { |
23 | | - t: "k", |
24 | | - u: "https://kiwix.tristanhavelick.com/search?pattern={{{s}}}", |
25 | | - }, |
26 | | - { |
27 | | - t: "lg", |
28 | | - u: "http://libgen.is/search.php?req={{{s}}}", |
29 | | - }, |
30 | | - { |
31 | | - t: "lgf", |
32 | | - u: "http://libgen.is/fiction/?q={{{s}}}", |
33 | | - }, |
34 | | - { |
35 | | - t: "m", |
36 | | - u: "https://search.marginalia.nu/search?query={{{s}}}", |
37 | | - }, |
38 | | - { |
39 | | - t: "py", |
40 | | - u: "https://kiwix.tristanhavelick.com/search?content=python-3.10.2&pattern={{{s}}}", |
41 | | - }, |
42 | | - { |
43 | | - t: "sg", |
44 | | - u: "https://app.thestorygraph.com/browse?search_term={{{s}}}", |
45 | | - }, |
46 | | - { |
47 | | - t: "ytt", |
48 | | - u: "https://youtranscript.tristanhavelick.com/search?search_term={{{s}}}", |
49 | | - }, |
50 | | - { |
51 | | - t: "x", |
52 | | - u: "https://searxng.tristanhavelick.com/search?q={{{s}}}", |
53 | | - }, |
54 | | - { |
55 | | - t: "gh", |
56 | | - u: "https://github.com/search?q={{{s}}}", |
57 | | - }, |
58 | | - { |
59 | | - t: "g", |
60 | | - u: "https://www.google.com/search?q={{{s}}}", |
61 | | - }, |
62 | | - { |
63 | | - t: "ddg", |
64 | | - u: "https://duckduckgo.com/?q={{{s}}}", |
65 | | - }, |
66 | | - { |
67 | | - t: "a", |
68 | | - u: "https://www.amazon.com/s?k={{{s}}}", |
69 | | - }, |
70 | | - { |
71 | | - t: "pypi", |
72 | | - u: "https://pypi.org/search/?q={{{s}}}", |
73 | | - }, |
74 | | - { |
75 | | - t: "w", |
76 | | - u: "https://en.wikipedia.org/wiki/Special:Search?search={{{s}}}", |
77 | | - }, |
| 2 | + { |
| 3 | + t: "al", |
| 4 | + u: "https://kiwix.tristanhavelick.com/search?content=archlinux_en_all_nopic_2022-05&pattern={{{s}}}", |
| 5 | + }, |
| 6 | + { |
| 7 | + t: "cl", |
| 8 | + u: "https://denver.craigslist.org/search/?query={{{s}}}", |
| 9 | + }, |
| 10 | + { |
| 11 | + t: "d", |
| 12 | + u: "https://lite.duckduckgo.com/lite?q={{{s}}}&kl=us-en", |
| 13 | + }, |
| 14 | + { |
| 15 | + t: "e", |
| 16 | + u: "https://www.ebay.com/sch/i.html?_nkw={{{s}}}&rt=nc&LH_ItemCondition=4", |
| 17 | + }, |
| 18 | + { |
| 19 | + t: "jw", |
| 20 | + u: "https://www.justwatch.com/us/search?q={{{s}}}", |
| 21 | + }, |
| 22 | + { |
| 23 | + t: "k", |
| 24 | + u: "https://kiwix.tristanhavelick.com/search?pattern={{{s}}}", |
| 25 | + }, |
| 26 | + { |
| 27 | + t: "lg", |
| 28 | + u: "http://libgen.is/search.php?req={{{s}}}", |
| 29 | + }, |
| 30 | + { |
| 31 | + t: "lgf", |
| 32 | + u: "http://libgen.is/fiction/?q={{{s}}}", |
| 33 | + }, |
| 34 | + { |
| 35 | + t: "m", |
| 36 | + u: "https://search.marginalia.nu/search?query={{{s}}}", |
| 37 | + }, |
| 38 | + { |
| 39 | + t: "py", |
| 40 | + u: "https://kiwix.tristanhavelick.com/search?content=python-3.10.2&pattern={{{s}}}", |
| 41 | + }, |
| 42 | + { |
| 43 | + t: "sg", |
| 44 | + u: "https://app.thestorygraph.com/browse?search_term={{{s}}}", |
| 45 | + }, |
| 46 | + { |
| 47 | + t: "ytt", |
| 48 | + u: "https://youtranscript.tristanhavelick.com/search?search_term={{{s}}}", |
| 49 | + }, |
| 50 | + { |
| 51 | + t: "x", |
| 52 | + u: "https://searxng.tristanhavelick.com/search?q={{{s}}}", |
| 53 | + }, |
| 54 | + { |
| 55 | + t: "gh", |
| 56 | + u: "https://github.com/search?q={{{s}}}", |
| 57 | + }, |
| 58 | + { |
| 59 | + t: "g", |
| 60 | + u: "https://www.google.com/search?q={{{s}}}", |
| 61 | + }, |
| 62 | + { |
| 63 | + t: "ddg", |
| 64 | + u: "https://duckduckgo.com/?q={{{s}}}", |
| 65 | + }, |
| 66 | + { |
| 67 | + t: "a", |
| 68 | + u: "https://www.amazon.com/s?k={{{s}}}", |
| 69 | + }, |
| 70 | + { |
| 71 | + t: "pypi", |
| 72 | + u: "https://pypi.org/search/?q={{{s}}}", |
| 73 | + }, |
| 74 | + { |
| 75 | + t: "w", |
| 76 | + u: "https://en.wikipedia.org/wiki/Special:Search?search={{{s}}}", |
| 77 | + }, |
78 | 78 | ]; |
79 | 79 |
|
80 | 80 | function processBang(query) { |
81 | | - const trimmed = query.trim(); |
| 81 | + const trimmed = query.trim(); |
82 | 82 |
|
83 | | - if (trimmed.startsWith("!")) { |
84 | | - const spaceIndex = trimmed.indexOf(" "); |
85 | | - if (spaceIndex === -1) { |
86 | | - return null; |
87 | | - } |
| 83 | + if (trimmed.startsWith("!")) { |
| 84 | + const spaceIndex = trimmed.indexOf(" "); |
| 85 | + if (spaceIndex === -1) { |
| 86 | + return null; |
| 87 | + } |
88 | 88 |
|
89 | | - const bangTag = trimmed.substring(1, spaceIndex); |
90 | | - const searchTerm = trimmed.substring(spaceIndex + 1).trim(); |
| 89 | + const bangTag = trimmed.substring(1, spaceIndex); |
| 90 | + const searchTerm = trimmed.substring(spaceIndex + 1).trim(); |
91 | 91 |
|
92 | | - const bang = bangs.find((b) => b.t === bangTag); |
93 | | - if (bang) { |
94 | | - return bang.u.replace(/{{{s}}}/g, encodeURIComponent(searchTerm)); |
95 | | - } |
96 | | - } |
97 | | - const bangMatch = trimmed.match(/^(.+)\s+(\w+)!$/); |
98 | | - if (bangMatch) { |
99 | | - const searchTerm = bangMatch[1].trim(); |
100 | | - const bangTag = bangMatch[2]; |
| 92 | + const bang = bangs.find((b) => b.t === bangTag); |
| 93 | + if (bang) { |
| 94 | + return bang.u.replace(/{{{s}}}/g, encodeURIComponent(searchTerm)); |
| 95 | + } |
| 96 | + } |
| 97 | + const bangMatch = trimmed.match(/^(.+)\s+(\w+)!$/); |
| 98 | + if (bangMatch) { |
| 99 | + const searchTerm = bangMatch[1].trim(); |
| 100 | + const bangTag = bangMatch[2]; |
101 | 101 |
|
102 | | - const bang = bangs.find((b) => b.t === bangTag); |
103 | | - if (bang) { |
104 | | - return bang.u.replace(/{{{s}}}/g, encodeURIComponent(searchTerm)); |
105 | | - } |
106 | | - } |
107 | | - return `https://lite.duckduckgo.com/lite?q=${encodeURIComponent(trimmed)}&kl=us-en`; |
| 102 | + const bang = bangs.find((b) => b.t === bangTag); |
| 103 | + if (bang) { |
| 104 | + return bang.u.replace(/{{{s}}}/g, encodeURIComponent(searchTerm)); |
| 105 | + } |
| 106 | + } |
| 107 | + return `https://lite.duckduckgo.com/lite?q=${encodeURIComponent(trimmed)}&kl=us-en`; |
108 | 108 | } |
109 | 109 |
|
110 | 110 | function getQueryParam(key) { |
111 | | - const urlParams = new URLSearchParams(window.location.search); |
112 | | - return urlParams.get(key); |
| 111 | + const urlParams = new URLSearchParams(window.location.search); |
| 112 | + return urlParams.get(key); |
113 | 113 | } |
114 | 114 |
|
115 | 115 | function redirect(url) { |
116 | | - window.location.href = url; |
| 116 | + window.location.href = url; |
117 | 117 | } |
118 | 118 |
|
119 | 119 | function setupUI() { |
120 | | - const searchInput = document.getElementById("searchInput"); |
| 120 | + const searchInput = document.getElementById("searchInput"); |
121 | 121 |
|
122 | | - function performSearch() { |
123 | | - const query = searchInput.value; |
124 | | - const url = processBang(query); |
125 | | - if (url) { |
126 | | - window.location.hash = `#q=${encodeURIComponent(query)}`; |
127 | | - redirect(url); |
128 | | - } |
129 | | - } |
| 122 | + function performSearch() { |
| 123 | + const query = searchInput.value; |
| 124 | + const url = processBang(query); |
| 125 | + if (url) { |
| 126 | + window.location.hash = `#q=${encodeURIComponent(query)}`; |
| 127 | + redirect(url); |
| 128 | + } |
| 129 | + } |
130 | 130 |
|
131 | | - document |
132 | | - .getElementById("searchButton") |
133 | | - .addEventListener("click", performSearch); |
| 131 | + document |
| 132 | + .getElementById("searchButton") |
| 133 | + .addEventListener("click", performSearch); |
134 | 134 |
|
135 | | - searchInput.addEventListener("keypress", (e) => { |
136 | | - if (e.key === "Enter") { |
137 | | - e.preventDefault(); |
138 | | - performSearch(); |
139 | | - } |
140 | | - }); |
| 135 | + searchInput.addEventListener("keypress", (e) => { |
| 136 | + if (e.key === "Enter") { |
| 137 | + e.preventDefault(); |
| 138 | + performSearch(); |
| 139 | + } |
| 140 | + }); |
141 | 141 |
|
142 | | - if (window.location.hash) { |
143 | | - const hashParams = new URLSearchParams(window.location.hash.substring(1)); |
144 | | - const hashQuery = hashParams.get("q"); |
145 | | - if (hashQuery) { |
146 | | - searchInput.value = decodeURIComponent(hashQuery); |
147 | | - } |
148 | | - } |
| 142 | + if (window.location.hash) { |
| 143 | + const hashParams = new URLSearchParams(window.location.hash.substring(1)); |
| 144 | + const hashQuery = hashParams.get("q"); |
| 145 | + if (hashQuery) { |
| 146 | + searchInput.value = decodeURIComponent(hashQuery); |
| 147 | + } |
| 148 | + } |
149 | 149 | } |
150 | 150 |
|
151 | 151 | function initialize() { |
152 | | - const queryParam = getQueryParam("q"); |
153 | | - if (queryParam) { |
154 | | - const url = processBang(queryParam); |
155 | | - if (url) { |
156 | | - redirect(url); |
157 | | - } |
158 | | - } else { |
159 | | - document.addEventListener("DOMContentLoaded", setupUI); |
160 | | - } |
| 152 | + const queryParam = getQueryParam("q"); |
| 153 | + if (queryParam) { |
| 154 | + const url = processBang(queryParam); |
| 155 | + if (url) { |
| 156 | + redirect(url); |
| 157 | + } |
| 158 | + } else { |
| 159 | + document.addEventListener("DOMContentLoaded", setupUI); |
| 160 | + } |
161 | 161 | } |
162 | 162 |
|
163 | 163 | initialize(); |
0 commit comments