Skip to content

Commit

Permalink
chore: 添加浏览器api
Browse files Browse the repository at this point in the history
  • Loading branch information
iDerekLi committed Jan 13, 2023
1 parent f56737a commit de90cc7
Show file tree
Hide file tree
Showing 6 changed files with 279 additions and 10 deletions.
33 changes: 33 additions & 0 deletions packages/juejin-helper/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,39 @@ async function run() {
run();
```

### Browser 浏览器访问示例
```javascript
const JuejinHelper = require("juejin-helper");

async function run() {
const juejin = new JuejinHelper();
await juejin.login("你的掘金Cookie");

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("/");
// const screenshotBuffer = await page.screenshot(); // 获取页面快照

await browser.close();
}
```

## 问题

### 如何获取Cookie
Expand Down
2 changes: 1 addition & 1 deletion packages/juejin-helper/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "juejin-helper",
"version": "1.7.0",
"version": "1.6.1",
"description": "稀土掘金助手:签到、抽奖、沾喜气、海底淘金、BugFix、自动化工作流。",
"author": "Derek Li",
"license": "Apache-2.0",
Expand Down
5 changes: 5 additions & 0 deletions packages/juejin-helper/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import Growth from "./growth";
import SeaGold from "./seagold";
import NumPuzz from "./numpuzz";
import Bugfix from "./bugfix";
import JuejinBrowser from "./utils/browser";

type JuejinUserProps = {
user_id: string;
Expand Down Expand Up @@ -76,6 +77,10 @@ class JuejinHelper {
bugfix() {
return new Bugfix(this);
}

browser() {
return new JuejinBrowser(this);
}
}

export default JuejinHelper;
48 changes: 48 additions & 0 deletions packages/juejin-helper/src/utils/browser.ts
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;
}
}
33 changes: 33 additions & 0 deletions packages/juejin-helper/test/test-browser.ts
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();
Loading

0 comments on commit de90cc7

Please sign in to comment.