Fetch rendered font using Chrome headless browser

断了今生、忘了曾经 提交于 2020-01-03 03:27:28

问题


I have been looking through the Chrome headless browser documentation but unable to found this information so far.

Is it possible to capture the rendered font on a website? This information is available through the Chrome dev console.


回答1:


Puppeteer doesn't expose this API directly, but it's possible to use the raw devtools protocol to get the "Rendered Fonts" information:

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()
})()

The devtools protocol documentation for CSS.getPlatformFontsForNode can be found here: https://chromedevtools.github.io/devtools-protocol/tot/CSS/#method-getPlatformFontsForNode



来源:https://stackoverflow.com/questions/47911613/fetch-rendered-font-using-chrome-headless-browser

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!