Skip to content

Commit

Permalink
Add support for snaps
Browse files Browse the repository at this point in the history
  • Loading branch information
DeclanChidlow committed Oct 15, 2024
1 parent aaf54ed commit caf8bcc
Showing 1 changed file with 30 additions and 10 deletions.
40 changes: 30 additions & 10 deletions script.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,20 +95,40 @@ function loadSelectedEngine() {
}
}

function getBaseUrl(url) {
try {
const urlObj = new URL(url);
return urlObj.origin;
} catch (e) {
console.error("Invalid URL:", url);
return null;
}
}

function performSearch(query) {
let searchUrl = engineSelect.value;

if (bangPatterns.hasOwnProperty(query)) {
const { base, url } = bangPatterns[query];
window.location.href = base || new URL(url).origin;
return;
let isSnap = false;

if (query.startsWith("@")) {
const [snapCode, ...rest] = query.slice(1).split(" ");
const bangKey = `!${snapCode}`;
if (bangPatterns.hasOwnProperty(bangKey)) {
const baseUrl = bangPatterns[bangKey].base || getBaseUrl(bangPatterns[bangKey].url);
if (baseUrl) {
searchUrl = `${engineSelect.value}site:${baseUrl} `;
query = rest.join(" ");
isSnap = true;
}
}
}

for (const [bang, { url }] of Object.entries(bangPatterns)) {
if (query.startsWith(`${bang} `)) {
searchUrl = url;
query = query.slice(bang.length + 1);
break;
if (!isSnap) {
for (const [bang, { url }] of Object.entries(bangPatterns)) {
if (query.startsWith(`${bang} `)) {
searchUrl = url;
query = query.slice(bang.length + 1);
break;
}
}
}

Expand Down

0 comments on commit caf8bcc

Please sign in to comment.