vladimyr-googlefeelinglucky.web.val.run
Readme

Google Feeling Lucky auto-redirect !lucky

DuckDuckGo !Bangs offer the following shortcuts for Google's Feeling Lucky (going straight to the first result) search option:

Unfortunately, they don't work anymore due to redirect notices that get shown. This service enables you to skip redirect notice by altering your search from the original query e.g.:
https://www.google.com/search?q=site:developer.mozilla.org+Object.hasOwn&btnI

To the modified one made against this val's HTTP endpoint:
https://vladimyr-googlefeelinglucky.web.val.run/?q=site:developer.mozilla.org+Object.hasOwn

Additionally, you can define custom search shortcut inside your browser to allow a local bang-like experience:

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
import { DOMParser, Element, initParser } from "https://deno.land/x/deno_dom/deno-dom-wasm-noinit.ts";
import ky from "npm:ky";
const DEFAULT_USER_AGENT =
"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36";
export default async function(req: Request): Promise<Response> {
const { searchParams } = new URL(req.url);
const query = searchParams.get("q");
if (!query) return Response.redirect("https://www.google.com");
try {
const resultUrl = await search(query);
return Response.redirect(resultUrl);
} catch {
return Response.redirect("https://www.google.com");
}
}
export function search(query: string, userAgent = DEFAULT_USER_AGENT) {
const searchUrl = createSearchUrl(query);
return getRedirectUrl(searchUrl, userAgent);
}
export function createSearchUrl(query: string, buttonName = "") {
const searchURL = new URL("https://www.google.com/search");
searchURL.searchParams.set("q", query);
searchURL.searchParams.set("btnI", buttonName);
return searchURL.href;
}
export async function getRedirectUrl(searchUrl: string | URL, userAgent = DEFAULT_USER_AGENT) {
const resp = await ky.get(searchUrl, { headers: { "user-agent": userAgent } });
const { searchParams } = new URL(resp.url);
const redirectURL = new URL(searchParams.get("q"));
return redirectURL.href;
}
export async function signSearchUrl(searchUrl: string | URL) {
const signedURL = new URL(searchUrl);
const params = await getSearchParams();
Object.entries(params).forEach(([name, value]) => signedURL.searchParams.set(name, String(value)));
return signedURL.href;
}
export async function getSearchParams(userAgent = DEFAULT_USER_AGENT) {
await initParser();
const html = await ky.get("https://www.google.com", { headers: { "user-agent": userAgent } }).text();
const doc = new DOMParser().parseFromString(html, "text/html");
const inputs: Array<Element> = Array.from(doc.querySelectorAll("form input[type=hidden]"));
const entries = inputs.map((input) => [input.getAttribute("name"), input.getAttribute("value")]);
return Object.fromEntries(entries);
}
Val Town is a social website to write and deploy JavaScript.
Build APIs and schedule functions from your browser.
Comments
Nobody has commented on this val yet: be the first!
April 26, 2024