From 1fe9935fca397c5a9b116b60cd7797ed764482af Mon Sep 17 00:00:00 2001 From: multipleof4 Date: Thu, 12 Mar 2026 14:31:53 -0700 Subject: [PATCH] Refactor: Extract Bing image search logic --- functions/_utils/bing.js | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 functions/_utils/bing.js diff --git a/functions/_utils/bing.js b/functions/_utils/bing.js new file mode 100644 index 0000000..3ab0d4a --- /dev/null +++ b/functions/_utils/bing.js @@ -0,0 +1,29 @@ +export async function bingImageSearchFallback(query) { + try { + const res = await fetch(`https://www.bing.com/images/search?q=${encodeURIComponent(query)}&safesearch=moderate`, { + 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" + } + }); + 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; + } +}