Skip to content

Commit

Permalink
feat(puppeteer): support selector
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed Mar 5, 2021
1 parent b338c44 commit 278f85d
Showing 1 changed file with 14 additions and 6 deletions.
20 changes: 14 additions & 6 deletions packages/plugin-puppeteer/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import puppeteer from 'puppeteer-core'
import puppeteer, { Shooter } from 'puppeteer-core'
import { Context } from 'koishi-core'
import { Logger, defineProperty, noop, segment } from 'koishi-utils'
import { escape } from 'querystring'
Expand All @@ -15,12 +15,17 @@ declare module 'puppeteer-core/lib/types' {
encoding?: 'binary'
}

interface Page {
interface Shooter {
screenshot(options?: Base64ScreenshotOptions): Promise<string>
screenshot(options?: BinaryScreenshotOptions): Promise<Buffer>
}

interface ElementHandle {
interface Page extends Shooter {
screenshot(options?: Base64ScreenshotOptions): Promise<string>
screenshot(options?: BinaryScreenshotOptions): Promise<Buffer>
}

interface ElementHandle extends Shooter {
screenshot(options?: Base64ScreenshotOptions): Promise<string>
screenshot(options?: BinaryScreenshotOptions): Promise<Buffer>
}
Expand Down Expand Up @@ -79,11 +84,11 @@ export function apply(ctx: Context, config: Config = {}) {
})

const ctx1 = ctx.intersect(sess => !!sess.app.browser)
ctx1.command('shot <url>', '网页截图', { authority: 2 })
ctx1.command('shot <url> [selector:text]', '网页截图', { authority: 2 })
.alias('screenshot')
.option('full', '-f 对整个可滚动区域截图')
.option('viewport', '-v <viewport> 指定视口', { type: 'string' })
.action(async ({ session, options }, url) => {
.action(async ({ session, options }, url, selector) => {
if (!url) return '请输入网址。'
const scheme = /^(\w+):\/\//.exec(url)
if (!scheme) {
Expand Down Expand Up @@ -135,7 +140,10 @@ export function apply(ctx: Context, config: Config = {}) {
return '无法打开页面。'
}

return page.screenshot({
const shooter: Shooter = selector ? await page.$(selector) : page
if (!shooter) return '找不到满足该选择器的元素。'

return shooter.screenshot({
fullPage: options.full,
}).then(async (buffer) => {
if (buffer.byteLength > config.maxLength) {
Expand Down

0 comments on commit 278f85d

Please sign in to comment.