Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ function noSearchDefaultPageRender() {
const LS_DEFAULT_BANG = localStorage.getItem("default-bang") ?? "g";
const defaultBang = bangs.find((b) => b.t === LS_DEFAULT_BANG);


function getBangredirectUrl() {
const url = new URL(window.location.href);
const query = url.searchParams.get("q")?.trim() ?? "";
Expand Down Expand Up @@ -75,7 +76,35 @@ function getBangredirectUrl() {
return searchUrl;
}

function feelingLuckyRedirect(query:string) {
const cleanQuery = query.replace("!", "").trim();

return `https://duckduckgo.com/?q=!ducky+${encodeURIComponent(cleanQuery)}`;
}

function doRedirect() {
const url = new URL(window.location.href);
const query = url.searchParams.get("q")?.trim() ?? "";

if (!query) {
noSearchDefaultPageRender();
return null;
}

const type = /!(?:\s|$)/i.test(query);

if (type) {
const searchUrl = feelingLuckyRedirect(query);
if (!searchUrl) return;

const link = document.createElement('a');
link.href = searchUrl;
link.rel = 'noreferrer noopener';
link.click();

return;
}

const searchUrl = getBangredirectUrl();
if (!searchUrl) return;
window.location.replace(searchUrl);
Expand Down