Repository navigation
Expand file tree
/
Copy pathscript.js
More file actions
375 lines (331 loc) · 15.8 KB
/
Copy pathscript.js
File metadata and controls
375 lines (331 loc) · 15.8 KB
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
const shortcodes = {
g: { url: "www.google.com/search?q=", desc: "Google" },
gf: { url: "fonts.google.com/?query=", desc: "Google Fonts" },
gi: { url: "fonts.google.com/icons?icon.query=", base: "fonts.google.com/icons", desc: "Google Icons" },
gm: { url: "www.google.com/maps/search/", base: "google.com/maps", desc: "Google Maps" },
gs: { url: "scholar.google.com/scholar?q=", desc: "Google Scholar" },
gp: { url: "patents.google.com/?q=", desc: "Google Patents" },
gb: { url: "books.google.com/?q=", desc: "Google Books" },
gt: { url: "trends.google.com/explore?q=", desc: "Google Trends" },
gdm: { url: "deepmind.google/search/?query=", desc: "Google DeepMind" },
yt: { url: "www.youtube.com/search?q=", desc: "YouTube" },
ytm: { url: "music.youtube.com/search?q=", desc: "YouTube Music" },
applem: { url: "music.apple.com/search?term=", desc: "Apple Music" },
b: { url: "www.bing.com/search?q=", desc: "Bing Search" },
ddg: { url: "start.duckduckgo.com/?q=", desc: "DuckDuckGo" },
ddgh: { url: "html.duckduckgo.com/html?q=", desc: "DuckDuckGo (HTML)" },
ddgl: { url: "lite.duckduckgo.com/lite?q=", desc: "DuckDuckGo (Lite)" },
you: { url: "you.com/search?q=", desc: "You" },
yep: { url: "yep.com/web?q=", desc: "Yep" },
yan: { url: "yandex.com/search/?text=", desc: "Yandex" },
good: { url: "good-search.org/en/search/?q=", desc: "Good" },
y: { url: "search.yahoo.com/search?p=", desc: "Yahoo!" },
p: { url: "www.perplexity.ai/search?q=", desc: "Perplexity" },
c: { url: "consensus.app/results/?q=", desc: "Consensus" },
m: { url: "www.mojeek.com/search?q=", desc: "Mojeek" },
sc: { url: "swisscows.com/web?query=", desc: "Swisscows" },
k: { url: "kagi.com/search?q=", desc: "Kagi" },
gpt: { url: "chatgpt.com/?hints=search&q=", desc: "ChatGPT Search" },
oai: { url: "openai.com/search/?q=", desc: "OpenAI" },
claude: { url: "claude.ai/new?q=", desc: "Claude" },
q: { url: "www.qwant.com/?q=", desc: "Qwant" },
exa: { url: "exa.ai/search?q=", desc: "Exa" },
sp: { url: "www.startpage.com/do/search?query=", desc: "StartPage" },
br: { url: "search.brave.com/search?q=", desc: "Brave Search" },
eco: { url: "www.ecosia.org/search?q=", desc: "Ecosia" },
vr: { url: "vale.rocks/search?q=", desc: "Vale.Rocks" },
amz: { url: "www.amazon.com/s?k=", desc: "Amazon" },
ebay: { url: "www.ebay.com/sch/?_nkw=", desc: "eBay" },
bc: { url: "bandcamp.com/search?q=", desc: "Bandcamp" },
hn: { url: "hn.algolia.com/?q=", base: "news.ycombinator.com", desc: "Hacker News" },
sw: { url: "simple.wikipedia.org/w/index.php?search=", desc: "Simple Wikipedia" },
w: { url: "wikipedia.org/w/index.php?search=", desc: "Wikipedia" },
wd: { url: "www.wikidata.org/w/index.php?search=", desc: "Wikidata" },
wv: { url: "wikivoyage.org/w/index.php?search=", desc: "Wikivoyage" },
wmc: { url: "commons.wikimedia.org/w/index.php?search=", desc: "Wikimedia Commons" },
wq: { url: "wikiquote.org/w/index.php?search=", desc: "Wikiquote" },
wn: { url: "wikinews.org/w/index.php?search=", desc: "Wikinews" },
wb: { url: "wikibooks.org/w/index.php?search=", desc: "Wikibooks" },
ud: { url: "www.urbandictionary.com/define.php?term=", desc: "Urban Dictionary" },
ia: { url: "archive.org/search?query=", desc: "Internet Archive" },
wm: { url: "web.archive.org/web/*/", desc: "Wayback Machine" },
me: { url: "medium.com/search?q=", desc: "Medium" },
r: { url: "www.reddit.com/search/?q=", desc: "Reddit" },
rs: { url: "www.reddit.com/search/?type=sr&q=", desc: "Reddit Subreddits" },
ciu: { url: "caniuse.com/?search=", desc: "Can I Use" },
cp: { url: "codepen.io/search/pens?q=", desc: "CodePen" },
tvdb: { url: "thetvdb.com/search?query=", desc: "TheTVDB" },
tmdb: { url: "www.themoviedb.org/search?query=", desc: "TMDB" },
anidb: { url: "anidb.net/search/anime/?adb.search=", desc: "AniDB" },
mal: { url: "myanimelist.net/search/all?q=", desc: "MyAnimeList" },
imdb: { url: "www.imdb.com/find/?q=", desc: "IMDB" },
imslp: { url: "imslp.org/index.php?search=", desc: "IMSLP" },
wa: { url: "www.wolframalpha.com/input?i=", desc: "WolframAlpha" },
osm: { url: "www.openstreetmap.org/search?query=", desc: "OpenStreetMap" },
mdn: { url: "developer.mozilla.org/en-US/search?q=", desc: "MDN Web Docs" },
html: { url: "developer.mozilla.org/en-US/search?topic=html&q=", base: "developer.mozilla.org/en-US/docs/Web/HTML", desc: "MDN HTML" },
css: { url: "developer.mozilla.org/en-US/search?topic=css&q=", base: "developer.mozilla.org/en-US/docs/Web/CSS", desc: "MDN CSS" },
js: { url: "developer.mozilla.org/en-US/search?topic=js&q=", base: "developer.mozilla.org/en-US/docs/Web/JavaScript", desc: "MDN JavaScript" },
npm: { url: "www.npmjs.com/search?q=", desc: "Node Package Manager" },
npmx: { url: "npmx.dev/search?q=", desc: "npmx.dev" },
reactn: { url: "reactnative.dev/search?q=", desc: "React Native documentation" },
wp: { url: "wordpress.org/search/", desc: "WordPress documentation" },
cfd: { url: "developer.chrome.com/s/results?q=", desc: "Chrome for Developers" },
pypi: { url: "pypi.org/search/?q=", desc: "Python Package Index" },
bs: { url: "bsky.app/search?q=", desc: "Bluesky" },
gh: { url: "github.com/search?q=", desc: "GitHub" },
gl: { url: "gitlab.com/search?search=", desc: "GitLab" },
dh: { url: "hub.docker.com/search?q=", desc: "Docker Hub" },
wc: { url: "www.webcrawler.com/serp?q=", desc: "WebCrawler" },
lw: { url: "www.lesswrong.com/search?query=", desc: "LessWrong" },
un: { url: "unsplash.com/s/photos/", desc: "Unsplash" },
ov: { url: "openverse.org/search/?q=", desc: "Openverse" },
aur: { url: "aur.archlinux.org/packages?K=", desc: "Arch User Repository" },
aw: { url: "wiki.archlinux.org/index.php?search=", desc: "Arch Wiki" },
nixpkg: { url: "search.nixos.org/packages?query=", desc: "Nix Packages" },
nixopt: { url: "search.nixos.org/options?query=", base: "search.nixos.org/options", desc: "Nix Options" },
pr: { url: "www.printables.com/search/models?q=", desc: "Printables" },
t: { url: "thangs.com/search/", desc: "Thangs" },
pm: { url: "mail.proton.me/u/0/almost-all-mail#keyword=", desc: "Proton Mail" },
pd: { url: "drive.proton.me/u/0/search#q=", desc: "Proton Drive" },
pc: { url: "calendar.proton.me/u/0/search#keyword=", desc: "Proton Calendar" },
sym: { url: "symbl.cc/en/search/?q=", desc: "SYMBL" },
mm: { url: "www.merklemap.com/search?query=", desc: "Merklemap" },
lb: { url: "letterboxd.com/search/", desc: "Letterboxd" },
mar: { url: "marginalia-search.com/search?query=", desc: "Marginalia Search" },
mcw: { url: "minecraft.wiki/w/?search=", desc: "Minecraft Wiki" },
pvzw: { url: "plantsvszombies.wiki.gg/index.php?search=", desc: "Plants vs Zombies Wiki" },
tw: { url: "terraria.wiki.gg/index.php?search=", desc: "Terraria Wiki" },
hp: { url: "www.halopedia.org/index.php?search=", desc: "Halopedia" },
tfw: { url: "wiki.teamfortress.com/w/index.php?search=", desc: "Team Fortress Wiki" },
arx: { url: "arxiv.org/search/?searchtype=all&query=", desc: "Arxiv" },
acdb: { url: "www.classification.gov.au/search/title?search=", desc: "Australian Classification Database" },
wiby: { url: "wiby.me/?q=", desc: "Wiby" },
wikenigma: { url: "wikenigma.org.uk/?do=search&q=", desc: "Wikenigma" },
tcrf: { url: "tcrf.net/index.php?search=", desc: "The Cutting Room Floor" },
sms: { url: "searchmysite.net/search/?q=", desc: "Search My Site" },
dblp: { url: "dblp.org/search?q=", desc: "dblp Computer Science Bibliography" },
snopes: { url: "www.snopes.com/search/?q=", desc: "Snopes" },
nature: { url: "www.nature.com/search?q=", desc: "Nature" },
ffn: { url: "www.fanfiction.net/search/?type=story&keywords=", desc: "FanFiction.Net" },
ao3: { url: "archiveofourown.org/works/search?work_search[query]=", desc: "Archive of Our Own" },
s: { url: "www.shodan.io/search?query=", desc: "Shodan" },
dp: { url: "www.dogpile.com/serp?q=", desc: "Dogpile" },
pubmed: { url: "pubmed.ncbi.nlm.nih.gov/?term=", desc: "PubMed" },
gmx: { url: "search.gmx.com/web/result?q=", desc: "GMX Search" },
ps: { url: "presearch.com/search?q=", desc: "Presearch" },
e: { url: "ekoru.org/?q=", desc: "Ekoru" },
pw: { url: "www.privacywall.org/search/secure?q=", desc: "PrivacyWall" },
yc: { url: "youcare.world/all?q=", desc: "YouCare" },
ly: { url: "search.lycos.com/web/?q=", desc: "Lycos" },
give: { url: "givero.com/results?q=", desc: "Givero" },
st: { url: "stract.com/search?q=", desc: "Stract" },
rd: { url: "rightdao.com/search?q=", desc: "Right Dao" },
a: { url: "www.alexandria.org/?q=", desc: "Alexandria Search" },
pkr: { url: "peekr.org/dig?q=", desc: "Peekr" },
it: { url: "infotiger.com/search?query=", desc: "Infotiger" },
burf: { url: "burf.co/search.php?term=", desc: "Burf.co" },
ssel: { url: "www.secretsearchenginelabs.com/find/", desc: "Secret Search Engine Labs" },
gab: { url: "www.gabanza.com/search?query=", desc: "Gabanza" },
fynd: { url: "fynd.bot/?search=", desc: "Fynd" },
pe: { url: "prieco.net/?&q=", desc: "PriEco" },
ichi: { url: "ichi.do/search?q=", desc: "Ichido" },
tl: { url: "teclis.com/search?q=", desc: "Teclis" },
cl: { url: "clew.se/search?q=", desc: "Clew" },
ll: { url: "search.lixialabs.com/?q=", desc: "Lixia Labs Search" },
gn: { url: "www.gnomit.com/search/", desc: "Gnomit" },
ko: { url: "kozmonavt.su/s?q=", desc: "Kozmonavt" },
se: { url: "www.sengine.info/Search?q=", desc: "sengine.info" },
ts: { url: "search.thunderstone.com/texis/websearch15/?q=", desc: "Thunderstone's Web Site Catalog" },
bm: { url: "bonzamate.com.au/?q=", desc: "Bonzamate" },
sems: { url: "www.semanticscholar.org/search?q=", desc: "Semantic Scholar" },
src: { url: "searchcode.com/?q=", desc: "searchcode" },
mwm: { url: "mwmbl.org/?q=", desc: "Mwmbl" },
us: { url: "unobtanium.rocks/search?search=", desc: "Unobtanium Search" },
ku: { url: "kukei.eu/?q=", desc: "Kukei.eu" },
ew: { url: "wiki.evageeks.org/index.php?search=", desc: "EvaWiki" },
scratch: { url: "scratch.mit.edu/search/projects?q=", desc: "Scratch" },
ssb: { url: "www.ssbwiki.com/index.php?search=", desc: "SmashWiki" },
am: { url: "automod.vale.rocks/search?q=", desc: "AutoMod" },
ph: { url: "www.phind.com/search/?q=", desc: "Phind" },
moz: { url: "mozberg.com/?q=", desc: "Mozberg" },
xkcd: { url: "searchxkcd.com/?q=", desc: "XKCD" },
lin: { url: "www.linkedin.com/search/results/all/?keywords=", desc: "LinkedIn" },
apple: { url: "www.apple.com/search/", desc: "Apple" },
crates: { url: "crates.io/search?q=", desc: "crates.io" },
docsrs: { url: "docs.rs/releases/search?query=", desc: "Docs.rs" },
wdm: { url: "www.webdesignmuseum.org/?s=", desc: "Web Design Museum" },
lcdb: { url: "content.luanti.org/packages/?q=", desc: "Luanti Content DB" },
mr: { url: "modrinth.com/mods?q=", desc: "Modrinth" },
sm: { url: "www.smashingmagazine.com/search/?q=", desc: "Smashing Magazine" },
v: { url: "www.theverge.com/search?q=", desc: "The Verge" },
wired: { url: "www.wired.com/search/?q=", desc: "WIRED" },
steam: { url: "store.steampowered.com/search/?term=", desc: "Steam" },
gog: { url: "www.gog.com/games?query=", desc: "GOG" },
egs: { url: "store.epicgames.com/browse?q=", desc: "Epic Games Store" },
abc: { url: "discover.abc.net.au/index.html#/?query=", desc: "Australian Broadcasting Corporation" },
iio: { url: "itch.io/search?q=", desc: "itch.io" },
iseas: { url: "indieseas.net/search?q=", desc: "IndieSeas" },
sci: { url: "scira.ai/?q=", desc: "Scira" },
glg: { url: "gloogle.tiago.zip/?q=", desc: "Gloogle" },
};
let shortcodesByCode = null;
function getShortcode(code) {
if (!shortcodesByCode) {
shortcodesByCode = new Map(Object.entries(shortcodes).map(([key, shortcode]) => [key.toLowerCase(), shortcode]));
}
return shortcodesByCode.get(code.toLowerCase());
}
let shortcodesByUrl = null;
function getShortcodeByUrl(url) {
if (!shortcodesByUrl) {
shortcodesByUrl = new Map(Object.entries(shortcodes).map(([, shortcode]) => [`https://${shortcode.url}`, shortcode]));
}
return shortcodesByUrl.get(url);
}
function getUrlParameter(name) {
return new URLSearchParams(location.search).get(name) || "";
}
function getBaseUrl(url) {
const urlObj = new URL("https://" + url);
return urlObj.origin;
}
function getSavedEngine() {
try {
return localStorage.getItem("selectedEngine");
} catch {
return null;
}
}
function processCode(code, isSnap) {
const shortcode = getShortcode(code);
if (!shortcode) {
return null;
}
if (isSnap) {
const baseUrl = shortcode.base || getBaseUrl(shortcode.url);
return baseUrl ? `site:${baseUrl.replace("https://", "")} ` : null;
}
return "https://" + shortcode.url;
}
function performSearch(query, defaultEngine) {
let searchUrl = defaultEngine;
let sitePrefix = "";
const processedTokens = [];
for (const token of query.split(" ")) {
if (token.startsWith("!")) {
const result = processCode(token.slice(1), false);
if (result) {
searchUrl = result;
continue;
}
} else if (token.startsWith("@")) {
const result = processCode(token.slice(1), true);
if (result) {
sitePrefix = result;
continue;
}
}
processedTokens.push(token);
}
let processedQuery = processedTokens.join(" ");
if (!processedQuery.trim()) {
const shortcode = getShortcodeByUrl(searchUrl);
if (shortcode) {
const target = shortcode.base ? `https://${shortcode.base}` : searchUrl.split("/").slice(0, 3).join("/");
window.location.replace(target);
} else {
window.location.replace(searchUrl);
}
return;
}
if (sitePrefix) {
processedQuery = sitePrefix + processedQuery;
}
window.location.replace(`${searchUrl}${encodeURIComponent(processedQuery)}`);
}
document.addEventListener("DOMContentLoaded", function () {
const scriptEnabled = document.getElementById("script-enabled"),
searchForm = document.getElementById("search-form"),
searchInput = document.getElementById("search-input"),
engineSelect = document.getElementById("engine-select"),
shortcodeList = document.getElementById("shortcode-list"),
shortcodePopover = document.getElementById("shortcode-popover");
function createShortcodeList() {
const fragment = document.createDocumentFragment();
Object.entries(shortcodes).forEach(([code, { desc }]) => {
const li = document.createElement("li");
li.textContent = `${code}: ${desc}`;
fragment.appendChild(li);
});
shortcodeList.appendChild(fragment);
}
function saveSelectedEngine() {
try {
localStorage.setItem("selectedEngine", engineSelect.value);
} catch {}
}
function loadSelectedEngine() {
const savedEngine = getSavedEngine();
if (savedEngine) {
engineSelect.value = savedEngine;
} else {
engineSelect.value = document.documentElement.dataset.defaultEngine || engineSelect.value;
// First time visitor
saveSelectedEngine();
}
}
function setupEventListeners() {
engineSelect.addEventListener("change", saveSelectedEngine);
shortcodePopover.addEventListener(
"beforetoggle",
(e) => {
if (e.newState === "open") {
createShortcodeList();
}
},
{ once: true },
);
searchForm.addEventListener("submit", (e) => {
e.preventDefault();
const query = searchInput.value.trim();
performSearch(query, engineSelect.value);
});
document.addEventListener("keydown", (e) => {
if (e.key === "/" && document.activeElement !== searchInput) {
e.preventDefault();
searchInput.focus();
} else if (e.key === "Escape" && document.activeElement === searchInput) {
searchInput.value = "";
}
});
}
function init() {
const searchQuery = getUrlParameter("q");
if (window.__searchRouterRedirected) {
return;
}
loadSelectedEngine();
if (searchQuery) {
performSearch(searchQuery, engineSelect.value);
return;
}
scriptEnabled.style.display = "flex";
setupEventListeners();
}
init();
});
(function () {
if (window.__searchRouterRedirected) {
return;
}
const searchQuery = getUrlParameter("q");
if (!searchQuery) {
return;
}
const engine = getSavedEngine() || document.documentElement.dataset.defaultEngine;
if (engine) {
window.__searchRouterRedirected = true;
performSearch(searchQuery, engine);
}
})();