|
| 1 | +import http from "http"; |
| 2 | +import fs from "fs"; |
| 3 | +import path from "path"; |
| 4 | +import puppeteer from "puppeteer"; |
| 5 | +import { UserConfig } from "../../types"; |
| 6 | + |
| 7 | +export const createServerAndOpenPage = async (options: { |
| 8 | + localConfig: UserConfig[], |
| 9 | + remoteConfig: UserConfig[], |
| 10 | +}) => { |
| 11 | + return new Promise<UserConfig[]>((resolve, reject) => { |
| 12 | + // 创建HTTP服务器 |
| 13 | + const server = http.createServer((req, res) => { |
| 14 | + // 处理根路径请求 |
| 15 | + const purePath = req.url?.slice(0, req.url.indexOf("?")); |
| 16 | + if (!purePath || purePath === "/") { |
| 17 | + const html = fs.readFileSync( |
| 18 | + path.join(__dirname, "../../server/index.html") |
| 19 | + ); |
| 20 | + res.writeHead(200, { "Content-Type": "text/html; charset=utf-8" }); |
| 21 | + return res.end(html); |
| 22 | + } |
| 23 | + |
| 24 | + // 处理其他静态文件 |
| 25 | + const filePath = path.join(__dirname, req.url as string); |
| 26 | + if (fs.existsSync(filePath)) { |
| 27 | + const content = fs.readFileSync(filePath); |
| 28 | + res.end(content); |
| 29 | + } else { |
| 30 | + res.writeHead(404); |
| 31 | + res.end("Not found"); |
| 32 | + } |
| 33 | + }); |
| 34 | + |
| 35 | + const port = 56789; |
| 36 | + const baseURL = `http://localhost:${port}`; |
| 37 | + const url = `${baseURL}?ws=ws://localhost:${port}`; |
| 38 | + |
| 39 | + /** |
| 40 | + * 打开冲突处理页面 |
| 41 | + */ |
| 42 | + const openConflictWebPage = async (cb: (type: 'success', data: any) => void) => { |
| 43 | + const browser = await puppeteer.launch({ |
| 44 | + headless: false, |
| 45 | + }); |
| 46 | + const page = await browser.newPage(); |
| 47 | + await page.goto(url, { |
| 48 | + |
| 49 | + }); |
| 50 | + // 给页面注入全局变量 |
| 51 | + await page.addScriptTag({ |
| 52 | + content: ` |
| 53 | + window.GCM = { |
| 54 | + localConfig: ${JSON.stringify(options.localConfig)}, |
| 55 | + remoteConfig: ${JSON.stringify(options.remoteConfig)}, |
| 56 | + }; |
| 57 | + document.querySelector('#localConfig').value = JSON.stringify(window.GCM.localConfig, null, 2); |
| 58 | + document.querySelector('#remoteConfig').value = JSON.stringify(window.GCM.remoteConfig, null, 2); |
| 59 | + `, |
| 60 | + }); |
| 61 | + // 监听页面中的点击事件 |
| 62 | + await page.exposeFunction("onClickEvent", (event) => { |
| 63 | + console.log("点击事件发生:", event); |
| 64 | + if (event.target === 'submit_btn') { |
| 65 | + |
| 66 | + // TODO 校验配置 |
| 67 | + |
| 68 | + cb('success', JSON.parse(event.data)) |
| 69 | + browser.close(); |
| 70 | + } |
| 71 | + }); |
| 72 | + |
| 73 | + // 在页面中注入 JavaScript 代码来监听点击事件 |
| 74 | + await page.evaluate(() => { |
| 75 | + |
| 76 | + document.addEventListener("click", (event) => { |
| 77 | + console.log('合并输入框', document.querySelector('#mergedConfig')) |
| 78 | + console.log('合并输入框的值', document.querySelector('#mergedConfig')?.innerHTML) |
| 79 | + // 调用 Node.js 中暴露的函数 |
| 80 | + window.onClickEvent({ |
| 81 | + target: event.target.id, |
| 82 | + data: document.querySelector('#mergedConfig')?.value |
| 83 | + }); |
| 84 | + }); |
| 85 | + }); |
| 86 | + }; |
| 87 | + |
| 88 | + // 启动服务 |
| 89 | + server.listen(port, async () => { |
| 90 | + console.log(`Server running at ${baseURL}`); |
| 91 | + // 获取本地配置内容 |
| 92 | + openConflictWebPage((type, data) => { |
| 93 | + if (type == 'success') { |
| 94 | + // TODO 提交配置 |
| 95 | + console.log('提交配置', data) |
| 96 | + resolve(data); |
| 97 | + } |
| 98 | + }); |
| 99 | + }); |
| 100 | + |
| 101 | + }) |
| 102 | +}; |
0 commit comments