-
Notifications
You must be signed in to change notification settings - Fork 10
/
index.ts
76 lines (73 loc) · 1.91 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import type { PageInfo, Plugin } from "@web-printer/core"
import { evaluateWaitForImgLoad, scrollLoading } from "@web-printer/core"
export default function (options: {
/**
* Url of a article list page
* @example
* - "https://juejin.cn/frontend"
* - "https://juejin.cn/tag/JavaScript"
* - "https://juejin.cn/column/6960944886115729422"
*/
url: string
/**
* When the article list page has a lot of articles, you can set maxPages to limit, especially endless loading.
* @default Infinity
*/
maxPages?: number
/**
* Interval of each scroll
* @default 500
* @unit ms
*/
interval?: number
}): Plugin {
const { url, interval = 500, maxPages = Infinity } = options
if (!url) throw new Error("url is required")
return {
async fetchPagesInfo({ context }) {
const page = await context.newPage()
await page.goto(url)
const fetchItemsNum = async () =>
Number(
await page.evaluate(
`document.querySelectorAll(".entry .title-row a").length`
)
)
await scrollLoading(page, fetchItemsNum, {
interval,
maxPages
})
const pagesInfo: PageInfo[] = JSON.parse(
await page.evaluate(`
(() => {
const ret = [...document.querySelectorAll(".entry .title-row a")].map(k=>({title: k.innerText, url:k.href}))
return JSON.stringify(ret)
})()
`)
)
await page.close()
return pagesInfo.filter(k => !k.title.includes("掘金")).slice(0, maxPages)
},
async onPageLoaded({ page }) {
await evaluateWaitForImgLoad(page, "article img")
},
injectStyle() {
const style = `
.copy-code-btn,
.ategory-course-recommend {
display: none !important;
}
html {
background-color: #fff !important;
}
.markdown-body {
background-image: unset !important;
}
`
return {
style,
contentSelector: "article"
}
}
}
}