web-crawling-utils
Version:
Common useful utils for web crawling and automation scripts
39 lines (38 loc) • 2.23 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.captureScreenshot = void 0;
const path_1 = __importDefault(require("path"));
/**
* Captures a full-page screenshot of the given Puppeteer page and saves it with a timestamped filename.
*
* @async
* @function captureScreenshot
* @param {Page} page - The Puppeteer page instance to capture.
* @param {string} [name='screenshot'] - The base name for the screenshot file. Default is 'screenshot'.
* @returns {Promise<void>} - A Promise that resolves when the screenshot is saved successfully.
*
* @description This function captures a full-page screenshot of the provided Puppeteer page. It saves the image
* as a PNG file in the `/tmp` directory, with a unique timestamp in the filename to avoid overwrites.
* Useful for logging, debugging, or recording automation sequences in web scraping or testing environments.
*
* @example
* await captureScreenshot(page, 'homepage'); // Saves screenshot as `/tmp/homepage-<timestamp>.png`
*/
const captureScreenshot = (page_1, ...args_1) => __awaiter(void 0, [page_1, ...args_1], void 0, function* (page, name = 'screenshot') {
const filePath = path_1.default.join('/tmp', `${name}-${Date.now()}.png`);
yield page.screenshot({ path: filePath, fullPage: true });
console.log(`Screenshot saved at ${filePath}`);
});
exports.captureScreenshot = captureScreenshot;