change package name from launch-browser-with-preload-extension to launch-bosszhipin-login-page-with-preload-extension

This commit is contained in:
geekgeekrun
2024-03-01 23:45:14 +08:00
parent d0c8dc0410
commit 7f2e241773
6 changed files with 60 additions and 127 deletions
@@ -7,6 +7,7 @@ import {
sleepWithRandomDelay
} from '@geekgeekrun/utils/sleep.mjs'
import extractZip from 'extract-zip'
import { blockNavigation } from '@geekgeekrun/utils/puppeteer/block-navigation.mjs'
import fs from 'node:fs'
import os from 'node:os'
@@ -50,10 +51,36 @@ async function main() {
const browser = await puppeteer.launch({
headless: false,
args: [
`--load-extension=${editThisCookieExtensionPath}`,
`https://www.zhipin.com/web/user/`
`--load-extension=${editThisCookieExtensionPath}`
]
})
const closeAttachedSet = new WeakSet()
browser.on('targetcreated', async function closeNewTabs(target) {
let targetBrowser = target.browser();
const pages = await targetBrowser.pages()
console.log(pages)
for (let i = 1; i < pages.length; i++) {
const page = pages[i]
if (!closeAttachedSet.has(page)) {
closeAttachedSet.add(page)
page.once('domcontentloaded', () => {
page.evaluate(() => {
window.close()
})
})
}
}
})
const [page] = await browser.pages();
page.once('close', () => {
browser.close()
})
const { dispose: disposeNavigation } = await blockNavigation(page, (req) => !req.url().startsWith('https://www.zhipin.com'))
await page.goto('https://www.zhipin.com/web/user/');
}
main()
@@ -1,8 +1,8 @@
{
"name": "@geekgeekrun/launch-browser-with-preload-extension",
"name": "@geekgeekrun/launch-bosszhipin-login-page-with-preload-extension",
"private": true,
"version": "1.0.0",
"description": "launch-browser-with-preload-extension",
"description": "launch-bosszhipin-login-page-with-preload-extension",
"module": "./index.mjs",
"type": "module",
"scripts": {},
@@ -0,0 +1,25 @@
export async function blockNavigation(page, predictor = (url) => true) {
console.log(`block navigation for puppeteer page from url ${page.url()}`)
await page.setRequestInterception(true)
const handler = (req) => {
if (req.isNavigationRequest() && req.frame() === page.mainFrame() && predictor(req)) {
req.abort('aborted')
} else {
try {
req.continue()
} catch {
//
}
}
}
page.on('request', handler)
return {
dispose: async () => {
page.off('request', handler)
await page.setRequestInterception(false)
}
}
}