mirror of
https://github.com/geekgeekrun/geekgeekrun.git
synced 2026-09-09 09:27:07 +08:00
add logic to detect and handle login invalid - will exit auto start chat and show cookie assistant. add the logic to store cookie and local storage while auto start chat running.
This commit is contained in:
@@ -8,13 +8,16 @@ import os from 'node:os'
|
||||
import { get__dirname } from '@geekgeekrun/utils/legacy-path.mjs';
|
||||
import path from 'node:path';
|
||||
import JSON5 from 'json5'
|
||||
import { EventEmitter } from 'node:events'
|
||||
import { setDomainLocalStorage } from '@geekgeekrun/utils/puppeteer/local-storage.mjs'
|
||||
|
||||
import { readConfigFile, ensureConfigFileExist, readStorageFile, ensureStorageFileExist } from './runtime-file-utils.mjs'
|
||||
import { readConfigFile, writeStorageFile, ensureConfigFileExist, readStorageFile, ensureStorageFileExist } from './runtime-file-utils.mjs'
|
||||
ensureConfigFileExist()
|
||||
ensureStorageFileExist()
|
||||
|
||||
const isRunFromUi = Boolean(process.env.MAIN_BOSSGEEKGO_UI_RUN_MODE)
|
||||
const isUiDev = process.env.NODE_ENV === 'development'
|
||||
export const autoStartChatEventBus = new EventEmitter()
|
||||
|
||||
let puppeteer, StealthPlugin
|
||||
export async function initPuppeteer () {
|
||||
@@ -52,9 +55,11 @@ export async function initPuppeteer () {
|
||||
}
|
||||
|
||||
const bossCookies = readStorageFile('boss-cookies.json')
|
||||
const bossLocalStorage = readStorageFile('boss-local-storage.json')
|
||||
|
||||
const targetCompanyList = readConfigFile('target-company-list.json')
|
||||
|
||||
const localStoragePageUrl = `https://www.zhipin.com/desktop/`
|
||||
const recommendJobPageUrl = `https://www.zhipin.com/web/geek/job-recommend`
|
||||
|
||||
const expectCompanySet = new Set(targetCompanyList)
|
||||
@@ -85,18 +90,40 @@ export async function mainLoop (hooks) {
|
||||
})
|
||||
|
||||
//set cookies
|
||||
const copiedBossCookies = JSON.parse(JSON.stringify(bossCookies))
|
||||
|
||||
hooks.cookieWillSet?.call(copiedBossCookies)
|
||||
for(let i = 0; i < copiedBossCookies.length; i++){
|
||||
await page.setCookie(copiedBossCookies[i]);
|
||||
hooks.cookieWillSet?.call(bossCookies)
|
||||
for(let i = 0; i < bossCookies.length; i++){
|
||||
await page.setCookie(bossCookies[i]);
|
||||
}
|
||||
|
||||
await setDomainLocalStorage(browser, localStoragePageUrl, bossLocalStorage)
|
||||
debugger
|
||||
|
||||
let userInfoResponse
|
||||
await Promise.all([
|
||||
page.goto(recommendJobPageUrl, { timeout: 0 }),
|
||||
page.waitForResponse((response) => {
|
||||
if (response.url().startsWith('https://www.zhipin.com/wapi/zpuser/wap/getUserInfo.json')) {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}).then((res) => {
|
||||
return res.json()
|
||||
}).then((res) => {
|
||||
userInfoResponse = res
|
||||
}),
|
||||
page.waitForNavigation(),
|
||||
])
|
||||
hooks.pageLoaded?.call()
|
||||
hooks.userInfoResponse?.call(userInfoResponse)
|
||||
|
||||
if (userInfoResponse.code !== 0) {
|
||||
autoStartChatEventBus.emit('LOGIN_STATUS_INVALID', {
|
||||
userInfoResponse
|
||||
})
|
||||
writeStorageFile('boss-cookies.json', [])
|
||||
throw new Error("LOGIN_STATUS_INVALID")
|
||||
} else {
|
||||
await storeStorage().catch(() => void 0)
|
||||
}
|
||||
|
||||
const INIT_START_EXCEPT_JOB_INDEX = 1
|
||||
let currentExceptJobIndex = INIT_START_EXCEPT_JOB_INDEX
|
||||
@@ -323,6 +350,23 @@ export async function mainLoop (hooks) {
|
||||
|
||||
export async function closeBrowserWindow () {
|
||||
browser?.close()
|
||||
browse = null
|
||||
browser = null
|
||||
page = null
|
||||
}
|
||||
|
||||
async function storeStorage (page) {
|
||||
const [
|
||||
cookies, localStorage
|
||||
] = await Promise.all([
|
||||
page.cookies(),
|
||||
page.evaluate(() => {
|
||||
return JSON.stringify(window.localStorage)
|
||||
}).then(res => JSON.parse(res))
|
||||
])
|
||||
return Promise.all(
|
||||
[
|
||||
writeStorageFile('boss-cookies.json', cookies),
|
||||
writeStorageFile('boss-local-storage.json', localStorage),
|
||||
]
|
||||
)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user