From 7fb14aadb3e16bf497c64e0bc54a0f8ee3f8e0b5 Mon Sep 17 00:00:00 2001 From: w4133d <113371782+w4133d@users.noreply.github.com> Date: Sat, 3 May 2025 14:25:54 +0100 Subject: [PATCH] Append extra queries for Google If the default bang is being used (Google), append any additional queries found other than the original "?q=%s" query. This allows for other queries to be passed to Google whenever default search is used, and extends functionality for us power users out there. This PR was originally made so that unduck wouldn't ignore the "udm=14" query I was passing it in my search engine settings: `https://unduck.link?q=%s&udm=14` Took the idea from [ThioJoe's video](https://www.youtube.com/watch?v=qGlNb2ZPZdc). --- src/main.ts | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/src/main.ts b/src/main.ts index 4452a6f4a..a437d3e0d 100644 --- a/src/main.ts +++ b/src/main.ts @@ -63,19 +63,34 @@ function getBangredirectUrl() { // Remove the first bang from the query const cleanQuery = query.replace(/!\S+\s*/i, "").trim(); + // Collect additional query parameters (excluding 'q') + const additionalParams = Array.from(url.searchParams.entries()) + .filter(([key]) => key !== "q") + .map(([key, value]) => `${key}=${encodeURIComponent(value)}`) + .join("&"); + // If the query is just `!gh`, use `github.com` instead of `github.com/search?q=` if (cleanQuery === "") - return selectedBang ? `https://${selectedBang.d}` : null; + return selectedBang + ? `https://${selectedBang.d}${additionalParams ? `?${additionalParams}` : ""}` + : null; - // Format of the url is: + // Format of the URL is: // https://www.google.com/search?q={{{s}}} - const searchUrl = selectedBang?.u.replace( + let searchUrl = selectedBang?.u.replace( "{{{s}}}", // Replace %2F with / to fix formats like "!ghr+t3dotgg/unduck" encodeURIComponent(cleanQuery).replace(/%2F/g, "/"), ); + if (!searchUrl) return null; + // Only append additional parameters if the selected bang is the default bang (Google) + if (selectedBang?.t === defaultBang?.t && additionalParams) { + const delimiter = searchUrl.includes("?") ? "&" : "?"; + searchUrl += `${delimiter}${additionalParams}`; + } + return searchUrl; }