首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >chromedp -如何从控制台检索FCP等

chromedp -如何从控制台检索FCP等
EN

Stack Overflow用户
提问于 2022-11-16 10:42:59
回答 1查看 37关注 0票数 1

我正在建造一个个人刮板使用铬包。我想得到我的FCP(第一次油漆)和其他统计数据。不幸的是,我无法找到一种方法来做到这一点,然后我得到了从开发人员控制台提取它的想法。

不幸的是,经过两天的摆弄,我在网上找不到任何教程。

有人能告诉我如何用chromedp测量FCP和其他指标吗?

我尝试过这样做,但它返回了一个错误:遇到了一个未定义的值。但是,当我把它输入浏览器的控制台时,它实际上是起作用的。

代码语言:javascript
复制
chromedp.EvaluateAsDevTools("const paintTimings = performance.getEntriesByType('paint');", nil),
chromedp.EvaluateAsDevTools("const fmp = paintTimings.find(({ name }) => name === \"first-contentful-paint\");", nil),
chromedp.EvaluateAsDevTools("console.log('First contentful paint at foo');", &jscript))

我发现我遇到的问题是,当执行console.log时,devtools还返回一个未定义的与Go期望的冲突。有人知道怎么解决这个问题吗?

EN

回答 1

Stack Overflow用户

发布于 2022-11-18 07:22:11

我不明白FCP是如何测量的。我将提供一个演示,从以下铬的角度工作。

最好使用cdp性能域和/或cdp PerformanceTimeline域,但正如我之前所说的,我不知道FCP,也不知道如何使用它们。

代码语言:javascript
复制
package main

import (
    "context"
    "encoding/json"
    "log"

    "github.com/chromedp/chromedp"
)

type performancePaintTiming struct {
    EntryType string  `json:"entryType"`
    Name      string  `json:"name"`
    Duration  int     `json:"duration"`
    StartTime float64 `json:"startTime"`
}

func main() {
    ctx, cancel := chromedp.NewContext(context.Background(),
        // Enable the debug mode to see the CDP messages.
        // It's helpful to understand how CDP works.
        // But please don't enable it in Production Environment.
        chromedp.WithDebugf(log.Printf))
    defer cancel()

    // I don't know why but it seems that Runtime.evaluate does not return
    // JSON object any more, so I have to stringified it into a string.
    js := `
const paintTimings = performance.getEntriesByType('paint');
const fcp = paintTimings.find(({ name }) => name === "first-contentful-paint");
JSON.stringify(fcp);`

    var res string
    if err := chromedp.Run(ctx,
        chromedp.Navigate("https://www.bing.com/"),
        chromedp.EvaluateAsDevTools(js, &res),
    ); err != nil {
        panic(err)
    }

    var fcp performancePaintTiming
    if err := json.Unmarshal([]byte(res), &fcp); err != nil {
        panic(err)
    }

    log.Printf("%#v", fcp)
}

参考文献:

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/74459066

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档