使用 Chrome 无头浏览器获取渲染字体

Hyd*_* B. 2 javascript google-chrome puppeteer

我一直在浏览 Chrome 无头浏览器文档,但到目前为止无法找到此信息。

是否可以在网站上捕获渲染的字体?此信息可通过 Chrome 开发者控制台获得。

在此处输入图片说明

Pas*_*asi 5

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();
})();
Run Code Online (Sandbox Code Playgroud)

CSS.getPlatformFontsForNode可以在此处找到devtools 协议文档:https : //chromedevtools.github.io/devtools-protocol/tot/CSS#method-getPlatformFontsForNode