gpt4 book ai didi

javascript - 使用 Puppeteer.js 截取 URL 列表的屏幕截图

转载 作者:行者123 更新时间:2023-12-01 02:05:04 28 4
gpt4 key购买 nike

假设我有 URL 列表或 SVG 文件的完整路径,现在我想为每个 URL 逐一截取屏幕截图。

这是测试代码,我用来一张一张截图,但它没有按预期工作!

这段代码只是为所有 URL 启动一次 headless chrome 页面实例,node.js 会抛出此错误

(node:3412) MaxListenersExceededWarning: Possible EventEmitter memory leak detected. 11 lifecycleevent listeners added. Use emitter.setMaxListeners() to increase limit

但我想一张一张地截图。

'use strict';

const fs = require('fs');
const glob = require('glob');
const validUrl = require("valid-url")
const puppeteer = require('puppeteer');
const devices = require('puppeteer/DeviceDescriptors');
const iPhone = devices['iPhone 6'];

/**
* Loading Application Config
*/
const inputUrl = 'http://www.google.com';

/**
* Using Puppeteer.js
*/
console.log('-- Trying to Launch Puppeteer');
const browser = puppeteer.launch({
headless: true
}).then(function(browserObj)
{
console.log('-- Trying to Open New Page');
browserObj.newPage().then(function(pageObj)
{
async function closeHeadlesssChrome(browserObj) {
console.log('-- Trying to Close Chome Headless Window');
await browserObj.close();
}

async function setChromeViewport(pageObj) {
console.log('-- Trying to Update page viewPort');
await pageObj.setViewport({
width: 1366,
height: 738,
deviceScaleFactor: 1,
isMobile: false,
hasTouch: false,
isLandscape: false
});
}

var takeScreenshot = async function(pageObj, srcUrl) {
console.log('-- Trying to Load Web Page ' + srcUrl);
await pageObj.goto(srcUrl);

console.log('-- Trying to Take Screenshot');
await pageObj.screenshot({
path: srcUrl + '.png',
clip: {
x: 0,
y: 0,
width: 795,
height: 1125
}
})
}


// Input or Source Url
const inputUrl = "C:/Users/ssp/Music/BR PUBLIC INTER COLLEGE";
var matchedFiles = [];

// Check if given Url/Path exists
if (fs.existsSync(inputUrl))
{
const inputUrlObj = fs.statSync(inputUrl);
if (inputUrlObj.isDirectory())
{
matchedFiles = glob.GlobSync(inputUrl + '/**/*.svg').found;
}
else if (inputUrlObj.isFile())
{
matchedFiles.push(inputUrl );
}
}
else
{
console.log('-- Input Url not exists')
return closeHeadlesssChrome(browserObj);
}

setChromeViewport(pageObj);

matchedFiles.map(function(srcUrl){
takeScreenshot(pageObj, srcUrl);
});
});
});

谢谢

最佳答案

使用 for..ofasync-await 而不是 .map.map 不会暂停执行,但 await 会暂停。

browserObj.newPage().then(async function(pageObj) { // <-- turn the main function into async function
// ... many lines later
for(let srcUrl of matchedFiles){
await takeScreenshot(pageObj, srcUrl);
}

旁注:由于所有函数都是相互独立的,也许您可​​以将它们移到 block 之外并在调用 browserObj.newPage() 之前声明它们

这是重构的代码,如果有问题请原谅,但你明白了。

"use strict";

const fs = require("fs");
const glob = require("glob");
const validUrl = require("valid-url");
const puppeteer = require("puppeteer");
const devices = require("puppeteer/DeviceDescriptors");
const iPhone = devices["iPhone 6"];

/**
* Loading Application Config
*/
const inputUrl = "http://www.google.com";

/**
* Controller functions
*/

async function closeHeadlesssChrome(browserObj) {
console.log("-- Trying to Close Chome Headless Window");
await browserObj.close();
}

async function setChromeViewport(pageObj) {
console.log("-- Trying to Update page viewPort");
await pageObj.setViewport({
width: 1366,
height: 738,
deviceScaleFactor: 1,
isMobile: false,
hasTouch: false,
isLandscape: false
});
}

var takeScreenshot = async function(pageObj, srcUrl) {
console.log("-- Trying to Load Web Page " + srcUrl);
await pageObj.goto(srcUrl);

console.log("-- Trying to Take Screenshot");
await pageObj.screenshot({
path: srcUrl + ".png",
clip: {
x: 0,
y: 0,
width: 795,
height: 1125
}
});
};

function getURLList() {
// Input or Source Url
const inputUrl = "C:/Users/ssp/Music/BR PUBLIC INTER COLLEGE";
var matchedFiles = [];

// Check if given Url/Path exists
if (fs.existsSync(inputUrl)) {
const inputUrlObj = fs.statSync(inputUrl);
if (inputUrlObj.isDirectory()) {
matchedFiles = glob.GlobSync(inputUrl + "/**/*.svg").found;
} else if (inputUrlObj.isFile()) {
matchedFiles.push(inputUrl);
}
return matchedFiles;
}
}

/**
* Using Puppeteer.js
*/

(async () => {
// get url list
const matchedFiles = getURLList();
if (!matchedFiles) {
console.log("-- Input Url not exists");
// if there is no url, then no need to even launch the browser and waste resources
return;
}

console.log("-- Trying to Launch Puppeteer");
const browserObj = await puppeteer.launch({
headless: true
});

console.log('-- Trying to Open New Page');
const pageObj = await browserObj.newPage();

console.log('-- Change Viewport');
await setChromeViewport(pageObj);

console.log('-- Run thru the url list');
for (const srcUrl of matchedFiles) {
await takeScreenshot(pageObj, srcUrl);
}
})();

关于javascript - 使用 Puppeteer.js 截取 URL 列表的屏幕截图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50177980/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com