Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug: @koishijs/plugin-puppeteer 中关于 browser.viewPort 的设置不起作用 #593

Closed
Dup4 opened this issue Apr 5, 2022 · 1 comment
Labels
bug BUG

Comments

@Dup4
Copy link
Contributor

Dup4 commented Apr 5, 2022

Describe the bug

image

以上是 browser.viewPort 的默认值。

我设置为如下配置:

puppeteer:
  browser:
    defaultViewport:
      width: 1920
      height: 1080

但是截图仍然是 800x600 的。

Steps to reproduce

根据上述描述可以轻易重现。

Expected behavior

截图的默认视口大小应为 1920x1080

Screenshots

No response

Versions

  • OS: macOS 12.3
  • Platform: OneBot
  • Node version: 16.11.0
  • Koishi version: 4.5.0
  • puppeteer-core version: 13.5.2

Additional context

const LaunchOptions = Schema.object({
executablePath: Schema.string().description('Chromium 可执行文件的路径。缺省时将自动从系统中寻找。'),
viewPort: Schema.object({
width: Schema.natural().description('默认的视图宽度。').default(800),
height: Schema.natural().description('默认的视图高度。').default(600),
deviceScaleFactor: Schema.number().min(0).description('默认的设备缩放比率。').default(2),
}),
}).description('浏览器设置')

我查看过相关源代码,问题可能出在 23 行的 viewPort

async start() {
this.browser = await puppeteer.launch(this.config.browser)
logger.debug('browser launched')

48 行直接拿 config 传入 puppeteer.launch 作为 options 参数。

https://github.com/puppeteer/puppeteer/blob/24e1469e15bab88031650dcdd890edf499fb5c25/src/common/BrowserConnector.ts#L31-L50

但是 puppeteer.launchoptions 对应的设置 viewPort 的参数名应该是 defaultViewport

所以其实简单 fix 的话,用以下的 patch 就能修复。

diff --git a/plugins/puppeteer/src/index.ts b/plugins/puppeteer/src/index.ts
index cb4cec3..27d189b 100644
--- a/plugins/puppeteer/src/index.ts
+++ b/plugins/puppeteer/src/index.ts
@@ -21,7 +21,7 @@ type LaunchOptions = Parameters<typeof puppeteer.launch>[0]
 
 const LaunchOptions = Schema.object({
   executablePath: Schema.string().description('Chromium 可执行文件的路径。缺省时将自动从系统中寻找。'),
-  viewPort: Schema.object({
+  defaultViewport: Schema.object({
     width: Schema.natural().description('默认的视图宽度。').default(800),
     height: Schema.natural().description('默认的视图高度。').default(600),
     deviceScaleFactor: Schema.number().min(0).description('默认的设备缩放比率。').default(2),

如果认可这个 fix,我可以提一个 PR.

@shigma
Copy link
Member

shigma commented Apr 5, 2022

非常感谢!似乎是因为 puppeteer 更新导致了 API 变更。

@shigma shigma closed this as completed in 868681d Apr 5, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug BUG
Projects
None yet
Development

No branches or pull requests

2 participants