-
Notifications
You must be signed in to change notification settings - Fork 2
/
Download.js
108 lines (88 loc) · 3.28 KB
/
Download.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
/* using window.print */
storyDiv = document.createElement('div');
storyDiv.setAttribute('id', 'FullStoryDiv');
StartRetrieving();
function fetchPage(callback, url, chapter) {
//Update display percentage
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function(data) {
if (xhr.readyState == 4) {
if (xhr.status == 200) {
var data = xhr.responseText;
callback(data, chapter);
} else {
callback(null, chapter);
}
}
}
// Note that any URL fetched here must be matched by a permission in
// the manifest.json file!
xhr.open('GET', url, true);
xhr.send();
};
function StartRetrieving() {
var chaptersDropdown = document.getElementsByName("chapter");
var chapters = 1;
if (chaptersDropdown.length != 0) {
chapters = chaptersDropdown[0].options.length;
}
var tmpLocation = self.location.href.substring(self.location.href.indexOf("fanfiction.net/s/") + 17);
tmpLocation = tmpLocation.substring(0, tmpLocation.indexOf("/"));
targetLocation = "https://www.fanfiction.net/s/" + tmpLocation + "/t('.'t)";
storyChapters = new Array(chapters);
completedChapters = 0;
fetchPage(ParseStoryContent, targetLocation.replace("t('.'t)", 1), 0);
}
function FinishStory() {
var title = document.title;
if (title.indexOf("Chapter") != -1) {
title = title.substring(0, title.indexOf("Chapter"));
}
/* Dear Cas - 59 chapters */
/* var story = "<h1>" + title + " - " + storyChapters.length + " chapters</h1><br />"; */
var story = "<br /><br /><br /><h1>" + title + "</h1>";
story += "<br /><br />";
/* story += "<h2>" + storyChapters.length + " chapters</h2><br />"; */
/* Chapter 1
blah blah blah ...
*/
for (var i = 0; i < storyChapters.length; i++) {
story += "<br /><br /><h3>Chapter " + (i + 1) + " </h3><br /><br />";
story += storyChapters[i];
story += "<br /><hr>";
}
storyDiv.innerHTML += story;
storyDiv.innerHTML += "<br /><br /><br /><br /><br /><br /><br /><center>Thank you for using FanfictionUltimate! <br /> \
Leave a review on Chrome Web Store if you liked it.</center>";
DownloadPDF(title);
}
function ParseStoryContent(story, chapter) {
completedChapters += 1;
if (!story) {
storyChapters[chapter] = "";
fetchPage(ParseStoryContent, targetLocation.replace("t('.'t)", completedChapters + 1), completedChapters);
}
else {
var root = document.implementation.createHTMLDocument();
root.body.innerHTML = story;
storyChapters[chapter] = root.getElementById("storytextp")
.innerHTML;
root = null;
if (completedChapters >= storyChapters.length) {
storyDiv.innerHTML = "";
FinishStory();
}
else {
fetchPage(ParseStoryContent, targetLocation.replace("t('.'t)", completedChapters + 1), completedChapters);
}
}
}
function DownloadPDF(title) {
var mywindow = window.open('', 'PRINT', 'height= 800, width=800');
mywindow.document.write(storyDiv.innerHTML);
mywindow.document.title = title;
mywindow.focus();
mywindow.print();
mywindow.close();
return true;
}