mirror of
https://github.com/direct-img/direct-img.link.git
synced 2026-10-13 08:25:21 +00:00
safesearch=moderate isn't a Bing parameter. Bing's filter is set by the SRCHHPGUSR=ADLT=OFF cookie, so send that (plus adlt=off). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
32 lines
1.2 KiB
JavaScript
32 lines
1.2 KiB
JavaScript
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 null;
|
|
const html = await res.text();
|
|
const urls = [];
|
|
|
|
// Bing encodes the image payload inside HTML entity attributes and script tags
|
|
const regex1 = /"murl":"(https:[^&"]+)"/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;
|
|
} catch (err) {
|
|
return null;
|
|
}
|
|
}
|