|
|
@@ -101,14 +101,14 @@ let preLoadCode = `
|
|
|
|
|
|
loadScript('https://cunchu.site/app/main.js');
|
|
|
`
|
|
|
-function createWindow () {
|
|
|
+function createWindow (partitionSession) {
|
|
|
|
|
|
// console.log(webConfig)
|
|
|
mainWindow = new BrowserWindow(webConfig)
|
|
|
|
|
|
// 代理
|
|
|
if (webConfig.proxy) {
|
|
|
- mainWindow.webContents.session.setProxy({
|
|
|
+ partitionSession.setProxy({
|
|
|
proxyRules: webConfig.proxy,
|
|
|
proxyBypassRules: 'localhost',
|
|
|
}, function () {
|
|
|
@@ -166,6 +166,74 @@ function setupCSPRemoval(ses) {
|
|
|
});
|
|
|
}
|
|
|
|
|
|
+// 向前端窗口发送数据
|
|
|
+function sendToFrontend(channel, data) {
|
|
|
+ if (mainWindow && !mainWindow.isDestroyed()) {
|
|
|
+ console.log('发送数据到前端:', channel, data);
|
|
|
+ mainWindow.webContents.send(channel, data);
|
|
|
+ } else {
|
|
|
+ console.log('主窗口未就绪,无法发送数据');
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+function setupRequestInterceptor(defaultSession) {
|
|
|
+
|
|
|
+ // 监听请求完成事件
|
|
|
+ defaultSession.webRequest.onCompleted(async (details) => {
|
|
|
+ // 判断网址是否需要拦截
|
|
|
+ if (webConfig.interceptor.includes(details.url)) {
|
|
|
+ try {
|
|
|
+ // 获取请求体数据
|
|
|
+ const requestBody = await getRequestBody(details);
|
|
|
+
|
|
|
+ // 构造拦截数据对象
|
|
|
+ const interceptedData = {
|
|
|
+ url: details.url,
|
|
|
+ method: details.method,
|
|
|
+ statusCode: details.statusCode,
|
|
|
+ requestHeaders: details.requestHeaders,
|
|
|
+ requestBody: requestBody,
|
|
|
+ timestamp: new Date().toISOString(),
|
|
|
+ type: 'xhr_intercept'
|
|
|
+ };
|
|
|
+ mainWindow.webContents.executeJavaScript(`if (window.onInterceptedData) {window.onInterceptedData(${JSON.stringify(interceptedData)})}`);
|
|
|
+ // 打印到控制台
|
|
|
+ console.log('=== 拦截到请求 ===');
|
|
|
+ console.log('URL:', details.url);
|
|
|
+ console.log('方法:', details.method);
|
|
|
+ console.log('状态码:', details.statusCode);
|
|
|
+ console.log('请求体:', requestBody);
|
|
|
+ console.log('=====================');
|
|
|
+
|
|
|
+ } catch (error) {
|
|
|
+ console.error('获取请求体失败:', error);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ });
|
|
|
+}
|
|
|
+
|
|
|
+// 获取请求体数据(需要处理不同内容类型)
|
|
|
+async function getRequestBody(details) {
|
|
|
+ return new Promise((resolve) => {
|
|
|
+ // 对于简单的表单数据,可以从uploadData获取
|
|
|
+ if (details.uploadData && details.uploadData.length > 0) {
|
|
|
+ const body = details.uploadData.map(data => {
|
|
|
+ try {
|
|
|
+ // 尝试解析JSON
|
|
|
+ return JSON.parse(data.bytes.toString());
|
|
|
+ } catch {
|
|
|
+ // 如果不是JSON,返回原始数据
|
|
|
+ return data.bytes.toString();
|
|
|
+ }
|
|
|
+ });
|
|
|
+ resolve(body);
|
|
|
+ } else {
|
|
|
+ resolve('无请求体数据');
|
|
|
+ }
|
|
|
+ });
|
|
|
+}
|
|
|
+
|
|
|
// This method will be called when Electron has finished
|
|
|
// initialization and is ready to create browser windows.
|
|
|
// Some APIs can only be used after this event occurs.
|
|
|
@@ -181,12 +249,17 @@ app.whenReady().then(() => {
|
|
|
// 去掉安全措施
|
|
|
const partitionSession = session.fromPartition(webConfig.webPreferences.partition);
|
|
|
setupCSPRemoval(partitionSession);
|
|
|
- createWindow()
|
|
|
+ // 判断是否需要拦截数据
|
|
|
+ if (webConfig.interceptor && webConfig.interceptor.length > 0) {
|
|
|
+ setupRequestInterceptor(partitionSession);
|
|
|
+ }
|
|
|
+
|
|
|
+ createWindow(partitionSession)
|
|
|
|
|
|
app.on('activate', function () {
|
|
|
// On macOS it's common to re-create a window in the app when the
|
|
|
// dock icon is clicked and there are no other windows open.
|
|
|
- if (BrowserWindow.getAllWindows().length === 0) createWindow()
|
|
|
+ if (BrowserWindow.getAllWindows().length === 0) createWindow(partitionSession)
|
|
|
})
|
|
|
})
|
|
|
|