-
Notifications
You must be signed in to change notification settings - Fork 0
/
htmlParser.js
57 lines (40 loc) · 1.4 KB
/
htmlParser.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
48
49
50
51
//Given an input, creates a text file of the input and link for that file
var textFile = null,
makeTextFile = function (text) {
var data = new Blob([text], {type: 'text/plain'});
// If we are replacing a previously generated file we need to
// manually revoke the object URL to avoid memory leaks.
if (textFile !== null) {
window.URL.revokeObjectURL(textFile);
}
textFile = window.URL.createObjectURL(data);
return textFile;
};
//Get all script tags
var scriptArray = document.getElementsByTagName("script");
var scriptString = "";
//Get all form tags
var formArray = document.getElementsByTagName("form");
var formString = "";
//Iterate through all script tags, parse them, and write their contents to a string
for (var i = 0; i < scriptArray.length; i++) {
if (scriptArray[i].innerHTML != "") {
scriptString += "\n\n<script>\n";
scriptString += scriptArray[i].innerHTML;
scriptString += "\n</script>";
}
}
//Iterate through all form tags, parse them, and write their contents to a string
for (var i = 0; i < formArray.length; i++) {
if (formArray[i].innerHTML != "") {
formString += "\n\n<form>\n";
formString += formArray[i].innerHTML;
formString += "\n</form>";
}
}
var file = makeTextFile(scriptString + formString);
//Redirects to file
if (window.confirm("Click \"OK\" to view to parsed HTML info.")) {
window.location.href = file;
}
console.log(file);