feat: 新增一键打开渠道脚本
This commit is contained in:
@@ -3,14 +3,18 @@ import { ipcMain, app } from 'electron';
|
||||
import { IPC_EVENTS } from '@common/constants';
|
||||
import { launchLocalChrome } from '@main/utils/chrome/launchLocalChrome'
|
||||
import { executeScriptService } from '@main/service/execute-script-service';
|
||||
import { chromium } from 'playwright';
|
||||
import { addStealthInit, connectCdpContext, safeDisconnectBrowser } from '@main/utils/chrome/cdp'
|
||||
import fs from 'fs'
|
||||
import path from 'path'
|
||||
import log from 'electron-log';
|
||||
|
||||
const openedTabIndexByChannelName = new Map<string, number>()
|
||||
|
||||
function getScriptsDir() {
|
||||
return app.isPackaged
|
||||
? path.join(process.resourcesPath, 'scripts')
|
||||
: path.join(process.cwd(), 'src/main/scripts')
|
||||
}
|
||||
|
||||
export function runTaskOperationService() {
|
||||
const executeScriptServiceInstance = new executeScriptService();
|
||||
// 打开渠道
|
||||
@@ -18,51 +22,20 @@ export function runTaskOperationService() {
|
||||
try {
|
||||
await launchLocalChrome()
|
||||
|
||||
const arr = Array.isArray(channels) ? channels : []
|
||||
const keys = arr
|
||||
.map((it: any) => typeof it === 'string' ? it : it.channelName)
|
||||
.filter(Boolean)
|
||||
if (keys.length === 0) return { success: true }
|
||||
const scriptsDir = getScriptsDir()
|
||||
const scriptPath = path.join(scriptsDir, 'open_all_channel.mjs')
|
||||
|
||||
const urlMap: Record<string, string> = {}
|
||||
openedTabIndexByChannelName.clear()
|
||||
|
||||
for (const it of arr) {
|
||||
const name = typeof it === 'string' ? it : it.channelName
|
||||
const url = typeof it === 'string' ? undefined : it?.channelUrl
|
||||
if (name && url) urlMap[name] = url
|
||||
if (Array.isArray(channels)) {
|
||||
for (let i = 0; i < channels.length; i++) {
|
||||
const name = channels[i]?.channelName
|
||||
if (name) openedTabIndexByChannelName.set(String(name), i)
|
||||
}
|
||||
}
|
||||
|
||||
const { browser, context } = await connectCdpContext(chromium)
|
||||
await addStealthInit(context)
|
||||
|
||||
for (let i = 0; i < keys.length; i++) {
|
||||
const key = keys[i]
|
||||
const targetUrl = urlMap[key]
|
||||
|
||||
let pages = await context.pages()
|
||||
|
||||
while (pages.length <= i) {
|
||||
await context.newPage()
|
||||
pages = await context.pages()
|
||||
}
|
||||
|
||||
const page = pages[i]
|
||||
await page.bringToFront()
|
||||
|
||||
if (targetUrl) {
|
||||
const current = page.url()
|
||||
|
||||
if (!current || !current.startsWith(targetUrl)) {
|
||||
await page.goto(targetUrl, { waitUntil: 'domcontentloaded' as any })
|
||||
}
|
||||
}
|
||||
|
||||
openedTabIndexByChannelName.set(key, i)
|
||||
}
|
||||
|
||||
await safeDisconnectBrowser(browser)
|
||||
|
||||
return { success: true }
|
||||
const result = await executeScriptServiceInstance.executeScript(scriptPath, { channels })
|
||||
return { success: true, result }
|
||||
} catch (error) {
|
||||
return { success: false, error: (error as any)?.message || 'open channel failed' }
|
||||
}
|
||||
@@ -74,9 +47,6 @@ export function runTaskOperationService() {
|
||||
// 从options.roomList列表中找到对应的名称
|
||||
const roomType = options.roomList.find((item: any) => item.id === options.roomType);
|
||||
|
||||
// 从roomType找fzName、mtName、dyHotelName、dyHotSpringName、xcName属性,判断是否有值
|
||||
// 如果有值,就找出对应的脚本
|
||||
// 如果没有值,就跳过
|
||||
const pairs: Array<[string, string]> = [
|
||||
['fzName', 'fg_trace.mjs'],
|
||||
['mtName', 'mt_trace.mjs'],
|
||||
@@ -86,9 +56,7 @@ export function runTaskOperationService() {
|
||||
]
|
||||
const scriptEntries = pairs.filter(([prop]) => roomType?.[prop])
|
||||
|
||||
const scriptsDir = app.isPackaged
|
||||
? path.join(process.resourcesPath, 'scripts')
|
||||
: path.join(process.cwd(), 'src/main/scripts')
|
||||
const scriptsDir = getScriptsDir()
|
||||
|
||||
const scriptPaths = scriptEntries.map(([channel, fileName]) => {
|
||||
const p = path.join(scriptsDir, fileName)
|
||||
|
||||
Reference in New Issue
Block a user