答案 0 :(得分:3)
Puppeteer没有直接暴露这个API,但是可以使用原始的devtools协议来获取"渲染字体"信息:
const puppeteer = require('puppeteer');
(async () => {
const browser = await puppeteer.launch()
const page = await browser.newPage()
await page.goto('https://www.stackoverflow.com/')
await page._client.send('DOM.enable')
await page._client.send('CSS.enable')
const doc = await page._client.send('DOM.getDocument')
const node = await page._client.send('DOM.querySelector', {nodeId: doc.root.nodeId, selector: 'h1'})
const fonts = await page._client.send('CSS.getPlatformFontsForNode', {nodeId: node.nodeId})
console.log(fonts)
await browser.close()
})()
CSS.getPlatformFontsForNode
的devtools协议文档可在此处找到:https://chromedevtools.github.io/devtools-protocol/tot/CSS/#method-getPlatformFontsForNode