Create index.js
Browse files
index.js
ADDED
@@ -0,0 +1,76 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
const puppeteer = require('puppeteer');
|
2 |
+
const express = require('express');
|
3 |
+
const app = express();
|
4 |
+
const port = 7860;
|
5 |
+
|
6 |
+
function convertEscapedUrlToStandard(escapedUrl) {
|
7 |
+
// 使用正则表达式替换所有的转义反斜杠
|
8 |
+
const standardUrl = escapedUrl.replace(/\\\//g, '/');
|
9 |
+
return standardUrl;
|
10 |
+
}
|
11 |
+
function extractStringAfterRp(url) {
|
12 |
+
// 分割URL并找到 'rp/' 后面的部分
|
13 |
+
const parts = url.split('rp/');
|
14 |
+
if (parts.length > 1) {
|
15 |
+
return parts[1]; // 返回 'rp/' 后面的字符串
|
16 |
+
}
|
17 |
+
return '未找到 "rp/" 后的字符串';
|
18 |
+
}
|
19 |
+
|
20 |
+
|
21 |
+
|
22 |
+
app.get('/', async (req, res) => {
|
23 |
+
// 启动浏览器
|
24 |
+
const browser = await puppeteer.launch({
|
25 |
+
executablePath: '/usr/bin/google-chrome-stable', // 根据您的实际路径进行修改
|
26 |
+
// 其他选项...
|
27 |
+
});
|
28 |
+
|
29 |
+
const page = await browser.newPage();
|
30 |
+
|
31 |
+
// 导航到网页
|
32 |
+
await page.goto('https://www.bing.com/search?q=Microsoft+Copilot&FORM=hpcodx&showconv=1&showconv=1');
|
33 |
+
|
34 |
+
// 等待页面源码中出现 'CodexBundle' 字符串,最长等待时间为30秒
|
35 |
+
await page.waitForFunction(() => {
|
36 |
+
return document.documentElement.innerHTML.includes('CodexBundle');
|
37 |
+
}, { timeout: 30000 }); // 设置超时时间为30秒
|
38 |
+
|
39 |
+
// 执行页面脚本来查找特定的字符串
|
40 |
+
const result = await page.evaluate(() => {
|
41 |
+
const regex = /CodexBundle:cib-bundle.*?\.js/;
|
42 |
+
const htmlContent = document.documentElement.innerHTML;
|
43 |
+
const matchcib = htmlContent.match(regex);
|
44 |
+
if (matchcib) {
|
45 |
+
// return matchcib[0];
|
46 |
+
// 使用正则表达式提取所有的JS文件URL
|
47 |
+
const allregex = /\/([a-zA-Z0-9_-]+\.br\.js)'/g
|
48 |
+
const matches = text.match(allregex)
|
49 |
+
|
50 |
+
let scripts = ''
|
51 |
+
if (matches) {
|
52 |
+
matches.forEach(match => {
|
53 |
+
const scriptUrl = match.replace(/'/g, "")
|
54 |
+
scripts += `<script src="https://sokwith-proxybing.hf.space/rp${scriptUrl}"></script>\n`
|
55 |
+
})
|
56 |
+
}
|
57 |
+
return scripts;
|
58 |
+
}
|
59 |
+
return '没有找到匹配的字符串';
|
60 |
+
});
|
61 |
+
|
62 |
+
console.log(result);
|
63 |
+
// 关闭浏览器
|
64 |
+
await browser.close();
|
65 |
+
res.send(result);
|
66 |
+
|
67 |
+
// const standardUrl = convertEscapedUrlToStandard(result);
|
68 |
+
// const stringAfterRp = extractStringAfterRp(standardUrl);
|
69 |
+
// 将结果发送回客户端
|
70 |
+
// res.send(stringAfterRp);
|
71 |
+
});
|
72 |
+
|
73 |
+
|
74 |
+
app.listen(port, () => {
|
75 |
+
console.log(`Server running at http://localhost:${port}`);
|
76 |
+
});
|