03-jietu.js 917 B

12345678910111213141516171819202122232425262728293031323334353637
  1. const puppeteer = require('puppeteer');
  2. const utils = require('./utils/base');
  3. const start = async (utils) => {
  4. const browser = await puppeteer.launch();
  5. const page = await browser.newPage();
  6. await page.goto('http://127.0.0.1:3000');
  7. //调用evaluate 方法返回id 为form元素的位置信息
  8. let clip = await page.evaluate((utils) => {
  9. // 设置标题
  10. const title = document.getElementById('title');
  11. const author = document.getElementById('author');
  12. title.innerText = utils.name;
  13. author.innerText = utils.author;
  14. let {
  15. x,
  16. y,
  17. width,
  18. height
  19. } = document.getElementById('page').getBoundingClientRect();
  20. return {
  21. x,
  22. y,
  23. width,
  24. height
  25. };
  26. }, utils);
  27. await page.screenshot({
  28. path: './book/OEBPS/Images/cover.jpg',
  29. clip: clip //设置clip 属性
  30. });
  31. await page.close();
  32. await browser.close();
  33. }
  34. start(utils);