最佳答案
Puppeteer 不直接公开此 API,但可以使用原始开发工具协议(protocol)获取“渲染字体”信息:
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
的开发工具协议(protocol)文档:https://chromedevtools.github.io/devtools-protocol/tot/CSS#method-getPlatformFontsForNode
关于javascript - 使用 Chrome headless 浏览器获取呈现的字体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47911613/