Files
direct-img.link/functions/_utils/bing.js
T
multipleof4andClaude Opus 5.5 38f76a9e04 Feat: Fall back to Bing when Brave refuses, and serve donate.webp when Bing blocks too
Brave errors (e.g. its spending limit being hit) now fall back to Bing
like empty results do, with the error in a Brave Unavailable alert. If
Bing fails too (HTTP error or a page without results, which for Bing
means it's blocking us), a new papercraft donate.webp is served instead
of an AI image, and nothing is cached so the query works once either
search is back.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-12 14:41:36 +00:00

24 lines
1.3 KiB
JavaScript

// { urls }, or { error } if Bing failed. Bing finds something for nearly any query (even gibberish), so a page without
// results almost always means it's blocking us (e.g. with a CAPTCHA) or changed its page format.
export async function bingImageSearchFallback(query) {
try {
// SafeSearch off, like Brave. The cookie is what actually sets it; adlt= alone is ignored.
const res = await fetch(`https://www.bing.com/images/search?q=${encodeURIComponent(query)}&adlt=off`, {
headers: {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36",
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
"Cookie": "SRCHHPGUSR=ADLT=OFF"
}
});
if (!res.ok) return { error: `HTTP ${res.status}` };
const html = await res.text();
// Bing encodes the image payload inside HTML entity attributes and script tags
const urls = [...html.matchAll(/&quot;murl&quot;:&quot;(https:[^&"]+)&quot;/g), ...html.matchAll(/"murl":"(https:[^"]+)"/g)]
.map(m => m[1].replace(/\\\//g, '/'));
return urls.length ? { urls: [...new Set(urls)] } : { error: `No results in page (${html.length} bytes, ${res.url})` };
} catch (err) {
return { error: err.message };
}
}