-
Notifications
You must be signed in to change notification settings - Fork 1
/
capture.js
47 lines (42 loc) · 1.53 KB
/
capture.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
"use strict";
var Monitor = require("./monitor");
function getPageContent() {
var docType, docTypeString = "";
docType = document.doctype;
if (docType) {
docTypeString = "<!DOCTYPE " + docType.nodeName;
if (docType.publicId) {
docTypeString += " PUBLIC \"" + docType.publicId + "\"";
if (docType.systemId)
docTypeString += " \"" + docType.systemId + "\"";
}
else if (docType.systemId)
docTypeString += " SYSTEM \"" + docType.systemId + "\"";
if (docType.internalSubset)
docTypeString += " [" + docType.internalSubset + "]";
}
return docTypeString + ">\n" + document.documentElement.outerHTML;
}
function getContentType(headers) {
var contentType = headers.filter(function(header) {
return header.name.toLowerCase() == "content-type"
});
if (contentType)
return contentType[0].value;
}
function capture(url, callback) {
var page = new WebPage(), contentType, status, monitor;
monitor = Monitor.create(function() {
callback(status, contentType || "text/html", page.evaluate(getPageContent));
});
page.open(url);
page.onResourceReceived = function(resource) {
if (resource.url == url) {
status = resource.status;
contentType = getContentType(resource.headers);
page.onResourceRequested = monitor.notifyRequest;
page.onResourceReceived = page.onResourceError = monitor.notifyResponse;
}
};
}
module.exports = capture;