import { RunnableScript } from "@pioneerjs/core" ; import { StoreGet } from "../types/setting" ; import { Pioneer } from "@pioneerjs/core" ; import puppeteer from "puppeteer-core" ; import { AllScriptObjects } from "./common/types" ; import { CXUserLoginScript } from "./login/cx.user.login" ; import { CXPhoneLoginScript } from "./login/cx.phone.login" ; import { CXUnitLoginScript } from "./login/cx.unit.login" ; import { ZHSPhoneLoginScript } from "./login/zhs.phone.login" ; import { ZHSStudentIdLoginScript } from "./login/zhs.studentId.login" ; param name param handler returns export async function StartScript < S extends RunnableScript > ( name ) { const SupportRunnableScript = [ CXUserLoginScript , CXPhoneLoginScript , CXUnitLoginScript , ZHSPhoneLoginScript , ZHSStudentIdLoginScript ] ; const setting = StoreGet ( "setting" ) ; if ( ! setting || ! setting ?. script ?. launch ?. binaryPath ) { throw new Error ( "浏览器路径未设置！" ) ; } let chromePath = setting . script . launch . binaryPath ; if ( chromePath ) { const browser = await puppeteer . launch ( { executablePath : chromePath , defaultViewport : null , headless : false , args : [ "--disable-web-security" , "--disable-features=CalculateNativeWinOcclusion" , ] , } ) ; const pioneer = Pioneer . create ( browser ) ; const script = SupportRunnableScript . find ( ( s ) => s . scriptName === name ) ; if ( script ) { await pioneer . startup ( { scripts : [ script ] , events : [ "request" , "response" , "frameattached" , "framedetached" , "framenavigated" ] , } ) ; } const s = pioneer . runnableScripts ?. find ( ( s ) => s . name === name ) as unknown as S ; return s ; } else { console . error ( "找不到 chrome 路径!!!" ) ; } }