add the feature to prevent startup chat for an inactive boss

This commit is contained in:
geekgeekrun
2024-11-23 13:28:26 +08:00
parent 1522907afe
commit ad7917d1fb
@@ -68,6 +68,7 @@ const enableCompanyAllowList = Boolean(expectCompanySet.size)
let browser, page let browser, page
const blockBossNotNewChat = new Set() const blockBossNotNewChat = new Set()
const blockBossNotActive = new Set()
async function toRecommendPage (hooks) { async function toRecommendPage (hooks) {
let userInfoPromise = page.waitForResponse((response) => { let userInfoPromise = page.waitForResponse((response) => {
@@ -152,25 +153,8 @@ async function toRecommendPage (hooks) {
} }
try { try {
const { targetJobElProxy, targetJobIndex } = await new Promise(async (resolve, reject) => { const { targetJobIndex, targetJobData } = await new Promise(async (resolve, reject) => {
try { try {
// job list
const recommendJobListElProxy = await page.$('.job-list-container .rec-job-list')
let jobListData = await page.evaluate(
`
document.querySelector('.job-recommend-main')?.__vue__?.jobList
`
)
// when disable company allow list, we will believe that the first one in the list is your expect job.
let targetJobIndex = enableCompanyAllowList ? jobListData.findIndex(
it => !blockBossNotNewChat.has(it.encryptBossId) && [...expectCompanySet].find(name => it.brandName.includes(name))
) : jobListData.findIndex(
it => !blockBossNotNewChat.has(it.encryptBossId)
)
let hasReachLastPage = false
let requestNextPagePromiseWithResolver = null let requestNextPagePromiseWithResolver = null
page.on( page.on(
'request', 'request',
@@ -199,7 +183,20 @@ async function toRecommendPage (hooks) {
} }
) )
while (targetJobIndex < 0 && !hasReachLastPage) { // job list
const recommendJobListElProxy = await page.$('.job-list-container .rec-job-list')
let jobListData = await page.evaluate(`document.querySelector('.job-recommend-main')?.__vue__?.jobList`)
let hasReachLastPage = false
let targetJobIndex = -1
let targetJobData
continueFind: while (targetJobIndex < 0 && !hasReachLastPage) {
// when disable company allow list, we will believe that the first one in the list is your expect job.
let tempTargetJobIndexToCheckDetail = enableCompanyAllowList ? jobListData.findIndex(
it => !blockBossNotNewChat.has(it.encryptBossId) && !blockBossNotActive.has(it.encryptBossId) && [...expectCompanySet].find(name => it.brandName.includes(name))
) : jobListData.findIndex(
it => !blockBossNotNewChat.has(it.encryptBossId) && !blockBossNotActive.has(it.encryptBossId)
)
while (tempTargetJobIndexToCheckDetail < 0 && !hasReachLastPage) {
// fetch new // fetch new
const recommendJobListElBBox = await recommendJobListElProxy.boundingBox() const recommendJobListElBBox = await recommendJobListElProxy.boundingBox()
const windowInnerHeight = await page.evaluate('window.innerHeight') const windowInnerHeight = await page.evaluate('window.innerHeight')
@@ -233,30 +230,20 @@ async function toRecommendPage (hooks) {
document.querySelector('.job-recommend-main')?.__vue__?.jobList document.querySelector('.job-recommend-main')?.__vue__?.jobList
` `
) )
targetJobIndex = jobListData.findIndex(it => !blockBossNotNewChat.has(it.encryptBossId) && [...expectCompanySet].find(name => it.brandName.includes(name))) tempTargetJobIndexToCheckDetail = jobListData.findIndex(it => !blockBossNotNewChat.has(it.encryptBossId) && !blockBossNotActive.has(it.encryptBossId) && [...expectCompanySet].find(name => it.brandName.includes(name)))
} }
if (targetJobIndex < 0 && hasReachLastPage) { if (tempTargetJobIndexToCheckDetail < 0 && hasReachLastPage) {
// has reach last page and not find target job // has reach last page and not find target job
reject(new Error('CANNOT_FIND_EXCEPT_JOB')) reject(new Error('CANNOT_FIND_EXCEPT_JOB'))
return return
} }
const recommendJobItemList = await recommendJobListElProxy.$$('ul.rec-job-list > li') //#region here to check detail
resolve( if (tempTargetJobIndexToCheckDetail >= 0) {
{
targetJobElProxy: recommendJobItemList[targetJobIndex],
targetJobIndex
}
)
} catch(err) {
reject(err)
}
})
if (targetJobIndex >= 0) {
// scroll that target element into view // scroll that target element into view
await page.evaluate(` await page.evaluate(`
targetEl = document.querySelector("ul.rec-job-list").children[${targetJobIndex}] targetEl = document.querySelector("ul.rec-job-list").children[${tempTargetJobIndexToCheckDetail}]
targetEl.scrollIntoView({ targetEl.scrollIntoView({
behavior: 'smooth', behavior: 'smooth',
block: ${Math.random() > 0.5 ? '\'center\'' : '\'end\''} block: ${Math.random() > 0.5 ? '\'center\'' : '\'end\''}
@@ -265,8 +252,10 @@ async function toRecommendPage (hooks) {
await sleepWithRandomDelay(200) await sleepWithRandomDelay(200)
if (targetJobIndex === 0) { if (tempTargetJobIndexToCheckDetail === 0) {
} else { } else {
const recommendJobItemList = await recommendJobListElProxy.$$('ul.rec-job-list > li')
const targetJobElProxy = recommendJobItemList[tempTargetJobIndexToCheckDetail]
// click that element // click that element
await targetJobElProxy.click() await targetJobElProxy.click()
await page.waitForResponse( await page.waitForResponse(
@@ -281,20 +270,66 @@ async function toRecommendPage (hooks) {
); );
await sleepWithRandomDelay(2000) await sleepWithRandomDelay(2000)
} }
const jobData = await page.evaluate('document.querySelector(".job-detail-box").__vue__.data') targetJobData = await page.evaluate('document.querySelector(".job-detail-box").__vue__.data')
// save the job detail info // save the job detail info
await hooks.jobDetailIsGetFromRecommendList?.promise(jobData) await hooks.jobDetailIsGetFromRecommendList?.promise(targetJobData)
//#region
// null
// 刚刚活跃 // 今日活跃 // 昨日活跃 // 3日内活跃 // 本周活跃
// 2周内活跃 // 本月活跃 // 2月内活跃 // 3月内活跃 // 4月内活跃 // 5月内活跃 // 近半年活跃 // 半年前活跃
//#endregion
if ([
'2周内活跃',
'本月活跃',
'2月内活跃',
'3月内活跃',
'4月内活跃',
'5月内活跃',
'近半年活跃',
'半年前活跃',
].includes(targetJobData.bossInfo.activeTimeDesc)) {
blockBossNotActive.add(targetJobData.jobInfo.encryptUserId)
continue continueFind
}
const startChatButtonInnerHTML = await page.evaluate('document.querySelector(".job-detail-box .op-btn.op-btn-chat")?.innerHTML.trim()') const startChatButtonInnerHTML = await page.evaluate('document.querySelector(".job-detail-box .op-btn.op-btn-chat")?.innerHTML.trim()')
if (startChatButtonInnerHTML === '立即沟通') { if (startChatButtonInnerHTML !== '立即沟通') {
await hooks.newChatWillStartup?.promise(jobData) blockBossNotNewChat.add(targetJobData.jobInfo.encryptUserId)
continue continueFind
}
targetJobIndex = tempTargetJobIndexToCheckDetail
//#endregion
}
if (targetJobIndex < 0 && hasReachLastPage) {
// has reach last page and not find target job
reject(new Error('CANNOT_FIND_EXCEPT_JOB'))
return
}
}
resolve(
{
targetJobIndex,
targetJobData
}
)
} catch(err) {
reject(err)
}
})
await sleepWithRandomDelay(200)
const startChatButtonInnerHTML = await page.evaluate('document.querySelector(".job-detail-box .op-btn.op-btn-chat")?.innerHTML.trim()')
await hooks.newChatWillStartup?.promise(targetJobData)
const startChatButtonProxy = await page.$('.job-detail-box .op-btn.op-btn-chat') const startChatButtonProxy = await page.$('.job-detail-box .op-btn.op-btn-chat')
//#region click the chat button
await startChatButtonProxy.click() await startChatButtonProxy.click()
const addFriendResponse = await page.waitForResponse( const addFriendResponse = await page.waitForResponse(
response => { response => {
if ( if (
response.url().startsWith('https://www.zhipin.com/wapi/zpgeek/friend/add.json') && response.url().includes(`jobId=${jobData.jobInfo.encryptId}`) response.url().startsWith('https://www.zhipin.com/wapi/zpgeek/friend/add.json') && response.url().includes(`jobId=${targetJobData.jobInfo.encryptId}`)
) { ) {
return true return true
} }
@@ -313,8 +348,8 @@ async function toRecommendPage (hooks) {
throw new Error('STARTUP_CHAT_ERROR_WITH_UNKNOWN_ERROR') throw new Error('STARTUP_CHAT_ERROR_WITH_UNKNOWN_ERROR')
} }
} else { } else {
await hooks.newChatStartup?.promise(jobData) await hooks.newChatStartup?.promise(targetJobData)
blockBossNotNewChat.add(jobData.jobInfo.encryptUserId) blockBossNotNewChat.add(targetJobData.jobInfo.encryptUserId)
await storeStorage(page).catch(() => void 0) await storeStorage(page).catch(() => void 0)
await sleepWithRandomDelay(750) await sleepWithRandomDelay(750)
@@ -322,10 +357,7 @@ async function toRecommendPage (hooks) {
await closeDialogButtonProxy.click() await closeDialogButtonProxy.click()
await sleepWithRandomDelay(1000) await sleepWithRandomDelay(1000)
} }
} else { // #endregion
blockBossNotNewChat.add(jobData.jobInfo.encryptUserId)
}
}
} catch (err) { } catch (err) {
if (err instanceof Error) { if (err instanceof Error) {
switch (err.message) { switch (err.message) {