Explorar o código

拦截到请求

PUGE hai 6 meses
pai
achega
98f4a73a30
Modificáronse 3 ficheiros con 95 adicións e 73 borrados
  1. 10 3
      config.json
  2. 77 4
      main.js
  3. 8 66
      preload.js

+ 10 - 3
config.json

@@ -1,16 +1,23 @@
 {
-    "enterURL": "https://www.tiktok.com/@yes_crystalwholesale/live?enter_from_merge=general_search&enter_method=others_photo&search_id=2025090223262442774B7E8537813E76BF&search_keyword=yes%20crystal%20wholesale&search_result_id=6830419904492258306&search_type=general",
+    "enterURL": "https://www.ea.com/ea-sports-fc/ultimate-team/web-app/",
     "width": 1920,
     "height": 1080,
     "webPreferences": {
         "webSecurity": false,
         "nodeIntegration": false,
-        "nativeWindowOpen": false
+        "nativeWindowOpen": false,
+        "preload": "<dir>/preload.js",
+        "worldSafeExecuteJavaScript": true,
+        "enableRemoteModule": false,
+        "safeDialogs": false
     },
     "userAgent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/138.0.0.0 Safari/537.36",
     "autoHideMenuBar": true,
     "redirect":[],
     "redirectURL":"",
     "proxy":"",
-    "noCache": false
+    "noCache": false,
+    "interceptor":[
+        "https://utas.mob.v4.prd.futc-ext.gcp.ea.com/ut/auth"
+    ]
 }

+ 77 - 4
main.js

@@ -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)
   })
 })
 

+ 8 - 66
preload.js

@@ -1,6 +1,13 @@
 const { contextBridge, ipcRenderer } = require('electron');
 
 
+
+// window.open = (url)=>{console.log(url)}
+window.lastText = ''
+window.lastText2 = ''
+
+
+
 contextBridge.exposeInMainWorld('electronAPI', {
   openWindow: (msg) => ipcRenderer.invoke('openWindow', msg),
   closeAllWindow: (msg) => ipcRenderer.send('closeAllWindow', msg),
@@ -18,70 +25,5 @@ contextBridge.exposeInMainWorld('electronAPI', {
   readdir: (msg) => ipcRenderer.send('readdir', msg),
   download: (msg) => ipcRenderer.send('download', msg),
   broadcast: (msg) => ipcRenderer.send('broadcast-message', msg),
-  onBroadcast: (callback) => ipcRenderer.on('message-broadcast', (event, data) => callback(data)),
+  onBroadcast: (callback) => ipcRenderer.on('message-broadcast', (event, data) => callback(data))
 });
-
-
-// window.open = (url)=>{console.log(url)}
-window.lastText = ''
-window.lastText2 = ''
-setInterval(() => {
-  // 快手
-  if (document.querySelectorAll('.virt-list__client .comment-cell')[0]) {
-    let nowMessage = document.querySelectorAll('.virt-list__client .comment-cell')[document.querySelectorAll('.virt-list__client .comment-cell').length - 1]
-    let nowText = nowMessage.querySelector('.comment').innerText
-    if (window.lastText !==  nowText) {
-        window.lastText =  nowText
-        console.log(nowText)
-        ipcRenderer.send('broadcast-message', nowText)
-    }
-  }
-  // 抖音
-  const chatroom = document.querySelectorAll('.webcast-chatroom .webcast-chatroom___content-with-emoji-text')
-  if (chatroom[0]) {
-    const userNameItem = document.querySelectorAll('.webcast-chatroom .hts-live-text-user')
-    let nowText = chatroom[chatroom.length - 1].innerText
-    let nowUser = ''
-    if (userNameItem[userNameItem.length - 1]) userNameItem[userNameItem.length - 1].innerText
-    if (window.lastText !==  nowText) {
-        window.lastText =  nowText
-        console.log(nowText)
-        ipcRenderer.send('broadcast-message', nowUser.replace('@', '') + ':' + nowText)
-    }
-    
-  } else if (document.querySelectorAll('.TNg5meqw')[0]) {
-    let nowMessage = document.querySelectorAll('.TNg5meqw')[document.querySelectorAll('.TNg5meqw').length - 1]
-    let nowText = nowMessage.innerText
-    if (window.lastText !==  nowText) {
-        window.lastText =  nowText
-        console.log(nowText)
-        ipcRenderer.send('broadcast-message', nowText)
-    }
-  }
-
-  // 抖音提醒
-  let dyAlertBox = document.querySelectorAll('.webcast-chatroom___list')
-  if (dyAlertBox[1] && dyAlertBox[1].innerText) {
-    
-    if (window.lastText2 !==  dyAlertBox[1].innerText) {
-        window.lastText2 =  dyAlertBox[1].innerText
-        ipcRenderer.send('broadcast-message', dyAlertBox[1].innerText)
-    }
-  }
-  
-  
-  // 巨量
-  if (document.querySelector('#comment-list-wrapper')) {
-    let timingList = document.querySelectorAll('#comment-list-wrapper>[elementtiming="element-timing"]')
-    let nowMessage = timingList[timingList.length - 1]
-    if (!nowMessage.innerText.includes('主播')) {
-      let nowText = nowMessage.innerText.replace(nowMessage.querySelector('span').innerText, '')
-      if (window.lastText !==  nowText) {
-          window.lastText =  nowText
-          console.log(nowText)
-          ipcRenderer.send('broadcast-message', nowText)
-      }
-    }
-    
-  }
-}, 1000);