single-file-cli 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.eslintrc.js +43 -0
- package/Dockerfile +12 -0
- package/README.MD +163 -0
- package/args.js +305 -0
- package/back-ends/common/scripts.js +66 -0
- package/back-ends/extensions/bypass-csp/index.js +36 -0
- package/back-ends/extensions/bypass-csp/manifest.json +20 -0
- package/back-ends/extensions/disable-web-security/index.js +54 -0
- package/back-ends/extensions/disable-web-security/manifest.json +20 -0
- package/back-ends/extensions/network-idle/bg.js +61 -0
- package/back-ends/extensions/network-idle/content.js +29 -0
- package/back-ends/extensions/network-idle/manifest.json +31 -0
- package/back-ends/extensions/signed/bypass_csp-0.0.3-an+fx.xpi +0 -0
- package/back-ends/extensions/signed/disable_web_security-0.0.3-an+fx.xpi +0 -0
- package/back-ends/extensions/signed/network_idle-0.0.2-an+fx.xpi +0 -0
- package/back-ends/jsdom.js +175 -0
- package/back-ends/playwright-chromium.js +113 -0
- package/back-ends/playwright-firefox.js +113 -0
- package/back-ends/puppeteer-firefox.js +170 -0
- package/back-ends/puppeteer.js +189 -0
- package/back-ends/webdriver-chromium.js +168 -0
- package/back-ends/webdriver-gecko.js +181 -0
- package/build-lib.sh +3 -0
- package/lib/single-file-bootstrap.js +1 -0
- package/lib/single-file-frames.js +1 -0
- package/lib/single-file-hooks-frames.js +1 -0
- package/lib/single-file-hooks.js +1 -0
- package/lib/single-file-infobar.js +1 -0
- package/lib/single-file.js +1 -0
- package/package.json +37 -0
- package/rollup.config.dev.js +64 -0
- package/rollup.config.js +64 -0
- package/single-file +81 -0
- package/single-file-cli-api.js +324 -0
- package/single-file.bat +2 -0
package/package.json
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "single-file-cli",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "SingleFile CLI",
|
|
5
|
+
"author": "Gildas Lormeau",
|
|
6
|
+
"license": "AGPL-3.0-or-later",
|
|
7
|
+
"main": "single-file-cli-api.js",
|
|
8
|
+
"bin": {
|
|
9
|
+
"single-file": "./single-file"
|
|
10
|
+
},
|
|
11
|
+
"scripts": {
|
|
12
|
+
"build": "./build-lib.sh"
|
|
13
|
+
},
|
|
14
|
+
"repository": {
|
|
15
|
+
"type": "git",
|
|
16
|
+
"url": "git+https://github.com/gildas-lormeau/single-file-cli.git"
|
|
17
|
+
},
|
|
18
|
+
"bugs": {
|
|
19
|
+
"url": "https://github.com/gildas-lormeau/single-file-cli/issues"
|
|
20
|
+
},
|
|
21
|
+
"homepage": "https://github.com/gildas-lormeau/single-file-cli#readme",
|
|
22
|
+
"dependencies": {
|
|
23
|
+
"file-url": "3.0.0",
|
|
24
|
+
"iconv-lite": "0.6.3",
|
|
25
|
+
"jsdom": "19.0.0",
|
|
26
|
+
"puppeteer-core": "13.5.2",
|
|
27
|
+
"selenium-webdriver": "4.1.1",
|
|
28
|
+
"single-file-core": "1.0.6",
|
|
29
|
+
"strong-data-uri": "1.0.6",
|
|
30
|
+
"yargs": "17.4.0"
|
|
31
|
+
},
|
|
32
|
+
"devDependencies": {
|
|
33
|
+
"@rollup/plugin-node-resolve": "13.3.0",
|
|
34
|
+
"rollup": "2.60.0",
|
|
35
|
+
"rollup-plugin-terser": "7.0.2"
|
|
36
|
+
}
|
|
37
|
+
}
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import resolve from "@rollup/plugin-node-resolve";
|
|
2
|
+
import { terser } from "rollup-plugin-terser";
|
|
3
|
+
|
|
4
|
+
const PLUGINS = [resolve({ moduleDirectories: [".."] })];
|
|
5
|
+
const EXTERNAL = ["single-file-core"];
|
|
6
|
+
|
|
7
|
+
export default [{
|
|
8
|
+
input: ["single-file-core/index.js"],
|
|
9
|
+
output: [{
|
|
10
|
+
file: "lib/single-file.js",
|
|
11
|
+
format: "umd",
|
|
12
|
+
name: "singlefile",
|
|
13
|
+
plugins: []
|
|
14
|
+
}],
|
|
15
|
+
plugins: PLUGINS,
|
|
16
|
+
external: EXTERNAL
|
|
17
|
+
}, {
|
|
18
|
+
input: ["single-file-core/processors/frame-tree/content/content-frame-tree.js"],
|
|
19
|
+
output: [{
|
|
20
|
+
file: "lib/single-file-frames.js",
|
|
21
|
+
format: "umd",
|
|
22
|
+
name: "singlefile",
|
|
23
|
+
plugins: []
|
|
24
|
+
}],
|
|
25
|
+
plugins: PLUGINS,
|
|
26
|
+
external: EXTERNAL
|
|
27
|
+
}, {
|
|
28
|
+
input: ["single-file-core/single-file-bootstrap.js"],
|
|
29
|
+
output: [{
|
|
30
|
+
file: "lib/single-file-bootstrap.js",
|
|
31
|
+
format: "umd",
|
|
32
|
+
name: "singlefileBootstrap",
|
|
33
|
+
plugins: []
|
|
34
|
+
}],
|
|
35
|
+
plugins: PLUGINS,
|
|
36
|
+
external: EXTERNAL
|
|
37
|
+
}, {
|
|
38
|
+
input: ["single-file-core/processors/hooks/content/content-hooks-web.js"],
|
|
39
|
+
output: [{
|
|
40
|
+
file: "lib/single-file-hooks.js",
|
|
41
|
+
format: "iife",
|
|
42
|
+
plugins: []
|
|
43
|
+
}],
|
|
44
|
+
plugins: PLUGINS,
|
|
45
|
+
external: EXTERNAL
|
|
46
|
+
}, {
|
|
47
|
+
input: ["single-file-core/processors/hooks/content/content-hooks-frames-web.js"],
|
|
48
|
+
output: [{
|
|
49
|
+
file: "lib/single-file-hooks-frames.js",
|
|
50
|
+
format: "iife",
|
|
51
|
+
plugins: []
|
|
52
|
+
}],
|
|
53
|
+
plugins: PLUGINS,
|
|
54
|
+
external: EXTERNAL
|
|
55
|
+
}, {
|
|
56
|
+
input: ["single-file-core/common/content-infobar-web.js"],
|
|
57
|
+
output: [{
|
|
58
|
+
file: "lib/single-file-infobar.js",
|
|
59
|
+
format: "iife",
|
|
60
|
+
plugins: [terser()]
|
|
61
|
+
}],
|
|
62
|
+
plugins: PLUGINS,
|
|
63
|
+
external: EXTERNAL
|
|
64
|
+
}];
|
package/rollup.config.js
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import { terser } from "rollup-plugin-terser";
|
|
2
|
+
import resolve from "@rollup/plugin-node-resolve";
|
|
3
|
+
|
|
4
|
+
const PLUGINS = [resolve({ moduleDirectories: ["node_modules"] })];
|
|
5
|
+
const EXTERNAL = ["single-file-core"];
|
|
6
|
+
|
|
7
|
+
export default [{
|
|
8
|
+
input: ["single-file-core/index.js"],
|
|
9
|
+
output: [{
|
|
10
|
+
file: "lib/single-file.js",
|
|
11
|
+
format: "umd",
|
|
12
|
+
name: "singlefile",
|
|
13
|
+
plugins: [terser()]
|
|
14
|
+
}],
|
|
15
|
+
plugins: PLUGINS,
|
|
16
|
+
external: EXTERNAL
|
|
17
|
+
}, {
|
|
18
|
+
input: ["single-file-core/processors/frame-tree/content/content-frame-tree.js"],
|
|
19
|
+
output: [{
|
|
20
|
+
file: "lib/single-file-frames.js",
|
|
21
|
+
format: "umd",
|
|
22
|
+
name: "singlefile",
|
|
23
|
+
plugins: [terser()]
|
|
24
|
+
}],
|
|
25
|
+
plugins: PLUGINS,
|
|
26
|
+
external: EXTERNAL
|
|
27
|
+
}, {
|
|
28
|
+
input: ["single-file-core/single-file-bootstrap.js"],
|
|
29
|
+
output: [{
|
|
30
|
+
file: "lib/single-file-bootstrap.js",
|
|
31
|
+
format: "umd",
|
|
32
|
+
name: "singlefileBootstrap",
|
|
33
|
+
plugins: [terser()]
|
|
34
|
+
}],
|
|
35
|
+
plugins: PLUGINS,
|
|
36
|
+
external: EXTERNAL
|
|
37
|
+
}, {
|
|
38
|
+
input: ["single-file-core/processors/hooks/content/content-hooks-web.js"],
|
|
39
|
+
output: [{
|
|
40
|
+
file: "lib/single-file-hooks.js",
|
|
41
|
+
format: "iife",
|
|
42
|
+
plugins: [terser()]
|
|
43
|
+
}],
|
|
44
|
+
plugins: PLUGINS,
|
|
45
|
+
external: EXTERNAL
|
|
46
|
+
}, {
|
|
47
|
+
input: ["single-file-core/processors/hooks/content/content-hooks-frames-web.js"],
|
|
48
|
+
output: [{
|
|
49
|
+
file: "lib/single-file-hooks-frames.js",
|
|
50
|
+
format: "iife",
|
|
51
|
+
plugins: [terser()]
|
|
52
|
+
}],
|
|
53
|
+
plugins: PLUGINS,
|
|
54
|
+
external: EXTERNAL
|
|
55
|
+
}, {
|
|
56
|
+
input: ["single-file-core/common/content-infobar-web.js"],
|
|
57
|
+
output: [{
|
|
58
|
+
file: "lib/single-file-infobar.js",
|
|
59
|
+
format: "iife",
|
|
60
|
+
plugins: [terser()]
|
|
61
|
+
}],
|
|
62
|
+
plugins: PLUGINS,
|
|
63
|
+
external: EXTERNAL
|
|
64
|
+
}];
|
package/single-file
ADDED
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
/*
|
|
4
|
+
* Copyright 2010-2020 Gildas Lormeau
|
|
5
|
+
* contact : gildas.lormeau <at> gmail.com
|
|
6
|
+
*
|
|
7
|
+
* This file is part of SingleFile.
|
|
8
|
+
*
|
|
9
|
+
* The code in this file is free software: you can redistribute it and/or
|
|
10
|
+
* modify it under the terms of the GNU Affero General Public License
|
|
11
|
+
* (GNU AGPL) as published by the Free Software Foundation, either version 3
|
|
12
|
+
* of the License, or (at your option) any later version.
|
|
13
|
+
*
|
|
14
|
+
* The code in this file is distributed in the hope that it will be useful,
|
|
15
|
+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
16
|
+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero
|
|
17
|
+
* General Public License for more details.
|
|
18
|
+
*
|
|
19
|
+
* As additional permission under GNU AGPL version 3 section 7, you may
|
|
20
|
+
* distribute UNMODIFIED VERSIONS OF THIS file without the copy of the GNU
|
|
21
|
+
* AGPL normally required by section 4, provided you include this license
|
|
22
|
+
* notice and a URL through which recipients can access the Corresponding
|
|
23
|
+
* Source.
|
|
24
|
+
*/
|
|
25
|
+
|
|
26
|
+
/* global require */
|
|
27
|
+
|
|
28
|
+
const fileUrl = require("file-url");
|
|
29
|
+
const fs = require("fs");
|
|
30
|
+
const api = require("./single-file-cli-api");
|
|
31
|
+
run(require("./args"))
|
|
32
|
+
.catch(error => console.error(error.message || error)); // eslint-disable-line no-console
|
|
33
|
+
|
|
34
|
+
async function run(options) {
|
|
35
|
+
let urls;
|
|
36
|
+
if (options.url && !api.VALID_URL_TEST.test(options.url)) {
|
|
37
|
+
options.url = fileUrl(options.url);
|
|
38
|
+
}
|
|
39
|
+
if (options.urlsFile) {
|
|
40
|
+
urls = fs.readFileSync(options.urlsFile).toString().split("\n");
|
|
41
|
+
} else {
|
|
42
|
+
urls = [options.url];
|
|
43
|
+
}
|
|
44
|
+
if (options.browserCookiesFile) {
|
|
45
|
+
const cookiesContent = fs.readFileSync(options.browserCookiesFile).toString();
|
|
46
|
+
try {
|
|
47
|
+
options.browserCookies = JSON.parse(cookiesContent);
|
|
48
|
+
} catch (error) {
|
|
49
|
+
options.browserCookies = parseCookies(cookiesContent);
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
options.retrieveLinks = true;
|
|
53
|
+
const singlefile = await api.initialize(options);
|
|
54
|
+
await singlefile.capture(urls);
|
|
55
|
+
await singlefile.finish();
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
function parseCookies(textValue) {
|
|
59
|
+
const httpOnlyRegExp = /^#HttpOnly_(.*)/;
|
|
60
|
+
return textValue.split(/\r\n|\n/)
|
|
61
|
+
.filter(line => line.trim() && (!/^#/.test(line) || httpOnlyRegExp.test(line)))
|
|
62
|
+
.map(line => {
|
|
63
|
+
const httpOnly = httpOnlyRegExp.test(line);
|
|
64
|
+
if (httpOnly) {
|
|
65
|
+
line = line.replace(httpOnlyRegExp, "$1");
|
|
66
|
+
}
|
|
67
|
+
const values = line.split(/\t/);
|
|
68
|
+
if (values.length == 7) {
|
|
69
|
+
return {
|
|
70
|
+
domain: values[0],
|
|
71
|
+
path: values[2],
|
|
72
|
+
secure: values[3] == "TRUE",
|
|
73
|
+
expires: (values[4] && Number(values[4])) || undefined,
|
|
74
|
+
name: values[5],
|
|
75
|
+
value: values[6],
|
|
76
|
+
httpOnly
|
|
77
|
+
};
|
|
78
|
+
}
|
|
79
|
+
})
|
|
80
|
+
.filter(cookieData => cookieData);
|
|
81
|
+
}
|
|
@@ -0,0 +1,324 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright 2010-2020 Gildas Lormeau
|
|
3
|
+
* contact : gildas.lormeau <at> gmail.com
|
|
4
|
+
*
|
|
5
|
+
* This file is part of SingleFile.
|
|
6
|
+
*
|
|
7
|
+
* The code in this file is free software: you can redistribute it and/or
|
|
8
|
+
* modify it under the terms of the GNU Affero General Public License
|
|
9
|
+
* (GNU AGPL) as published by the Free Software Foundation, either version 3
|
|
10
|
+
* of the License, or (at your option) any later version.
|
|
11
|
+
*
|
|
12
|
+
* The code in this file is distributed in the hope that it will be useful,
|
|
13
|
+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
14
|
+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero
|
|
15
|
+
* General Public License for more details.
|
|
16
|
+
*
|
|
17
|
+
* As additional permission under GNU AGPL version 3 section 7, you may
|
|
18
|
+
* distribute UNMODIFIED VERSIONS OF THIS file without the copy of the GNU
|
|
19
|
+
* AGPL normally required by section 4, provided you include this license
|
|
20
|
+
* notice and a URL through which recipients can access the Corresponding
|
|
21
|
+
* Source.
|
|
22
|
+
*/
|
|
23
|
+
|
|
24
|
+
/* global require, exports, URL */
|
|
25
|
+
|
|
26
|
+
const fs = require("fs");
|
|
27
|
+
const path = require("path");
|
|
28
|
+
const scripts = require("./back-ends/common/scripts.js");
|
|
29
|
+
const VALID_URL_TEST = /^(https?|file):\/\//;
|
|
30
|
+
|
|
31
|
+
const DEFAULT_OPTIONS = {
|
|
32
|
+
removeHiddenElements: true,
|
|
33
|
+
removeUnusedStyles: true,
|
|
34
|
+
removeUnusedFonts: true,
|
|
35
|
+
removeFrames: false,
|
|
36
|
+
removeImports: true,
|
|
37
|
+
compressHTML: true,
|
|
38
|
+
compressCSS: false,
|
|
39
|
+
loadDeferredImages: true,
|
|
40
|
+
loadDeferredImagesMaxIdleTime: 1500,
|
|
41
|
+
loadDeferredImagesBlockCookies: false,
|
|
42
|
+
loadDeferredImagesBlockStorage: false,
|
|
43
|
+
loadDeferredImagesKeepZoomLevel: false,
|
|
44
|
+
loadDeferredImagesDispatchScrollEvent: false,
|
|
45
|
+
filenameTemplate: "{page-title} ({date-locale} {time-locale}).html",
|
|
46
|
+
infobarTemplate: "",
|
|
47
|
+
includeInfobar: false,
|
|
48
|
+
filenameMaxLength: 192,
|
|
49
|
+
filenameMaxLengthUnit: "bytes",
|
|
50
|
+
filenameReplacedCharacters: ["~", "+", "\\\\", "?", "%", "*", ":", "|", "\"", "<", ">", "\x00-\x1f", "\x7F"],
|
|
51
|
+
filenameReplacementCharacter: "_",
|
|
52
|
+
maxResourceSizeEnabled: false,
|
|
53
|
+
maxResourceSize: 10,
|
|
54
|
+
backgroundSave: true,
|
|
55
|
+
removeAlternativeFonts: true,
|
|
56
|
+
removeAlternativeMedias: true,
|
|
57
|
+
removeAlternativeImages: true,
|
|
58
|
+
groupDuplicateImages: true,
|
|
59
|
+
saveRawPage: false,
|
|
60
|
+
resolveFragmentIdentifierURLs: false,
|
|
61
|
+
userScriptEnabled: false,
|
|
62
|
+
saveFavicon: true,
|
|
63
|
+
includeBOM: false,
|
|
64
|
+
insertMetaCSP: true,
|
|
65
|
+
insertMetaNoIndex: false,
|
|
66
|
+
insertSingleFileComment: true,
|
|
67
|
+
blockImages: false,
|
|
68
|
+
blockStylesheets: false,
|
|
69
|
+
blockFont: false,
|
|
70
|
+
blockScripts: true,
|
|
71
|
+
blockVideos: true,
|
|
72
|
+
blockAudios: true
|
|
73
|
+
};
|
|
74
|
+
const STATE_PROCESSING = "processing";
|
|
75
|
+
const STATE_PROCESSED = "processed";
|
|
76
|
+
|
|
77
|
+
const backEnds = {
|
|
78
|
+
jsdom: "./back-ends/jsdom.js",
|
|
79
|
+
puppeteer: "./back-ends/puppeteer.js",
|
|
80
|
+
"puppeteer-firefox": "./back-ends/puppeteer-firefox.js",
|
|
81
|
+
"webdriver-chromium": "./back-ends/webdriver-chromium.js",
|
|
82
|
+
"webdriver-gecko": "./back-ends/webdriver-gecko.js",
|
|
83
|
+
"playwright-firefox": "./back-ends/playwright-firefox.js",
|
|
84
|
+
"playwright-chromium": "./back-ends/playwright-chromium.js"
|
|
85
|
+
};
|
|
86
|
+
|
|
87
|
+
let backend, tasks = [], maxParallelWorkers = 8, sessionFilename;
|
|
88
|
+
|
|
89
|
+
exports.getBackEnd = backEndName => require(backEnds[backEndName]);
|
|
90
|
+
exports.DEFAULT_OPTIONS = DEFAULT_OPTIONS;
|
|
91
|
+
exports.VALID_URL_TEST = VALID_URL_TEST;
|
|
92
|
+
exports.initialize = initialize;
|
|
93
|
+
|
|
94
|
+
async function initialize(options) {
|
|
95
|
+
options = Object.assign({}, DEFAULT_OPTIONS, options);
|
|
96
|
+
maxParallelWorkers = options.maxParallelWorkers;
|
|
97
|
+
backend = require(backEnds[options.backEnd]);
|
|
98
|
+
await backend.initialize(options);
|
|
99
|
+
if (options.crawlSyncSession || options.crawlLoadSession) {
|
|
100
|
+
try {
|
|
101
|
+
tasks = JSON.parse(fs.readFileSync(options.crawlSyncSession || options.crawlLoadSession).toString());
|
|
102
|
+
} catch (error) {
|
|
103
|
+
if (options.crawlLoadSession) {
|
|
104
|
+
throw error;
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
if (options.crawlSyncSession || options.crawlSaveSession) {
|
|
109
|
+
sessionFilename = options.crawlSyncSession || options.crawlSaveSession;
|
|
110
|
+
}
|
|
111
|
+
return {
|
|
112
|
+
capture: urls => capture(urls, options),
|
|
113
|
+
finish: () => finish(options),
|
|
114
|
+
};
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
async function capture(urls, options) {
|
|
118
|
+
let newTasks;
|
|
119
|
+
const taskUrls = tasks.map(task => task.url);
|
|
120
|
+
newTasks = urls.map(url => createTask(url, options));
|
|
121
|
+
newTasks = newTasks.filter(task => task && !taskUrls.includes(task.url));
|
|
122
|
+
if (newTasks.length) {
|
|
123
|
+
tasks = tasks.concat(newTasks);
|
|
124
|
+
saveTasks();
|
|
125
|
+
}
|
|
126
|
+
await runTasks();
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
async function finish(options) {
|
|
130
|
+
const promiseTasks = tasks.map(task => task.promise);
|
|
131
|
+
await Promise.all(promiseTasks);
|
|
132
|
+
if (options.crawlReplaceURLs) {
|
|
133
|
+
tasks.forEach(task => {
|
|
134
|
+
try {
|
|
135
|
+
let pageContent = fs.readFileSync(task.filename).toString();
|
|
136
|
+
tasks.forEach(otherTask => {
|
|
137
|
+
if (otherTask.filename) {
|
|
138
|
+
pageContent = pageContent.replace(new RegExp(escapeRegExp("\"" + otherTask.originalUrl + "\""), "gi"), "\"" + otherTask.filename + "\"");
|
|
139
|
+
pageContent = pageContent.replace(new RegExp(escapeRegExp("'" + otherTask.originalUrl + "'"), "gi"), "'" + otherTask.filename + "'");
|
|
140
|
+
const filename = otherTask.filename.replace(/ /g, "%20");
|
|
141
|
+
pageContent = pageContent.replace(new RegExp(escapeRegExp("=" + otherTask.originalUrl + " "), "gi"), "=" + filename + " ");
|
|
142
|
+
pageContent = pageContent.replace(new RegExp(escapeRegExp("=" + otherTask.originalUrl + ">"), "gi"), "=" + filename + ">");
|
|
143
|
+
}
|
|
144
|
+
});
|
|
145
|
+
fs.writeFileSync(task.filename, pageContent);
|
|
146
|
+
} catch (error) {
|
|
147
|
+
// ignored
|
|
148
|
+
}
|
|
149
|
+
});
|
|
150
|
+
}
|
|
151
|
+
if (!options.browserDebug) {
|
|
152
|
+
return backend.closeBrowser();
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
async function runTasks() {
|
|
157
|
+
const availableTasks = tasks.filter(task => !task.status).length;
|
|
158
|
+
const processingTasks = tasks.filter(task => task.status == STATE_PROCESSING).length;
|
|
159
|
+
const promisesTasks = [];
|
|
160
|
+
for (let workerIndex = 0; workerIndex < Math.min(availableTasks, maxParallelWorkers - processingTasks); workerIndex++) {
|
|
161
|
+
promisesTasks.push(runNextTask());
|
|
162
|
+
}
|
|
163
|
+
return Promise.all(promisesTasks);
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
async function runNextTask() {
|
|
167
|
+
const task = tasks.find(task => !task.status);
|
|
168
|
+
if (task) {
|
|
169
|
+
const options = task.options;
|
|
170
|
+
let taskOptions = JSON.parse(JSON.stringify(options));
|
|
171
|
+
taskOptions.url = task.url;
|
|
172
|
+
task.status = STATE_PROCESSING;
|
|
173
|
+
saveTasks();
|
|
174
|
+
task.promise = capturePage(taskOptions);
|
|
175
|
+
const pageData = await task.promise;
|
|
176
|
+
task.status = STATE_PROCESSED;
|
|
177
|
+
if (pageData) {
|
|
178
|
+
task.filename = pageData.filename;
|
|
179
|
+
if (options.crawlLinks && testMaxDepth(task)) {
|
|
180
|
+
let newTasks = pageData.links
|
|
181
|
+
.map(urlLink => createTask(urlLink, options, task, tasks[0]))
|
|
182
|
+
.filter(task => task &&
|
|
183
|
+
testMaxDepth(task) &&
|
|
184
|
+
!tasks.find(otherTask => otherTask.url == task.url) &&
|
|
185
|
+
(!options.crawlInnerLinksOnly || task.isInnerLink) &&
|
|
186
|
+
(!options.crawlNoParent || (task.isChild || !task.isInnerLink)));
|
|
187
|
+
tasks.splice(tasks.length, 0, ...newTasks);
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
saveTasks();
|
|
191
|
+
await runTasks();
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
function testMaxDepth(task) {
|
|
196
|
+
const options = task.options;
|
|
197
|
+
return (options.crawlMaxDepth == 0 || task.depth <= options.crawlMaxDepth) &&
|
|
198
|
+
(options.crawlExternalLinksMaxDepth == 0 || task.externalLinkDepth < options.crawlExternalLinksMaxDepth);
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
function createTask(url, options, parentTask, rootTask) {
|
|
202
|
+
url = parentTask ? rewriteURL(url, options.crawlRemoveURLFragment, options.crawlRewriteRules) : url;
|
|
203
|
+
if (VALID_URL_TEST.test(url)) {
|
|
204
|
+
const isInnerLink = rootTask && url.startsWith(getHostURL(rootTask.url));
|
|
205
|
+
const rootBaseURIMatch = rootTask && rootTask.url.match(/(.*?)[^/]*$/);
|
|
206
|
+
const isChild = isInnerLink && rootBaseURIMatch && rootBaseURIMatch[1] && url.startsWith(rootBaseURIMatch[1]);
|
|
207
|
+
return {
|
|
208
|
+
url,
|
|
209
|
+
isInnerLink,
|
|
210
|
+
isChild,
|
|
211
|
+
originalUrl: url,
|
|
212
|
+
rootBaseURI: rootBaseURIMatch && rootBaseURIMatch[1],
|
|
213
|
+
depth: parentTask ? parentTask.depth + 1 : 0,
|
|
214
|
+
externalLinkDepth: isInnerLink ? -1 : parentTask ? parentTask.externalLinkDepth + 1 : -1,
|
|
215
|
+
options
|
|
216
|
+
};
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
function saveTasks() {
|
|
221
|
+
if (sessionFilename) {
|
|
222
|
+
fs.writeFileSync(sessionFilename, JSON.stringify(
|
|
223
|
+
tasks.map(task => Object.assign({}, task, {
|
|
224
|
+
status: task.status == STATE_PROCESSING ? undefined : task.status,
|
|
225
|
+
promise: undefined,
|
|
226
|
+
options: task.status && task.status == STATE_PROCESSED ? undefined : task.options
|
|
227
|
+
}))
|
|
228
|
+
));
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
function rewriteURL(url, crawlRemoveURLFragment, crawlRewriteRules) {
|
|
233
|
+
url = url.trim();
|
|
234
|
+
if (crawlRemoveURLFragment) {
|
|
235
|
+
url = url.replace(/^(.*?)#.*$/, "$1");
|
|
236
|
+
}
|
|
237
|
+
crawlRewriteRules.forEach(rewriteRule => {
|
|
238
|
+
const parts = rewriteRule.trim().split(/ +/);
|
|
239
|
+
if (parts.length) {
|
|
240
|
+
url = url.replace(new RegExp(parts[0]), parts[1] || "").trim();
|
|
241
|
+
}
|
|
242
|
+
});
|
|
243
|
+
return url;
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
function getHostURL(url) {
|
|
247
|
+
url = new URL(url);
|
|
248
|
+
return url.protocol + "//" + (url.username ? url.username + (url.password || "") + "@" : "") + url.hostname;
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
async function capturePage(options) {
|
|
252
|
+
try {
|
|
253
|
+
let filename;
|
|
254
|
+
const pageData = await backend.getPageData(options);
|
|
255
|
+
if (options.includeInfobar) {
|
|
256
|
+
await includeInfobarScript(pageData);
|
|
257
|
+
}
|
|
258
|
+
if (options.output) {
|
|
259
|
+
filename = getFilename(options.output, options);
|
|
260
|
+
} else if (options.dumpContent) {
|
|
261
|
+
console.log(pageData.content); // eslint-disable-line no-console
|
|
262
|
+
} else {
|
|
263
|
+
filename = getFilename(pageData.filename, options);
|
|
264
|
+
}
|
|
265
|
+
if (filename) {
|
|
266
|
+
const dirname = path.dirname(filename);
|
|
267
|
+
if (dirname) {
|
|
268
|
+
fs.mkdirSync(dirname, { recursive: true });
|
|
269
|
+
}
|
|
270
|
+
fs.writeFileSync(filename, pageData.content);
|
|
271
|
+
}
|
|
272
|
+
return pageData;
|
|
273
|
+
} catch (error) {
|
|
274
|
+
const message = "URL: " + options.url + "\nStack: " + error.stack + "\n";
|
|
275
|
+
if (options.errorFile) {
|
|
276
|
+
fs.writeFileSync(options.errorFile, message, { flag: "a" });
|
|
277
|
+
} else {
|
|
278
|
+
console.error(error.message || error, message); // eslint-disable-line no-console
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
function getFilename(filename, options, index = 1) {
|
|
284
|
+
if (Array.isArray(options.outputDirectory)) {
|
|
285
|
+
const outputDirectory = options.outputDirectory.pop();
|
|
286
|
+
if (outputDirectory.startsWith("/")) {
|
|
287
|
+
options.outputDirectory = outputDirectory;
|
|
288
|
+
} else {
|
|
289
|
+
options.outputDirectory = options.outputDirectory[0] + outputDirectory;
|
|
290
|
+
}
|
|
291
|
+
}
|
|
292
|
+
let outputDirectory = options.outputDirectory || "";
|
|
293
|
+
if (outputDirectory && !outputDirectory.endsWith("/")) {
|
|
294
|
+
outputDirectory += "/";
|
|
295
|
+
}
|
|
296
|
+
let newFilename = outputDirectory + filename;
|
|
297
|
+
if (options.filenameConflictAction == "overwrite") {
|
|
298
|
+
return filename;
|
|
299
|
+
} else if (options.filenameConflictAction == "uniquify" && index > 1) {
|
|
300
|
+
const regExpMatchExtension = /(\.[^.]+)$/;
|
|
301
|
+
const matchExtension = newFilename.match(regExpMatchExtension);
|
|
302
|
+
if (matchExtension && matchExtension[1]) {
|
|
303
|
+
newFilename = newFilename.replace(regExpMatchExtension, " (" + index + ")" + matchExtension[1]);
|
|
304
|
+
} else {
|
|
305
|
+
newFilename += " (" + index + ")";
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
if (fs.existsSync(newFilename)) {
|
|
309
|
+
if (options.filenameConflictAction != "skip") {
|
|
310
|
+
return getFilename(filename, options, index + 1);
|
|
311
|
+
}
|
|
312
|
+
} else {
|
|
313
|
+
return newFilename;
|
|
314
|
+
}
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
function escapeRegExp(string) {
|
|
318
|
+
return string.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
async function includeInfobarScript(pageData) {
|
|
322
|
+
const infobarContent = await scripts.getInfobarScript();
|
|
323
|
+
pageData.content += "<script>document.currentScript.remove();" + infobarContent + "</script>";
|
|
324
|
+
}
|
package/single-file.bat
ADDED