Bläddra i källkod

added --http-header option (fix #501)

Gildas 5 år sedan
förälder
incheckning
e1b953b8a1

+ 12 - 0
cli/args.js

@@ -49,6 +49,7 @@ const args = require("yargs")
 		"filename-template": "{page-title} ({date-iso} {time-locale}).html",
 		"filename-replacement-character": "_",
 		"group-duplicate-images": true,
+		"http-header": [],
 		"include-infobar": false,
 		"load-deferred-images": true,
 		"load-deferred-images-max-idle-time": 1500,
@@ -140,6 +141,8 @@ const args = require("yargs")
 	.string("filename-replacement-character")
 	.options("group-duplicate-images", { description: "Group duplicate images into CSS custom properties" })
 	.boolean("group-duplicate-images")
+	.options("http-header", { description: "Extra HTTP header (puppeteer)" })
+	.array("http-header")
 	.options("include-BOM", { description: "Include the UTF-8 BOM into the HTML page" })
 	.boolean("include-BOM")
 	.options("include-infobar", { description: "Include the infobar" })
@@ -197,6 +200,15 @@ args.compressHTML = args.compressHtml;
 args.includeBOM = args.includeBom;
 args.crawlReplaceURLs = args.crawlReplaceUrls;
 args.crawlRemoveURLFragment = args.crawlRemoveUrlFragment;
+const headers = args.httpHeader;
+delete args.httpHeader;
+args.httpHeaders = {};
+headers.forEach(header => {
+	const matchedHeader = header.match(/^(.*?):(.*)$/);
+	if (matchedHeader.length == 3) {
+		args.httpHeaders[matchedHeader[1].trim()] = matchedHeader[2].trimLeft();
+	}
+});
 Object.keys(args).filter(optionName => optionName.includes("-"))
 	.forEach(optionName => delete args[optionName]);
 delete args["$0"];

+ 3 - 0
cli/back-ends/playwright-chromium.js

@@ -73,6 +73,9 @@ async function setPageOptions(page, options) {
 			width: options.browserWidth,
 			height: options.browserHeight
 		});
+	}	
+	if (options.httpHeaders && options.httpHeaders.length) {
+		page.setExtraHTTPHeaders(options.httpHeaders);
 	}
 }
 

+ 3 - 0
cli/back-ends/playwright-firefox.js

@@ -74,6 +74,9 @@ async function setPageOptions(page, options) {
 			height: options.browserHeight
 		});
 	}
+	if (options.httpHeaders && options.httpHeaders.length) {
+		page.setExtraHTTPHeaders(options.httpHeaders);
+	}
 }
 
 async function getPageData(page, options) {

+ 7 - 0
cli/back-ends/puppeteer-firefox.js

@@ -81,6 +81,13 @@ async function setPageOptions(page, options) {
 			// ignored
 		}
 	}
+	if (options.httpHeaders && options.httpHeaders.length) {
+		try {
+			await page.setExtraHTTPHeaders(options.httpHeaders);
+		} catch (error) {
+			// ignored
+		}
+	}
 }
 
 async function getPageData(browser, page, options) {

+ 3 - 0
cli/back-ends/puppeteer.js

@@ -88,6 +88,9 @@ async function setPageOptions(page, options) {
 	if (options.browserBypassCSP === undefined || options.browserBypassCSP) {
 		await page.setBypassCSP(true);
 	}
+	if (options.httpHeaders && options.httpHeaders.length) {
+		page.setExtraHTTPHeaders(options.httpHeaders);
+	}
 	if (options.browserStartMinimized) {
 		const session = await page.target().createCDPSession();
 		const { windowId } = await session.send("Browser.getWindowForTarget");