|
const puppeteer = require('puppeteer'); |
|
const express = require('express'); |
|
const cors = require('cors'); |
|
const app = express(); |
|
const port = 7860; |
|
|
|
app.use(cors()); |
|
|
|
function convertEscapedUrlToStandard(escapedUrl) { |
|
|
|
const standardUrl = escapedUrl.replace(/\\\//g, '/'); |
|
return standardUrl; |
|
} |
|
|
|
function extractStringAfterRp(url) { |
|
|
|
const parts = url.split('rp/'); |
|
if (parts.length > 1) { |
|
return parts[1]; |
|
} |
|
return '未找到 "rp/" 后的字符串'; |
|
} |
|
|
|
app.get('/*', async (req, res) => { |
|
const referer = req.headers.referer || "https://r.bing.com"; |
|
const url = new URL(referer); |
|
const rehost = url.hostname; |
|
|
|
const browser = await puppeteer.launch({ |
|
executablePath: '/usr/bin/google-chrome-stable', |
|
|
|
}); |
|
|
|
const page = await browser.newPage(); |
|
|
|
|
|
await page.goto('https://www.bing.com/search?q=Microsoft+Copilot&FORM=hpcodx&showconv=1&showconv=1'); |
|
|
|
|
|
await page.waitForFunction(() => { |
|
return document.documentElement.innerHTML.includes('CodexBundle'); |
|
}, { timeout: 30000 }); |
|
|
|
|
|
const result = await page.evaluate(() => { |
|
const regex = /CodexBundle:cib-bundle.*?\.js/; |
|
const htmlContent = document.documentElement.innerHTML; |
|
const matchcib = htmlContent.match(regex); |
|
if (matchcib) { |
|
const cibname = matchcib[0]; |
|
|
|
const allregex = /\/([a-zA-Z0-9_-]+\.br\.js)'/g; |
|
const matches = htmlContent.match(allregex); |
|
|
|
let scripts = ''; |
|
if (matches) { |
|
matches.forEach(match => { |
|
const scriptUrl = match.replace(/'/g, ""); |
|
scripts += `<script src="https://r.bing.com/rp${scriptUrl}"></script>\n`; |
|
}); |
|
} |
|
return { scripts, cibname, htmlContent }; |
|
} |
|
return { scripts: '没有找到匹配的字符串', cibname, htmlContent }; |
|
}); |
|
|
|
console.log(result.cibname); |
|
|
|
|
|
if (req.path === '/html') { |
|
res.set('Content-Type', 'text/html'); |
|
res.send(result.htmlContent); |
|
} else if (req.path === '/core') { |
|
|
|
const coreContent = result.htmlContent.match(/\(function\(n,t\)\{onload=function\(\)\{_G\.BPT=new.*?\/\/\]\]/s); |
|
if (coreContent) { |
|
|
|
let extractedString = coreContent[0]; |
|
extractedString = extractedString.replace('[CDATA[', '').replace('//]]', '').replace('https:\/\/r.bing.com', ''); |
|
extractedString = extractedString.replace(/\\\/rp\\\//g, `https://${rehost}/rp/`); |
|
extractedString = extractedString.replace(/https:\\\/\\\/r\.bing\.com/g, ''); |
|
extractedString = extractedString.replace(/\\\/rs\\\/6r\\\/x2\\\/nj\\\//g, `https://r.bing.com/rs/6r/x2/nj/`); |
|
extractedString = extractedString.replace(/\\\/rs\\\/6r\\\/sQ\\\/jnc,nj\\\//g, `https://r.bing.com/rs/6r/sQ/jnc,nj/`); |
|
extractedString = extractedString.replace(/\\\/rs\\\/6r\\\/kQ\\\/jnc,nj\\\//g, `https://r.bing.com/rs/6r/kQ/jnc,nj/`); |
|
|
|
|
|
extractedString = extractedString.replace(/,\{\'A:.{1,2}\':.{1,2}\}/g, ''); |
|
|
|
extractedString = extractedString.replace(/\{\'A:rms:answers:Web:SydneyFSCHelper/g, '//{\'A:rms:answers:Web:SydneyFSCHelper'); |
|
extractedString = extractedString.replace(/\'\},/g, '\'},\n'); |
|
|
|
|
|
res.set('Content-Type', 'text/plain'); |
|
res.send(extractedString); |
|
|
|
} else { |
|
res.set('Content-Type', 'text/plain'); |
|
res.send('没有找到Core匹配的字符串'); |
|
} |
|
} else if (req.path === '/importmap') { |
|
|
|
const importmapContent = result.htmlContent.match(/<script type="importmap" nonce="">.*?<\/script>/s); |
|
if (importmapContent) { |
|
let pextractedString = importmapContent[0]; |
|
pextractedString = pextractedString.replace('<script type="importmap" nonce="">', '').replace('<\/script>', ''); |
|
|
|
|
|
|
|
res.set('Content-Type', 'text/plain'); |
|
res.send(pextractedString); |
|
} else { |
|
res.set('Content-Type', 'text/plain'); |
|
res.send('没有找到map匹配的字符串'); |
|
} |
|
} else { |
|
res.set('Content-Type', 'text/plain'); |
|
res.send(result.scripts); |
|
} |
|
|
|
|
|
await browser.close(); |
|
}); |
|
|
|
app.listen(port, () => { |
|
console.log(`Server running at http://localhost:${port}`); |
|
}); |
|
|