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>
This commit is contained in:
2026-10-12 14:41:36 +00:00
co-authored by Claude Opus 5.5
parent 469d8ef8fe
commit 38f76a9e04
6 changed files with 40 additions and 29 deletions
+8 -16
View File
@@ -1,3 +1,5 @@
// { 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.
@@ -8,24 +10,14 @@ export async function bingImageSearchFallback(query) {
"Cookie": "SRCHHPGUSR=ADLT=OFF"
}
});
if (!res.ok) return null;
if (!res.ok) return { error: `HTTP ${res.status}` };
const html = await res.text();
const urls = [];
// Bing encodes the image payload inside HTML entity attributes and script tags
const regex1 = /&quot;murl&quot;:&quot;(https:[^&"]+)&quot;/g;
const regex2 = /"murl":"(https:[^"]+)"/g;
let match;
while ((match = regex1.exec(html)) !== null) {
urls.push(match[1].replace(/\\\//g, '/'));
}
while ((match = regex2.exec(html)) !== null) {
urls.push(match[1].replace(/\\\//g, '/'));
}
return urls.length > 0 ? [...new Set(urls)] : null;
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 null;
return { error: err.message };
}
}
+11 -6
View File
@@ -1,8 +1,13 @@
// { urls } ([] if none found), or { error } if Brave refused or failed, e.g. once our rate or spending limit is hit
export async function braveImageSearch(query, apiKey) {
const res = await fetch(`https://api.search.brave.com/res/v1/images/search?q=${encodeURIComponent(query)}&count=50&safesearch=off`, {
headers: { "Accept": "application/json", "X-Subscription-Token": apiKey },
});
if (!res.ok) return null;
const data = await res.json();
return data.results?.map(r => r.properties?.url || r.thumbnail?.src).filter(url => !!url) || null;
try {
const res = await fetch(`https://api.search.brave.com/res/v1/images/search?q=${encodeURIComponent(query)}&count=50&safesearch=off`, {
headers: { "Accept": "application/json", "X-Subscription-Token": apiKey },
});
if (!res.ok) return { error: `HTTP ${res.status}: ${(await res.text()).slice(0, 300)}` };
const data = await res.json();
return { urls: data.results?.map(r => r.properties?.url || r.thumbnail?.src).filter(url => !!url) || [] };
} catch (err) {
return { error: err.message };
}
}