html-snapshots 5.11.0 → 6.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/.nvmrc +1 -0
- package/HISTORY.md +6 -0
- package/README.md +66 -106
- package/eslint.config.js +9 -8
- package/lib/async/exists.js +1 -1
- package/lib/async/index.js +4 -3
- package/lib/common/sitemap/sitemap-collection.js +2 -2
- package/lib/common/sitemap/sitemap.js +5 -5
- package/lib/html-snapshots.js +41 -78
- package/lib/input-generators/_base.js +10 -9
- package/lib/input-generators/array.js +1 -1
- package/lib/input-generators/index.js +2 -2
- package/lib/input-generators/robots.js +1 -1
- package/lib/input-generators/textfile.js +2 -2
- package/lib/playwright/index.js +125 -0
- package/lib/playwright/removeScripts.js +11 -0
- package/lib/puppeteer/index.js +7 -7
- package/lib/puppeteer/removeScripts.js +1 -1
- package/package.json +23 -31
- package/lib/phantom/customFilter.js +0 -24
- package/lib/phantom/default.js +0 -21
- package/lib/phantom/modules/cli.js +0 -100
- package/lib/phantom/modules/detectors.js +0 -48
- package/lib/phantom/modules/globals.js +0 -43
- package/lib/phantom/modules/selectorDetect.js +0 -108
- package/lib/phantom/modules/verbose.js +0 -71
- package/lib/phantom/removeScripts.js +0 -30
|
@@ -1,108 +0,0 @@
|
|
|
1
|
-
/*
|
|
2
|
-
* selectorDetect.js
|
|
3
|
-
*
|
|
4
|
-
* Produce a single snapshot for a web page when a given selector is detected.
|
|
5
|
-
*
|
|
6
|
-
* This is a module for a phantomJS script that runs in phantomjs.
|
|
7
|
-
*
|
|
8
|
-
* Copyright (c) 2013 - 2025 Alex Grant, LocalNerve, contributors
|
|
9
|
-
* Licensed under the MIT license.
|
|
10
|
-
*/
|
|
11
|
-
|
|
12
|
-
// Get the start time immediately since we're already in a PhantomJS process instance
|
|
13
|
-
// The spawner is already counting down (with allowances)...
|
|
14
|
-
var start = new Date().getTime();
|
|
15
|
-
|
|
16
|
-
/**
|
|
17
|
-
* Dependencies, phantomjs
|
|
18
|
-
*/
|
|
19
|
-
var page = require("webpage").create();
|
|
20
|
-
var fs = require("fs");
|
|
21
|
-
var globals = require("./globals");
|
|
22
|
-
var verbose = require("./verbose");
|
|
23
|
-
|
|
24
|
-
/**
|
|
25
|
-
* waitFor
|
|
26
|
-
*
|
|
27
|
-
* Heavily borrowed from phantomJS 'waitFor' example
|
|
28
|
-
*/
|
|
29
|
-
function waitFor (testFx, onReady, onTimeout, timeout, checkInterval) {
|
|
30
|
-
var condition = false,
|
|
31
|
-
interval = setInterval(function () {
|
|
32
|
-
if ( (new Date().getTime() - start < timeout) && !condition ) {
|
|
33
|
-
// If not timeout yet and condition not yet fulfilled
|
|
34
|
-
condition = testFx();
|
|
35
|
-
} else {
|
|
36
|
-
clearInterval(interval); // Stop this interval
|
|
37
|
-
if ( !condition ) {
|
|
38
|
-
// If condition still not fulfilled (timeout but condition is 'false')
|
|
39
|
-
onTimeout();
|
|
40
|
-
} else {
|
|
41
|
-
// Condition fulfilled (timeout and/or condition is 'true')
|
|
42
|
-
onReady((new Date().getTime() - start));
|
|
43
|
-
}
|
|
44
|
-
}
|
|
45
|
-
}, checkInterval);
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
/**
|
|
49
|
-
* snapshot
|
|
50
|
-
*
|
|
51
|
-
* Opens the page and waits for the selector to become visible. Then takes the html snapshot.
|
|
52
|
-
* Applies an optional output filter to the html content.
|
|
53
|
-
*
|
|
54
|
-
*/
|
|
55
|
-
function snapshot (options, detector, filter) {
|
|
56
|
-
filter = filter || function(content) { return content; };
|
|
57
|
-
|
|
58
|
-
console.log("Creating snapshot for "+options.url+"...");
|
|
59
|
-
|
|
60
|
-
// https://github.com/ariya/phantomjs/issues/10930
|
|
61
|
-
page.customHeaders = {
|
|
62
|
-
"Accept-Encoding": "identity"
|
|
63
|
-
};
|
|
64
|
-
|
|
65
|
-
// add optional verbose output
|
|
66
|
-
if (options.verbose) {
|
|
67
|
-
verbose(page);
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
// create the snapshot
|
|
71
|
-
page.open(options.url, {
|
|
72
|
-
resourceTimeout: options.timeout - 300 // a little space to report problems.
|
|
73
|
-
}, function (status) {
|
|
74
|
-
if (status !== "success") {
|
|
75
|
-
// if phantomJS could not load the page, so end right now
|
|
76
|
-
globals.exit(2, "Unable to load page " + options.url);
|
|
77
|
-
} else {
|
|
78
|
-
// phantomJS loaded the page, so wait for it to be ready
|
|
79
|
-
waitFor(
|
|
80
|
-
|
|
81
|
-
// The test to determine readiness
|
|
82
|
-
function () {
|
|
83
|
-
return page.evaluate(detector, {
|
|
84
|
-
selector: options.selector,
|
|
85
|
-
url: options.url
|
|
86
|
-
});
|
|
87
|
-
},
|
|
88
|
-
|
|
89
|
-
// The onReady callback
|
|
90
|
-
function (time) {
|
|
91
|
-
fs.write(options.outputFile, filter(page.content), "w");
|
|
92
|
-
globals.exit(0, "snapshot for "+options.url+" finished in "+time+" ms\n written to "+options.outputFile);
|
|
93
|
-
},
|
|
94
|
-
|
|
95
|
-
// The onTimeout callback
|
|
96
|
-
function () {
|
|
97
|
-
globals.exit(1, "timed out waiting for "+options.selector+" to become visible for "+options.url);
|
|
98
|
-
},
|
|
99
|
-
|
|
100
|
-
options.timeout,
|
|
101
|
-
options.checkInterval
|
|
102
|
-
|
|
103
|
-
);
|
|
104
|
-
}
|
|
105
|
-
});
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
module.exports = snapshot;
|
|
@@ -1,71 +0,0 @@
|
|
|
1
|
-
/*
|
|
2
|
-
* verbose.js
|
|
3
|
-
*
|
|
4
|
-
* Adds handlers to WebPage module to produce additional output for debugging.
|
|
5
|
-
*
|
|
6
|
-
* This is a module for a phantomJS script that runs in phantomjs.
|
|
7
|
-
*
|
|
8
|
-
* Copyright (c) 2013 - 2025 Alex Grant, LocalNerve, contributors
|
|
9
|
-
* Licensed under the MIT license.
|
|
10
|
-
*/
|
|
11
|
-
/* global window */
|
|
12
|
-
var system = require("system");
|
|
13
|
-
|
|
14
|
-
/**
|
|
15
|
-
* Decorate the given WebPage with handlers to produce verbose output.
|
|
16
|
-
* Source:
|
|
17
|
-
* https://newspaint.wordpress.com/2013/04/25/getting-to-the-bottom-of-why-a-phantomjs-page-load-fails/
|
|
18
|
-
*/
|
|
19
|
-
function verbose (page) {
|
|
20
|
-
|
|
21
|
-
page.onResourceError = function(resourceError) {
|
|
22
|
-
system.stderr.writeLine('= onResourceError()');
|
|
23
|
-
system.stderr.writeLine(' - unable to load url: "' + resourceError.url + '"');
|
|
24
|
-
system.stderr.writeLine(' - error code: ' + resourceError.errorCode + ', description: ' + resourceError.errorString );
|
|
25
|
-
};
|
|
26
|
-
|
|
27
|
-
page.onError = function(msg, trace) {
|
|
28
|
-
system.stderr.writeLine('= onError()');
|
|
29
|
-
var msgStack = [' ERROR: ' + msg];
|
|
30
|
-
if (trace) {
|
|
31
|
-
msgStack.push(' TRACE:');
|
|
32
|
-
trace.forEach(function(t) {
|
|
33
|
-
msgStack.push(' -> ' + t.file + ': ' + t.line + (t.function ? ' (in function "' + t.function + '")' : ''));
|
|
34
|
-
});
|
|
35
|
-
}
|
|
36
|
-
system.stderr.writeLine(msgStack.join('\n'));
|
|
37
|
-
};
|
|
38
|
-
|
|
39
|
-
page.onResourceRequested = function (request) {
|
|
40
|
-
system.stderr.writeLine('= onResourceRequested()');
|
|
41
|
-
system.stderr.writeLine(' request: ' + JSON.stringify(request, undefined, 4));
|
|
42
|
-
};
|
|
43
|
-
|
|
44
|
-
page.onResourceReceived = function(response) {
|
|
45
|
-
system.stderr.writeLine('= onResourceReceived()' );
|
|
46
|
-
system.stderr.writeLine(' id: ' + response.id + ', stage: "' + response.stage + '", response: ' + JSON.stringify(response));
|
|
47
|
-
};
|
|
48
|
-
|
|
49
|
-
page.onLoadStarted = function() {
|
|
50
|
-
system.stderr.writeLine('= onLoadStarted()');
|
|
51
|
-
var currentUrl = page.evaluate(function() {
|
|
52
|
-
return window.location.href;
|
|
53
|
-
});
|
|
54
|
-
system.stderr.writeLine(' leaving url: ' + currentUrl);
|
|
55
|
-
};
|
|
56
|
-
|
|
57
|
-
page.onNavigationRequested = function(url, type, willNavigate, main) {
|
|
58
|
-
system.stderr.writeLine('= onNavigationRequested');
|
|
59
|
-
system.stderr.writeLine(' destination_url: ' + url);
|
|
60
|
-
system.stderr.writeLine(' type (cause): ' + type);
|
|
61
|
-
system.stderr.writeLine(' will navigate: ' + willNavigate);
|
|
62
|
-
system.stderr.writeLine(' from page\'s main frame: ' + main);
|
|
63
|
-
};
|
|
64
|
-
|
|
65
|
-
page.onLoadFinished = function(status) {
|
|
66
|
-
system.stderr.writeLine('= onLoadFinished()');
|
|
67
|
-
system.stderr.writeLine(' status: ' + status);
|
|
68
|
-
};
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
module.exports = verbose;
|
|
@@ -1,30 +0,0 @@
|
|
|
1
|
-
/*
|
|
2
|
-
* removeScripts.js
|
|
3
|
-
*
|
|
4
|
-
* Produce a single snapshot for a web page.
|
|
5
|
-
* Snapshot taken when selector is(:visible).
|
|
6
|
-
* Applies a single filter to remove any script tags.
|
|
7
|
-
*
|
|
8
|
-
* This is a phantomJS script that runs in phantomjs.
|
|
9
|
-
*
|
|
10
|
-
* Copyright (c) 2013 - 2025 Alex Grant, LocalNerve, contributors
|
|
11
|
-
* Licensed under the MIT license.
|
|
12
|
-
*/
|
|
13
|
-
var selectorDetect = require("./modules/selectorDetect");
|
|
14
|
-
var cli = require("./modules/cli");
|
|
15
|
-
var detectors = require("./modules/detectors");
|
|
16
|
-
|
|
17
|
-
/**
|
|
18
|
-
* Remove all script tags from content
|
|
19
|
-
*/
|
|
20
|
-
function removeScriptTags(content) {
|
|
21
|
-
return content.replace(/<script\b[^<]*(?:(?!<\/script>)<[^<]*)*<\/script>/gi, "");
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
var options = cli.selector();
|
|
25
|
-
|
|
26
|
-
selectorDetect(
|
|
27
|
-
options,
|
|
28
|
-
options.useJQuery ? detectors.jquerySelector : detectors.querySelector,
|
|
29
|
-
removeScriptTags
|
|
30
|
-
);
|