Skip to content

Commit

Permalink
Clean up shortcode references in JS
Browse files Browse the repository at this point in the history
  • Loading branch information
DeclanChidlow committed Nov 25, 2024
1 parent 61a7e5e commit eb4e220
Showing 1 changed file with 81 additions and 86 deletions.
167 changes: 81 additions & 86 deletions script.js
Original file line number Diff line number Diff line change
@@ -1,72 +1,72 @@
const shortcodes = {
"!g": { url: "https://www.google.com/search?q=", desc: "Google Search" },
"!gf": { url: "https://fonts.google.com/?query=", desc: "Google Fonts" },
"!gi": { url: "https://fonts.google.com/icons?icon.query=", base: "https://fonts.google.com/icons", desc: "Google Icons" },
"!gm": { url: "https://www.google.com/maps/search/", base: "https://google.com/maps", desc: "Google Maps" },
"!gs": { url: "https://scholar.google.com/scholar?q=", desc: "Google Scholar" },
"!yt": { url: "https://www.youtube.com/search?q=", desc: "YouTube" },
"!ytm": { url: "https://music.youtube.com/search?q=", desc: "YouTube Music" },
"!b": { url: "https://www.bing.com/search?q=", desc: "Bing Search" },
"!ddg": { url: "https://start.duckduckgo.com/?q=", desc: "DuckDuckGo" },
"!you": { url: "https://you.com/search?q=", desc: "You" },
"!yep": { url: "https://yep.com/web?q=", desc: "Yep" },
"!yan": { url: "https://yandex.com/search/?text=", desc: "Yandex" },
"!p": { url: "https://www.perplexity.ai/search?q=", desc: "Perplexity" },
"!c": { url: "https://consensus.app/results/?q=", desc: "Consensus" },
"!m": { url: "https://www.mojeek.com/search?q=", desc: "Mojeek" },
"!sc": { url: "https://swisscows.com/web?query=", desc: "Swisscows" },
"!k": { url: "https://kagi.com/search?q=", desc: "Kagi" },
"!gpt": { url: "https://chatgpt.com/?hints=search&q=", desc: "ChatGPT Search" },
"!ask": { url: "https://www.ask.com/web?q=", desc: "Ask" },
"!q": { url: "https://www.qwant.com/?q=", desc: "Qwant" },
"!exa": { url: "https://exa.ai/search?q=", desc: "Exa" },
"!sp": { url: "https://www.startpage.com/do/search?query=", desc: "StartPage" },
"!b": { url: "https://search.brave.com/search?q=", desc: "Brave Search" },
"!eco": { url: "https://www.ecosia.org/search?q=", desc: "Ecosia" },
"!amz": { url: "https://www.amazon.com/s?k=", desc: "Amazon" },
"!ebay": { url: "https://www.ebay.com/sch/?_nkw=", desc: "eBay" },
"!bc": { url: "https://bandcamp.com/search?q=", desc: "Bandcamp" },
"!hn": { url: "https://hn.trieve.ai/?q=", base: "https://news.ycombinator.com", desc: "Hacker News" },
"!w": { url: "https://wikipedia.org/w/index.php?search=", desc: "Wikipedia" },
"!ud": { url: "https://www.urbandictionary.com/define.php?term=", desc: "Urban Dictionary" },
"!ia": { url: "https://archive.org/search?query=", desc: "Internet Archive" },
"!wm": { url: "https://web.archive.org/web/*/", desc: "Wayback Machine" },
"!r": { url: "https://www.reddit.com/search/?q=", desc: "Reddit" },
"!rs": { url: "https://www.reddit.com/search/?type=sr&q=", desc: "Reddit Subreddits" },
"!ciu": { url: "https://caniuse.com/?search=", desc: "Can I Use" },
"!cp": { url: "https://codepen.io/search/pens?q=", desc: "CodePen" },
"!mdn": { url: "https://developer.mozilla.org/en-US/search?q=", desc: "MDN Web Docs" },
"!html": { url: "https://developer.mozilla.org/en-US/search?topic=html&q=", base: "https://developer.mozilla.org/en-US/docs/Web/HTML", desc: "MDN HTML" },
"!css": { url: "https://developer.mozilla.org/en-US/search?topic=css&q=", base: "https://developer.mozilla.org/en-US/docs/Web/CSS", desc: "MDN CSS" },
"!js": { url: "https://developer.mozilla.org/en-US/search?topic=js&q=", base: "https://developer.mozilla.org/en-US/docs/Web/JavaScript", desc: "MDN JavaScript" },
"!npm": { url: "https://www.npmjs.com/search?q=", desc: "Node Package Manager" },
"!reactn": { url: "https://reactnative.dev/search?q=", desc: "React Native documentation" },
"!wp": { url: "https://wordpress.org/search/", desc: "WordPress documentation" },
"!pypi": { url: "https://pypi.org/search/?q=", desc: "Python Package Index" },
"!bs": { url: "https://bsky.app/search?q=", desc: "Bluesky" },
"!gh": { url: "https://github.com/search?q=", desc: "GitHub" },
"!gl": { url: "https://gitlab.com/search?search=", desc: "GitLab" },
"!lw": { url: "https://www.lesswrong.com/search?query=", desc: "LessWrong" },
"!un": { url: "https://unsplash.com/s/photos/", desc: "Unsplash" },
"!ov": { url: "https://openverse.org/search/?q=", desc: "Openverse" },
"!aur": { url: "https://aur.archlinux.org/packages?K=", desc: "Arch User Repository" },
"!aw": { url: "https://wiki.archlinux.org/index.php?search=", desc: "Arch Wiki" },
"!nixpkg": { url: "https://search.nixos.org/packages?query=", desc: "Nix Packages" },
"!nixopt": { url: "https://search.nixos.org/options?query=", base: "https://search.nixos.org/options", desc: "Nix Options" },
"!pr": { url: "https://www.printables.com/search/models?q=", desc: "Printables" },
"!t": { url: "https://thangs.com/search/", desc: "Thangs" },
"!pm": { url: "https://mail.proton.me/u/0/almost-all-mail#keyword=", desc: "Proton Mail" },
"!pd": { url: "https://drive.proton.me/u/0/search#q=", desc: "Proton Drive" },
"!pc": { url: "https://calendar.proton.me/u/0/search#keyword=", desc: "Proton Calendar" },
"!sym": { url: "https://symbl.cc/en/search/?q=", desc: "SYMBL" },
"!mm": { url: "https://www.merklemap.com/search?query=", desc: "Merklemap" },
"!lb": { url: "https://letterboxd.com/search/", desc: "Letterboxd" },
"!mar": { url: "https://search.marginalia.nu/search?query=", desc: "Marginalia" },
"!mcw": { url: "https://minecraft.wiki/w/?search=", desc: "Minecraft Wiki" },
"!pvzw": { url: "https://plantsvszombies.wiki.gg/index.php?search=", desc: "Plants vs Zombies Wiki" },
"!tw": { url: "https://terraria.wiki.gg/index.php?search=", desc: "Terraria Wiki" },
"!hp": { url: "https://www.halopedia.org/index.php?search=", desc: "Halopedia" },
"!tfw": { url: "https://wiki.teamfortress.com/w/index.php?search=", desc: "Team Fortress Wiki" },
g: { url: "https://www.google.com/search?q=", desc: "Google Search" },
gf: { url: "https://fonts.google.com/?query=", desc: "Google Fonts" },
gi: { url: "https://fonts.google.com/icons?icon.query=", base: "https://fonts.google.com/icons", desc: "Google Icons" },
gm: { url: "https://www.google.com/maps/search/", base: "https://google.com/maps", desc: "Google Maps" },
gs: { url: "https://scholar.google.com/scholar?q=", desc: "Google Scholar" },
yt: { url: "https://www.youtube.com/search?q=", desc: "YouTube" },
ytm: { url: "https://music.youtube.com/search?q=", desc: "YouTube Music" },
b: { url: "https://www.bing.com/search?q=", desc: "Bing Search" },
ddg: { url: "https://start.duckduckgo.com/?q=", desc: "DuckDuckGo" },
you: { url: "https://you.com/search?q=", desc: "You" },
yep: { url: "https://yep.com/web?q=", desc: "Yep" },
yan: { url: "https://yandex.com/search/?text=", desc: "Yandex" },
p: { url: "https://www.perplexity.ai/search?q=", desc: "Perplexity" },
c: { url: "https://consensus.app/results/?q=", desc: "Consensus" },
m: { url: "https://www.mojeek.com/search?q=", desc: "Mojeek" },
sc: { url: "https://swisscows.com/web?query=", desc: "Swisscows" },
k: { url: "https://kagi.com/search?q=", desc: "Kagi" },
gpt: { url: "https://chatgpt.com/?hints=search&q=", desc: "ChatGPT Search" },
ask: { url: "https://www.ask.com/web?q=", desc: "Ask" },
q: { url: "https://www.qwant.com/?q=", desc: "Qwant" },
exa: { url: "https://exa.ai/search?q=", desc: "Exa" },
sp: { url: "https://www.startpage.com/do/search?query=", desc: "StartPage" },
b: { url: "https://search.brave.com/search?q=", desc: "Brave Search" },
eco: { url: "https://www.ecosia.org/search?q=", desc: "Ecosia" },
amz: { url: "https://www.amazon.com/s?k=", desc: "Amazon" },
ebay: { url: "https://www.ebay.com/sch/?_nkw=", desc: "eBay" },
bc: { url: "https://bandcamp.com/search?q=", desc: "Bandcamp" },
hn: { url: "https://hn.trieve.ai/?q=", base: "https://news.ycombinator.com", desc: "Hacker News" },
w: { url: "https://wikipedia.org/w/index.php?search=", desc: "Wikipedia" },
ud: { url: "https://www.urbandictionary.com/define.php?term=", desc: "Urban Dictionary" },
ia: { url: "https://archive.org/search?query=", desc: "Internet Archive" },
wm: { url: "https://web.archive.org/web/*/", desc: "Wayback Machine" },
r: { url: "https://www.reddit.com/search/?q=", desc: "Reddit" },
rs: { url: "https://www.reddit.com/search/?type=sr&q=", desc: "Reddit Subreddits" },
ciu: { url: "https://caniuse.com/?search=", desc: "Can I Use" },
cp: { url: "https://codepen.io/search/pens?q=", desc: "CodePen" },
mdn: { url: "https://developer.mozilla.org/en-US/search?q=", desc: "MDN Web Docs" },
html: { url: "https://developer.mozilla.org/en-US/search?topic=html&q=", base: "https://developer.mozilla.org/en-US/docs/Web/HTML", desc: "MDN HTML" },
css: { url: "https://developer.mozilla.org/en-US/search?topic=css&q=", base: "https://developer.mozilla.org/en-US/docs/Web/CSS", desc: "MDN CSS" },
js: { url: "https://developer.mozilla.org/en-US/search?topic=js&q=", base: "https://developer.mozilla.org/en-US/docs/Web/JavaScript", desc: "MDN JavaScript" },
npm: { url: "https://www.npmjs.com/search?q=", desc: "Node Package Manager" },
reactn: { url: "https://reactnative.dev/search?q=", desc: "React Native documentation" },
wp: { url: "https://wordpress.org/search/", desc: "WordPress documentation" },
pypi: { url: "https://pypi.org/search/?q=", desc: "Python Package Index" },
bs: { url: "https://bsky.app/search?q=", desc: "Bluesky" },
gh: { url: "https://github.com/search?q=", desc: "GitHub" },
gl: { url: "https://gitlab.com/search?search=", desc: "GitLab" },
lw: { url: "https://www.lesswrong.com/search?query=", desc: "LessWrong" },
un: { url: "https://unsplash.com/s/photos/", desc: "Unsplash" },
ov: { url: "https://openverse.org/search/?q=", desc: "Openverse" },
aur: { url: "https://aur.archlinux.org/packages?K=", desc: "Arch User Repository" },
aw: { url: "https://wiki.archlinux.org/index.php?search=", desc: "Arch Wiki" },
nixpkg: { url: "https://search.nixos.org/packages?query=", desc: "Nix Packages" },
nixopt: { url: "https://search.nixos.org/options?query=", base: "https://search.nixos.org/options", desc: "Nix Options" },
pr: { url: "https://www.printables.com/search/models?q=", desc: "Printables" },
t: { url: "https://thangs.com/search/", desc: "Thangs" },
pm: { url: "https://mail.proton.me/u/0/almost-all-mail#keyword=", desc: "Proton Mail" },
pd: { url: "https://drive.proton.me/u/0/search#q=", desc: "Proton Drive" },
pc: { url: "https://calendar.proton.me/u/0/search#keyword=", desc: "Proton Calendar" },
sym: { url: "https://symbl.cc/en/search/?q=", desc: "SYMBL" },
mm: { url: "https://www.merklemap.com/search?query=", desc: "Merklemap" },
lb: { url: "https://letterboxd.com/search/", desc: "Letterboxd" },
mar: { url: "https://search.marginalia.nu/search?query=", desc: "Marginalia" },
mcw: { url: "https://minecraft.wiki/w/?search=", desc: "Minecraft Wiki" },
pvzw: { url: "https://plantsvszombies.wiki.gg/index.php?search=", desc: "Plants vs Zombies Wiki" },
tw: { url: "https://terraria.wiki.gg/index.php?search=", desc: "Terraria Wiki" },
hp: { url: "https://www.halopedia.org/index.php?search=", desc: "Halopedia" },
tfw: { url: "https://wiki.teamfortress.com/w/index.php?search=", desc: "Team Fortress Wiki" },
};

function getUrlParameter(name) {
Expand All @@ -86,22 +86,17 @@ function getBaseUrl(url) {
}

function processBangOrSnap(code, isSnap, defaultEngine) {
if (isSnap) {
const bangKey = `!${code.toLowerCase()}`;
const matchingBang = Object.keys(shortcodes).find((key) => key.toLowerCase() === bangKey);
const shortcodeKey = code.toLowerCase();
const matchingShortcode = Object.keys(shortcodes).find((key) => key.toLowerCase() === shortcodeKey);

if (matchingBang) {
const baseUrl = shortcodes[matchingBang].base || getBaseUrl(shortcodes[matchingBang].url);
if (matchingShortcode) {
if (isSnap) {
const baseUrl = shortcodes[matchingShortcode].base || getBaseUrl(shortcodes[matchingShortcode].url);
if (baseUrl) {
return `${defaultEngine}site:${baseUrl} `;
}
}
} else {
const bangKey = `!${code.toLowerCase()}`;
const matchingBang = Object.keys(shortcodes).find((key) => key.toLowerCase() === bangKey);

if (matchingBang) {
return shortcodes[matchingBang].url;
} else {
return shortcodes[matchingShortcode].url;
}
}
return null;
Expand Down Expand Up @@ -140,11 +135,11 @@ function performSearch(query, defaultEngine) {
}

if (!query.trim()) {
const bangKey = Object.keys(shortcodes).find((key) => shortcodes[key].url === searchUrl);
const shortcodeKey = Object.keys(shortcodes).find((key) => shortcodes[key].url === searchUrl);

if (bangKey) {
if (shortcodes[bangKey].base) {
window.location.href = shortcodes[bangKey].base;
if (shortcodeKey) {
if (shortcodes[shortcodeKey].base) {
window.location.href = shortcodes[shortcodeKey].base;
} else {
const urlParts = searchUrl.split("/");
window.location.href = urlParts.slice(0, 3).join("/");
Expand Down Expand Up @@ -175,9 +170,9 @@ document.addEventListener("DOMContentLoaded", function () {

function createShortcodeList() {
const fragment = document.createDocumentFragment();
Object.entries(shortcodes).forEach(([bang, { desc }]) => {
Object.entries(shortcodes).forEach(([code, { desc }]) => {
const li = document.createElement("li");
li.textContent = `${bang}: ${desc}`;
li.textContent = `${code}: ${desc}`;
fragment.appendChild(li);
});
shortcodeList.appendChild(fragment);
Expand Down

0 comments on commit eb4e220

Please sign in to comment.