playwright-single-file
Version:
SingleFile for Playwright
46 lines (45 loc) • 2.1 kB
JavaScript
import { getPageData } from './lib/cdp-client.js';
import { removeEmptyLines } from './lib/remover.js';
// Implementation of the SingleFile plugin
class SingleFile {
async run(page, options = {}) {
const cdpOptions = createCdpOptions(options);
cdpOptions.displayStats = true;
const pageData = await getPageData(page, cdpOptions);
let content = pageData.content;
if (cdpOptions.removeEmptyLines) {
content = removeEmptyLines(content);
}
return { ...pageData, content };
}
}
export async function pageToSingleFile(page, options = {}) {
const singleFile = new SingleFile();
return singleFile.run(page, options);
}
function createCdpOptions(options) {
return {
browserLoadMaxTime: options.browserLoadMaxTime || 60000,
browserWaitUntil: options.browserWaitUntil || 'networkIdle',
browserWaitDelay: options.browserWaitDelay || 0,
removeFrames: options.removeFrames ?? true,
removeScripts: options.removeScripts ?? true,
removeHidden: options.removeHidden ?? true,
removeUnusedStyles: options.removeUnusedStyles ?? true,
removeUnusedFonts: options.removeUnusedFonts ?? true,
removeEmptyLines: options.removeEmptyLines ?? false,
compressHTML: options.compressHTML ?? false,
browserByPassCSP: options.browserByPassCSP ?? true,
includeBOM: options.includeBOM ?? false,
removeAlternativeFonts: options.removeAlternativeFonts ?? true,
removeAlternativeMedias: options.removeAlternativeMedias ?? false,
removeAlternativeImages: options.removeAlternativeImages ?? true,
groupDuplicateImages: options.groupDuplicateImages ?? true,
loadDeferredImages: options.loadDeferredImages ?? true,
loadDeferredImagesMaxIdleTime: options.loadDeferredImagesMaxIdleTime ?? 1500,
maxResourceSize: options.maxResourceSize ?? 10,
blockScripts: options.blockScripts ?? true,
blockVideos: options.blockVideos ?? true,
blockAudios: options.blockAudios ?? true,
};
}