Sfoglia il codice sorgente

add --browser-cookies-file option (fix #574)

Gildas 5 anni fa
parent
commit
57d08c6c92
2 ha cambiato i file con 42 aggiunte e 0 eliminazioni
  1. 3 0
      cli/args.js
  2. 39 0
      cli/single-file

+ 3 - 0
cli/args.js

@@ -44,6 +44,7 @@ const args = require("yargs")
 		"browser-args": "",
 		"browser-start-minimized": false,
 		"browser-cookie": [],
+		"browser-cookies-file": "",
 		"compress-CSS": false,
 		"compress-HTML": true,
 		"dump-content": false,
@@ -111,6 +112,8 @@ const args = require("yargs")
 	.boolean("browser-start-minimized")
 	.options("browser-cookie", { description: "Ordered list of cookie parameters separated by a comma: name,value,domain,path,expires,httpOnly,secure,sameSite,url (puppeteer, webdriver-gecko, webdriver-chromium, jsdom)" })
 	.array("browser-cookie")
+	.options("browser-cookies-file", { description: "Path of the cookies file formatted as a JSON file or a Netscape text file (puppeteer, webdriver-gecko, webdriver-chromium, jsdom)" })
+	.string("browser-cookies-file")
 	.options("compress-CSS", { description: "Compress CSS stylesheets" })
 	.boolean("compress-CSS")
 	.options("compress-HTML", { description: "Compress HTML content" })

+ 39 - 0
cli/single-file

@@ -41,7 +41,46 @@ async function run(options) {
 	} else {
 		urls = [options.url];
 	}
+	if (options.browserCookiesFile) {
+		const cookiesContent = fs.readFileSync(options.browserCookiesFile).toString();
+		try {
+			options.browserCookies = JSON.parse(cookiesContent);
+		} catch (error) {
+			options.browserCookies = parseCookies(cookiesContent);
+		}
+	}
 	options.retrieveLinks = true;
 	await singlefile.capture(urls);
 	await singlefile.finish();
+}
+
+function parseCookies(textValue) {
+	return textValue.split(/\r\n|\n/)
+		.filter(line => line.trim() && (!/^#/.test(line) || /^#HttpOnly_/.test(line)))
+		.map(line => {
+			let httpOnly;
+			if (/^#HttpOnly_/.test(line)) {
+				httpOnly = true;
+				line = line.replace(/^#HttpOnly_(.*)/, "$1");
+			} else {
+				httpOnly = false;
+			}
+			const values = line.split(/\t/);
+			if (values.length == 7) {
+				try {
+					return {
+						domain: values[0],
+						path: values[2],
+						secure: values[3] == "TRUE" ? true : false,
+						expires: (values[4] && Number(values[4])) || undefined,
+						name: values[5],
+						value: values[6],
+						httpOnly: httpOnly
+					};
+				} catch (error) {
+					// ignored
+				}
+			}
+		})
+		.filter(cookieData => cookieData);
 }