forked from phantom-sea-limited/Crawler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlightnovel.us.user.js
360 lines (334 loc) · 14.7 KB
/
lightnovel.us.user.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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
// ==UserScript==
// @name lightnovel.us下载工具
// @namespace Rcrwrate
// @version 1.0.1
// @description 防止防火墙,直接采用前端js进行爬虫
// @author Rcrwrate
// @match https://www.lightnovel.us/*
// @require https://static.sirin.top/https://cdn.jsdelivr.net/gh/mozilla/localForage/dist/localforage.min.js
// @icon https://api.phantom-sea-limited.ltd/favicon.ico
// @updateURL https://static.sirin.top/https://github.com/phantom-sea-limited/Crawler/raw/main/lightnovel.us.user.js
// @downloadURL https://static.sirin.top/https://github.com/phantom-sea-limited/Crawler/raw/main/lightnovel.us.user.js
// @grant none
// @license MIT
// ==/UserScript==
class Article {
ID;
bookname = null;
cover = null;
details = null;
author = null;
tag = null;
chapterList = [];
init_status = false;
load_status = false
constructor(ID, mode = "normal") {
this.ID = ID;
this.mode = mode
// this.load();
}
async init() {
this.init_status = true
Task.add(
[
Task.createBymethod(this.ID, "fetchInfo", this.mode),
Task.createBymethod(this.ID, "fetchCatalog", this.mode),
Task.createBymethod(this.ID, "PrefetchChapter", this.mode),
Task.createBymethod(this.ID, "file", this.mode),
]
)
await this.save()
setTimeout(Task.init, 200)
}
async reinit() {
await this.load()
if (this.init_status != true) {
Task.add(
[
Task.createBymethod(this.ID, "fetchInfo", this.mode),
Task.createBymethod(this.ID, "fetchCatalog", this.mode),
Task.createBymethod(this.ID, "PrefetchChapter", this.mode),
Task.createBymethod(this.ID, "file", this.mode),
]
)
setTimeout(Task.init, 200)
}
}
async fetchInfo() {
await this.load()
if (document.location.href == 'https://www.lightnovel.us/cn/series/' + this.ID) {
var main = document.getElementsByClassName("top-title")[0]
this.details = window.__NUXT__.data[0].series.intro
this.author = window.__NUXT__.data[0].series.author
this.bookname = main.children[0].innerText
this.book_uptime = window.__NUXT__.data[0].series.last_time
this.cover = window.__NUXT__.data[0].series.cover
this.tag = ""
} else {
Task.add(Task.createBymethod(this.ID, "fetchInfo", this.mode), "push")
window.Task_STOP = true
document.location.href = 'https://www.lightnovel.us/cn/series/' + this.ID
}
}
async fetchCatalog() {
await this.load()
if (document.location.href == `https://www.lightnovel.us/cn/series/` + this.ID) {
var oldchaperList = [...this.chapterList]
this.chapterList = [[]]
window.__NUXT__.data[0].pages[0].forEach(chap => {
this.chapterList[0].push({
"name": chap['title'],
"href": "https://www.lightnovel.us/cn/detail/" + chap['aid'],
"content": "",
"cover": chap['cover'],
})
})
} else {
Task.add(Task.createBymethod(this.ID, "fetchCatalog", this.mode), "push")
window.Task_STOP = true
document.location.href = `https://www.lightnovel.us/cn/series/` + this.ID
}
}
async fetchChapter(i, j) {
await this.load()
var chap = this.chapterList[i][j]
if (document.location.href == chap.href) {
chap.content = document.getElementById("article-main-contents").innerHTML.replaceAll("<br>", "\n")
} else {
Task.add(Task.create(this.ID, [`await A.fetchChapter(${i},${j})`], this.mode), "push")
window.Task_STOP = true
document.location.href = chap.href
}
}
async PrefetchChapter() {
await this.load()
if (this.chapterList != null) {
var tasklists = []
var i = 0
this.chapterList.forEach(chap => {
var j = 0
chap.forEach(chapinfo => {
if (chapinfo["content"] == "") {
tasklists.unshift(Task.create(this.ID, [`await A.fetchChapter(${i},${j})`], this.mode))
}
j++
})
i++
})
if (tasklists.length != 0) {
tasklists.unshift(Task.createBymethod(this.ID, "PrefetchChapter", this.mode))
}
Task.add(tasklists, "push")
}
}
output() {
return {
'bookname': this.bookname,
'cover': this.cover,
'book_uptime': this.book_uptime,
'details': this.details,
'author': this.author,
'tag': this.tag,
'chapterList': this.chapterList,
}
}
async file() {
await this.load()
let link = document.createElement('a');
link.download = `${this.bookname}.json`;
let blob = new Blob([JSON.stringify(this.output())], { type: 'text/json' });
link.href = URL.createObjectURL(blob);
link.click();
URL.revokeObjectURL(link.href);
}
async load() {
if (this.load_status != true) {
if (this.mode == "normal") {
var tmp = Article.localconfig(this.ID);
} else {
var tmp = await Article.localforge(this.ID)
if (tmp == null) { tmp = Article.localconfig(this.ID) }
}
if (tmp != null) {
this.bookname = tmp.bookname;
this.details = tmp.details;
this.author = tmp.author;
this.tag = tmp.tag
this.chapterList = tmp.chapterList;
this.cover = tmp.cover;
this.book_uptime = tmp.book_uptime;
} else {
await this.init()
}
this.load_status = true
}
}
async save() {
if (this.mode == "normal") {
Article.localconfig(this.ID, this.output())
} else {
await Article.localforge(this.ID, this.output())
}
}
static localconfig(key, msg = '') {
if (msg === '') {
return JSON.parse(localStorage.getItem(key));
} else {
return localStorage.setItem(key, JSON.stringify(msg))
}
}
static async localforge(key, msg = "") {
var localforge = window.localforage
if (msg === "") {
return JSON.parse(await localforge.getItem(key))
} else {
return await localforge.setItem(key, JSON.stringify(msg))
}
}
}
window.Task_STOP = false
class Task {
static createBymethod(ID, method, mode = "normal") {
return `
var A = new Article(${ID},\"${mode}\")
A.${method}().then(res=>{A.save()})`
}
static create(ID, lines, mode = "normal") {
function _(lines) {
var command = ""
lines.forEach(element => {
command += element + "\n"
})
return command
}
return `
var A = new Article(${ID},\"${mode}\")
async function _(){
${_(lines)}
}
_().then(res=>{A.save()})`
}
static add(command, mode = "unshift") {
var commands = window.Task_info
if (commands == null) { commands = [] }
if (typeof (command) == 'object') {
command.forEach(element => {
if (mode == "unshift") { commands.unshift(element) } else { commands.push(element) }
})
} else {
if (mode == "unshift") { commands.unshift(command) } else { commands.push(command) }
}
Task.localconfig(commands)
}
static init() {
window.Task_info = Task.localconfig()
var commands = window.Task_info
var command;
if (commands != null) {
if (commands.length != 0 && window.Task_STOP != true) {
command = commands.pop()
eval(command)
console.log(command)
Task.localconfig(commands)
setTimeout(Task.init, 200)
}
}
console.log("Task finish! waiting for another")
}
static localconfig(msg = '') {
if (msg === '') {
return JSON.parse(localStorage.getItem("Task"));
} else {
return localStorage.setItem("Task", JSON.stringify(msg))
}
}
}
window.Task_info = Task.localconfig()
window.Article = Article
window.Task = Task
window.Task_statu = null
window.onload = function _init() {
window.Task_statu = "installed"
add_button()
setTimeout(Task.init)
// document.getElementsByClassName("sidebar-buttons")[0].children[2].click() //自动点击繁简切换按钮
}
setTimeout(check_Task_status, 5000)
function check_Task_status() {
if (window.Task_statu != "installed") {
window.onload()
setTimeout(check_Task_status, 5000)
}
}
function add_task_status() {
var l = window.Task_info
var msg
if (l == null) { msg = "任务已完成" }
else if (l.length != 0) { msg = `任务剩余${l.length}` }
else { msg = "任务已完成" }
document.getElementsByClassName("nav-dl")[0].innerText = msg
if (msg != "任务已完成") {
setTimeout(add_task_status, 2000)
}
}
function add_button() {
IDs = document.location.href.match(/https:\/\/www.lightnovel.us\/cn\/series\/(\d+)$/)
function create(svg, text, func) {
var button = document.createElement("button")
button.innerHTML = svg + text
button.onclick = func
button.classList.add("btn")
button.classList.add("btn-rate")
button.classList.add("mar-right-20")
return button
}
if (IDs != null) {
IDs = IDs[1]
var main = document.getElementsByClassName("btns mar-top-30")[0]
main.children[1].classList.remove("btn-collect")
main.children[1].classList.add("btn-rate")
main.children[1].classList.add("mar-right-20")
main.append(create(
`<svg xmlns="http://www.w3.org/2000/svg" height="1em" viewBox="0 0 512 512"><!--! Font Awesome Free 6.4.0 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license (Commercial License) Copyright 2023 Fonticons, Inc. --><style>svg{fill:#cfcfcf}</style><path d="M288 32c0-17.7-14.3-32-32-32s-32 14.3-32 32V274.7l-73.4-73.4c-12.5-12.5-32.8-12.5-45.3 0s-12.5 32.8 0 45.3l128 128c12.5 12.5 32.8 12.5 45.3 0l128-128c12.5-12.5 12.5-32.8 0-45.3s-32.8-12.5-45.3 0L288 274.7V32zM64 352c-35.3 0-64 28.7-64 64v32c0 35.3 28.7 64 64 64H448c35.3 0 64-28.7 64-64V416c0-35.3-28.7-64-64-64H346.5l-45.3 45.3c-25 25-65.5 25-90.5 0L165.5 352H64zm368 56a24 24 0 1 1 0 48 24 24 0 1 1 0-48z"/></svg>`,
'下载', function () {
var A = new Article(window.__NUXT__.data[0].series.sid, "async")
A.load().then(res => { A.reinit() })
}
))
main.append(create(
`<svg xmlns="http://www.w3.org/2000/svg" height="1em" viewBox="0 0 512 512"><!--! Font Awesome Free 6.4.0 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license (Commercial License) Copyright 2023 Fonticons, Inc. --><style>svg{fill:#cfcfcf}</style><path d="M288 32c0-17.7-14.3-32-32-32s-32 14.3-32 32V274.7l-73.4-73.4c-12.5-12.5-32.8-12.5-45.3 0s-12.5 32.8 0 45.3l128 128c12.5 12.5 32.8 12.5 45.3 0l128-128c12.5-12.5 12.5-32.8 0-45.3s-32.8-12.5-45.3 0L288 274.7V32zM64 352c-35.3 0-64 28.7-64 64v32c0 35.3 28.7 64 64 64H448c35.3 0 64-28.7 64-64V416c0-35.3-28.7-64-64-64H346.5l-45.3 45.3c-25 25-65.5 25-90.5 0L165.5 352H64zm368 56a24 24 0 1 1 0 48 24 24 0 1 1 0-48z"/></svg>`,
'修复下载', function () {
var A = new Article(window.__NUXT__.data[0].series.sid, "async")
A.PrefetchChapter().then(res => { Task.init() })
}
))
main.append(create(
`<svg xmlns="http://www.w3.org/2000/svg" height="1em" viewBox="0 0 448 512"><!--! Font Awesome Free 6.4.0 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license (Commercial License) Copyright 2023 Fonticons, Inc. --><style>svg{fill:#adadad}</style><path d="M64 32C28.7 32 0 60.7 0 96V416c0 35.3 28.7 64 64 64H384c35.3 0 64-28.7 64-64V173.3c0-17-6.7-33.3-18.7-45.3L352 50.7C340 38.7 323.7 32 306.7 32H64zm0 96c0-17.7 14.3-32 32-32H288c17.7 0 32 14.3 32 32v64c0 17.7-14.3 32-32 32H96c-17.7 0-32-14.3-32-32V128zM224 288a64 64 0 1 1 0 128 64 64 0 1 1 0-128z"/></svg>`,
'手动导出', function () {
var A = new Article(window.__NUXT__.data[0].series.sid, "async")
A.file()
}
))
main.append(create(
`<svg xmlns="http://www.w3.org/2000/svg" height="1em" viewBox="0 0 384 512"><!--! Font Awesome Free 6.4.0 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license (Commercial License) Copyright 2023 Fonticons, Inc. --><style>svg{fill:#999999}</style><path d="M342.6 150.6c12.5-12.5 12.5-32.8 0-45.3s-32.8-12.5-45.3 0L192 210.7 86.6 105.4c-12.5-12.5-32.8-12.5-45.3 0s-12.5 32.8 0 45.3L146.7 256 41.4 361.4c-12.5 12.5-12.5 32.8 0 45.3s32.8 12.5 45.3 0L192 301.3 297.4 406.6c12.5 12.5 32.8 12.5 45.3 0s12.5-32.8 0-45.3L237.3 256 342.6 150.6z"/></svg>`,
'清空缓存', function () {
localforage.clear()
var vuex = localStorage.getItem("vuex")
var last_read = localStorage.getItem("last_read")
localStorage.clear()
localStorage.setItem("vuex", vuex)
localStorage.setItem("last_read", last_read)
}
))
}
var lab = document.getElementsByClassName("nav flex jc-between")[0]
lab.insertBefore(create(
`<svg xmlns="http://www.w3.org/2000/svg" height="1em" viewBox="0 0 384 512"><!--! Font Awesome Free 6.4.0 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license (Commercial License) Copyright 2023 Fonticons, Inc. --><style>svg{fill:#999999}</style><path d="M342.6 150.6c12.5-12.5 12.5-32.8 0-45.3s-32.8-12.5-45.3 0L192 210.7 86.6 105.4c-12.5-12.5-32.8-12.5-45.3 0s-12.5 32.8 0 45.3L146.7 256 41.4 361.4c-12.5 12.5-12.5 32.8 0 45.3s32.8 12.5 45.3 0L192 301.3 297.4 406.6c12.5 12.5 32.8 12.5 45.3 0s12.5-32.8 0-45.3L237.3 256 342.6 150.6z"/></svg>`,
'终止任务', function () {
window.Task_STOP = true
window.Task_info = []
Task.localconfig([])
}
), lab.children[2])
setTimeout(add_task_status)
}