-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathgitbook_download.js
235 lines (191 loc) · 6.84 KB
/
gitbook_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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
/************************************************
gitbook 下载
使用方式:
1. 使用Chrome登陆gitbook
2. 进入你购买的专栏详情页面,点击阅读,进入阅读页面。
3. 在此页面打开控制台,贴入脚本回车运行
4. 可能会提示是否下载多个文件,选择是
*************************************************/
(function main() {
var $ = function (selector) {
return document.querySelector(selector)
};
var $$ = function (selector) {
return document.querySelectorAll(selector)
};
/**
* 格式化数字
* @param digitCount 位数
*/
function formatNumber(number, digitCount) {
var str = number + ''
for (var i = 0; i < digitCount - str.length; i++) {
str = "0" + str
}
return str
}
function postData(url, data) {
// Default options are marked with *
return fetch(url, {
body: JSON.stringify(data), // must match 'Content-Type' header
cache: 'no-cache', // *default, no-cache, reload, force-cache, only-if-cached
credentials: 'same-origin', // include, same-origin, *omit
headers: {
'user-agent': 'Mozilla/4.0 MDN Example',
'content-type': 'application/json'
},
method: 'POST', // *GET, POST, PUT, DELETE, etc.
mode: 'cors', // no-cors, cors, *same-origin
redirect: 'follow', // manual, *follow, error
referrer: 'no-referrer', // *client, no-referrer
})
.then(response => response.json()) // parses response to JSON
}
function waitFor(judge, onfinish, onerror, maxRetry) {
if (judge()) {
onfinish()
return true
}
var retryCount = 0
var handler = setInterval(function () {
if (judge()) {
onfinish()
clearInterval(handler)
return
}
retryCount++
if (maxRetry != 0 && retryCount >= maxRetry) {
console.warn("重试达到最大次数")
clearInterval(handler)
onerror()
}
}, 500)
}
function doSave(value, type, name) {
var blob;
if (typeof window.Blob == "function") {
blob = new Blob([value], { type: type });
} else {
var BlobBuilder = window.BlobBuilder || window.MozBlobBuilder || window.WebKitBlobBuilder || window.MSBlobBuilder;
var bb = new BlobBuilder();
bb.append(value);
blob = bb.getBlob(type);
}
var URL = window.URL || window.webkitURL;
var bloburl = URL.createObjectURL(blob);
var anchor = document.createElement("a");
if ('download' in anchor) {
anchor.style.visibility = "hidden";
anchor.href = bloburl;
anchor.download = name;
document.body.appendChild(anchor);
var evt = document.createEvent("MouseEvents");
evt.initEvent("click", true, true);
anchor.dispatchEvent(evt);
document.body.removeChild(anchor);
} else if (navigator.msSaveBlob) {
navigator.msSaveBlob(blob, name);
} else {
location.href = bloburl;
}
}
function isArticleLoadSuccess(title) {
console.log("waiting...")
return $("#iframe").$(".topic_title") && $("#iframe").$(".topic_title").innerText == title
}
function downloadArticle(index, title) {
var iframe = $("#iframe")
var headerLinks = iframe.$$("link")
for (var i = 0; i < headerLinks.length; i++) {
if (headerLinks[i].tagName == "LINK") {
var href = headerLinks[i].href
headerLinks[i].href = href
}
}
var headerLinks = iframe.$$("script")
for (var i = 0; i < headerLinks.length; i++) {
if (headerLinks[i].tagName == "SCRIPT") {
var src = headerLinks[i].src
headerLinks[i].src = src
}
}
if (iframe.$(".audioWrapper")) {
iframe.$("#audio").controls = "controls"
iframe.$("#audio").style = ""
iframe.$(".audioWrapper").append(iframe.$("#audio"))
}
var next_icon = iframe.$(".next_icon")
if (next_icon) {
iframe.$(".column_topic_view").removeChild(next_icon.parentNode)
}
var pre_icon = iframe.$(".pre_icon")
if (pre_icon) {
iframe.$(".column_topic_view").removeChild(pre_icon.parentNode)
}
// var header = "<head>" + $("#iframe").$("head").innerHTML + "</head>"
var content = "<html>" + $("#iframe").$("html").innerHTML +
"</html>";
var fileName = formatNumber(index, 2) + "-" + title + ".html"
doSave(content, "text/html", fileName)
console.log("正在下载 " + fileName)
}
function viewArticle(start, end) {
if (start > end) {
console.log("END...")
return;
}
setTimeout(function() {
console.log("休息3秒...");
waitFor(
() => iframe.contentWindow.clickOnTopic != null,
() => {
var selections = $("#iframe").$$(".catelog_item");
var selection = selections[start]
selection.click()
// console.log(selection.getElementsByClassName(".catelog_ready_title>h2"))
var title = selection.querySelector(".catelog_ready_title>h2").innerText;
console.log("正在加载 " + title)
waitFor(
() => isArticleLoadSuccess(title),
() => {
downloadArticle(start, title)
viewArticle(start + 1, end)
},
() => {
downloadArticle(start, title)
viewArticle(start + 1, end)
},
10
)
},
() => { },
0
)
}, 3 * 1000);
}
if (!$(".catalog_infos h2")) {
console.error("页面没有加载完毕,请加载完后重试")
return
}
console.log("正在下载:" + $(".catalog_infos h2").innerText)
var iframe = document.createElement("iframe")
iframe.setAttribute("src", location.href)
iframe.setAttribute("width", "100%")
iframe.setAttribute("height", "500px")
iframe.id = "iframe"
document.getElementsByTagName("body")[0].appendChild(iframe)
iframe.$ = function (selector) {
return this.contentDocument.querySelector(selector)
};
iframe.$$ = function (selector) {
return this.contentDocument.querySelectorAll(selector)
};
iframe.onload = function () {
var selections = this.$$(".catelog_item");
var start = 0
var end = selections.length - 1
// var end = 1
viewArticle(start, end)
iframe.onload = null
}
})()