forked from iDerekLi/juejin-helper
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
279 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import puppeteer, { Browser, PuppeteerLaunchOptions, Page } from "puppeteer"; | ||
import JuejinHelper from "../index"; | ||
|
||
export default class JuejinBrowser { | ||
juejin: JuejinHelper; | ||
browser: Browser | null = null; | ||
|
||
constructor(juejin: JuejinHelper) { | ||
this.juejin = juejin; | ||
} | ||
|
||
async open(options?: PuppeteerLaunchOptions) { | ||
if (this.browser) { | ||
await this.close(); | ||
} | ||
this.browser = await puppeteer.launch(options); | ||
return this.browser; | ||
} | ||
|
||
async close() { | ||
if (this.browser) { | ||
await this.browser.close(); | ||
this.browser = null; | ||
} | ||
} | ||
|
||
async visitPage(path: string = "", options = {}): Promise<Page> { | ||
const opts = Object.assign( | ||
{ | ||
viewport: { width: 414, height: 820 } | ||
}, | ||
options | ||
); | ||
|
||
const browser = this.browser as Browser; | ||
const page = await browser.newPage(); | ||
|
||
await page.setViewport(opts.viewport); | ||
|
||
const cookiesString = this.juejin.cookie.toString(); | ||
const cookiesArray = cookiesString.split(/;\W+/).map(item => item.split("=")); | ||
const cookies = cookiesArray.map(([name, value]) => ({ name, value, domain: ".juejin.cn" })); | ||
await page.setCookie(...cookies); | ||
await page.goto("https://juejin.cn" + path); | ||
|
||
return page; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import JuejinHelper from ".."; | ||
|
||
const mockCookie = `juejin-cookies`; | ||
|
||
async function run() { | ||
const juejin = new JuejinHelper(); | ||
await juejin.login(mockCookie); | ||
|
||
const browser = juejin.browser(); | ||
|
||
await browser.open(); | ||
|
||
try { | ||
await browser.visitPage("/"); | ||
console.log("掘金首页:页面访问成功"); | ||
} catch (e) { | ||
console.log("掘金首页:页面访问失败"); | ||
} | ||
|
||
try { | ||
await browser.visitPage("/user/center/signin"); | ||
console.log("掘金每日签到:页面访问成功"); | ||
} catch (e) { | ||
console.log("掘金每日签到:页面访问失败"); | ||
} | ||
|
||
// const page = await browser.visitPage("/user/center/signin"); | ||
// const screenshotBuffer = await page.screenshot(); // 获取页面快照 | ||
|
||
await browser.close(); | ||
} | ||
|
||
run(); |
Oops, something went wrong.