forked from XIU2/UserScript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
V2ex-Enhanced.user.js
597 lines (561 loc) · 30.6 KB
/
V2ex-Enhanced.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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
// ==UserScript==
// @name V2EX 增强
// @version 1.2.0
// @author X.I.U
// @description 自动签到、链接转图片、自动无缝翻页、使用 SOV2EX 搜索、回到顶部(右键点击两侧空白处)、快速回复(左键双击两侧空白处)、新标签页打开链接、标签页伪装为 Github(摸鱼)
// @match *://v2ex.com/*
// @match *://*.v2ex.com/*
// @match *://www.sov2ex.com/*
// @icon data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAYAAABzenr0AAAAOGVYSWZNTQAqAAAACAABh2kABAAAAAEAAAAaAAAAAAACoAIABAAAAAEAAAAgoAMABAAAAAEAAAAgAAAAAI9OQMkAAARKSURBVHicrVe7SzNLFP/t7CObbEg0WliJooUxcAsfYKFW2lh8NhdLwUIsbMTCBG3EIoid4J/xNd9t7ZSAIiJcNaSIYBcbNVlMsq/M3MK762YziVE8EGbYOTnnd55zRsD/xBiT7+/vf+u6vlir1VTLskApFfADRAhhiqIgEokYsVjsNJVK/S0Igg0AAgAUCoW/np+fr15fX5WfUPgZ9fT0WP39/dNjY2P/SowxOZfLXZXLZUVRFIyPjyORSCAUCkEQmh3AGIMgCGCMAYC3D/IFiVIKy7Lw8vKCfD6PcrmsALhijEWFu7u7P4+Pj78URcHMzAyi0WjXCoPnQeLxV6tVXFxcwLIsDA0N/UN0XV8EgGQyCU3TQClFo9FAo9EAY8xb/Xv33OWllIJSCsaYt/L4GWPQNA3JZBIAoOv6olSr1VQASCQS3h/9FgWtC54FPcDbB2UkEgkAQK1WUyXLsgAAiqK0MFNKIQgCKKVcl3ajMChPFEXIsgwAsCwLkltq/jh1K/C7RAhxAQmSX3DQwo2NDZydnbWcu4D9vKFQCNvb21hdXfUU8ZIwaIQHwD3w50Aul/PQflZyjuPg6OgIoVAIKysrbRUGc6wFgH8lhLRV2K4Es9ksotEolpaWuuobTSEAPhIPACSpCV/XtL+/D03TMD8/3ySP5xHiB+BnYIxBFEUQQkAI8faiKHJ/fh7GGPb29nB9fd0kryOAIBAXgCRJkCQJhBBv5QGSJKmJ33EcpNNp5PP5JpnBkHXMgcvLy65cXiqVsLm5iaenp6bvpmkinU7j+PgYw8PD3BJuCgEAr+kEAXXqCQMDAzg5OUFfX19LaN7e3pDJZGDbNtcLXA+4ICzL6nj5uHu3WtwwBIn3rQVAJws/6/WlUgmZTAa6rnvKXHDxeByHh4cghHgXFRcAT8Hy8nLXVy/P0nA4jGw2i8HBQTiO09kDfgD+PvDdWUCWZRwcHGB0dBTuhcczkhsCfyfslvwACSHY3d1FKpXy8oinvAlAEATw7tLPEjB4DgBbW1uYnJyEYRhfb8V+hJ0AtPPA+vo6ZmdnYZpmizy3urruhFNTU17n83dDf9dz6z0cDmNtbQ0LCwswDKNpNON1QK4Hgi7a2dnh/qkdMcZgmmbbCYo7DxBCGKVU4CWh/ybjCQwqNwyD2zWDSdhoNAC8P1gkWZZhmiYsy4KiKCCEeAz1ev1LpfdZqYqiCMaY1xNkWQZRVdUAgHK5/PHxY2bzVv/eb43fa/7RPMhPCPGGUVeXqqqGFI1GTyuVyq9isQhN0xCJRBAKhbqy8KvE2PvDpFgsAgA0TTsVGGPy+fn5m67riizLGBkZQTweh6Io31YUJEopbNtGpVLBw8MDbNtGLBaz5ubmopIgCPbt7e00gCtd15VCofAjSjtRLBazent7pwVBsD0TGWPyzc3N72q1uliv11XHcX70eS7LMlRVNSKRyOnExIT3PP8P91unlxYYZf4AAAAASUVORK5CYII=
// @grant GM_xmlhttpRequest
// @grant GM_registerMenuCommand
// @grant GM_unregisterMenuCommand
// @grant GM_openInTab
// @grant GM_getValue
// @grant GM_setValue
// @grant GM_notification
// @license GPL-3.0 License
// @run-at document-end
// @namespace https://github.com/XIU2/UserScript
// @supportURL https://github.com/XIU2/UserScript
// @homepageURL https://github.com/XIU2/UserScript
// ==/UserScript==
(function() {
'use strict';
var menu_ALL = [
['menu_autoClockIn', '自动签到', '自动签到', true],
['menu_linksToImgs', '链接转图片', '链接转图片', true],
['menu_pageLoading', '自动无缝翻页', '自动无缝翻页', true],
['menu_pageLoading_reply', '帖子内自动翻页', '帖子内自动翻页', false],
['menu_backToTop', '回到顶部(右键点击两侧空白处)', '回到顶部', true],
['menu_quickReply', '快速回复(左键双击两侧空白处)', '快速回复', true],
['menu_linksBlank', '新标签页打开链接', '新标签页打开链接', true],
['menu_sov2ex', '使用 SOV2EX 搜索', '使用 SOV2EX 搜索', false],
['menu_fish', '标签页伪装为 Github(摸鱼)', '标签页伪装为 Github', false]
], menu_ID = [], pausePage = true;
for (let i=0;i<menu_ALL.length;i++){ // 如果读取到的值为 null 就写入默认值
if (GM_getValue(menu_ALL[i][0]) == null){GM_setValue(menu_ALL[i][0], menu_ALL[i][3])};
}
registerMenuCommand();
// 注册脚本菜单
function registerMenuCommand() {
if (menu_ID.length > menu_ALL.length){ // 如果菜单ID数组多于菜单数组,说明不是首次添加菜单,需要卸载所有脚本菜单
for (let i=0;i<menu_ID.length;i++){
GM_unregisterMenuCommand(menu_ID[i]);
}
}
for (let i=0;i<menu_ALL.length;i++){ // 循环注册脚本菜单
menu_ALL[i][3] = GM_getValue(menu_ALL[i][0]);
menu_ID[i] = GM_registerMenuCommand(`${menu_ALL[i][3]?'✅':'❌'} ${menu_ALL[i][1]}`, function(){menu_switch(`${menu_ALL[i][3]}`,`${menu_ALL[i][0]}`,`${menu_ALL[i][2]}`)});
}
menu_ID[menu_ID.length] = GM_registerMenuCommand('💬 反馈 & 建议', function () {window.GM_openInTab('https://github.com/XIU2/UserScript#xiu2userscript', {active: true,insert: true,setParent: true});window.GM_openInTab('https://greasyfork.org/zh-CN/scripts/424246/feedback', {active: true,insert: true,setParent: true});});
}
// 菜单开关
function menu_switch(menu_status, Name, Tips) {
if (menu_status == 'true'){
GM_setValue(`${Name}`, false);
GM_notification({text: `已关闭 [${Tips}] 功能\n(点击刷新网页后生效)`, timeout: 3500, onclick: function(){location.reload();}});
}else{
GM_setValue(`${Name}`, true);
GM_notification({text: `已开启 [${Tips}] 功能\n(点击刷新网页后生效)`, timeout: 3500, onclick: function(){location.reload();}});
}
registerMenuCommand(); // 重新注册脚本菜单
};
// 返回菜单值
function menu_value(menuName) {
for (let menu of menu_ALL) {
if (menu[0] == menuName) {
return menu[3]
}
}
}
// 默认 ID 为 0
var curSite = {SiteTypeID: 0};
// 自动翻页规则
let DBSite = {
recent: { // 最近主题页
SiteTypeID: 1,
pager: {
type: 1,
nextLink: '//a[@class="page_current"]/following-sibling::a[1][@href]',
pageElement: 'css;.cell.item',
HT_insert: ['//div[@id="Main"]//div[@class="box"]//div[@class="cell"][last()]', 1],
replaceE: 'css;#Main > .box > .cell[style]:not(.item) > table',
scrollDelta: 1500
}
},
notifications: { // 提醒消息页
SiteTypeID: 2,
pager: {
type: 1,
nextLink: '//a[@class="page_current"]/following-sibling::a[1][@href]',
pageElement: 'css;#notifications > div',
HT_insert: ['css;#notifications', 3],
replaceE: 'css;#Main > .box > .cell[style] > table',
scrollDelta: 1500
}
},
replies: { // 用户回复页
SiteTypeID: 3,
pager: {
type: 1,
nextLink: '//a[@class="page_current"]/following-sibling::a[1][@href]',
pageElement: '//div[@id="Main"]//div[@class="box"]//div[@class="dock_area"] | //*[@id="Main"]//div[@class="box"]//div[@class="inner"] | //*[@id="Main"]//div[@class="box"]//div[@class="dock_area"][last()]/following-sibling::div[@class="cell"][1]',
HT_insert: ['//div[@id="Main"]//div[@class="box"]//div[@class="cell"][last()]', 1],
replaceE: 'css;#Main > .box > .cell[style] > table',
scrollDelta: 1500
}
},
go: { // 分类主题页
SiteTypeID: 4,
pager: {
type: 1,
nextLink: '//a[@class="page_current"]/following-sibling::a[1][@href]',
pageElement: 'css;#TopicsNode > div',
HT_insert: ['css;#TopicsNode', 3],
replaceE: 'css;#Main > .box > .cell[style] > table',
scrollDelta: 1500
}
},
reply: { // 帖子内容页(从前往后)
SiteTypeID: 5,
pager: {
type: 1,
nextLink: '//a[@class="page_current"]/following-sibling::a[1][@href]',
pageElement: 'css;.cell[id^="r_"]',
HT_insert: ['//div[starts-with(@id, "r_")][last()]/following-sibling::div[@class="cell"][1]', 1],
replaceE: 'css;#Main > .box > .cell[style] > table',
scrollDelta: 1500
}
},
reply_positive: { // 帖子内容页(从后往前)
SiteTypeID: 6,
pager: {
type: 1,
nextLink: '//a[@class="page_current"]/preceding-sibling::a[1][@href]',
pageElement: 'css;.cell[id^="r_"]',
HT_insert: ['//div[starts-with(@id, "r_")][1]', 1],
replaceE: 'css;#Main > .box > .cell[style] > table',
scrollDelta: 1500
}
},
balance: { // 账户余额页
SiteTypeID: 7,
pager: {
type: 1,
nextLink: '//a[@class="page_current"]/following-sibling::a[1][@href]',
pageElement: 'css;#Main .box > div:not(.cell) > table > tbody > tr:not(:first-child)',
HT_insert: ['css;#Main .box > div:not(.cell) > table > tbody', 3],
replaceE: 'css;#Main > .box > .cell[style] > table',
scrollDelta: 1000
}
},
sov2ex: { // sov2ex
SiteTypeID: 8,
pager: {
nextLink: '.paging>a',
scrollDelta: 1000
}
}
};
if (location.hostname === 'www.sov2ex.com') {
curSite = DBSite.sov2ex;
pageLoading();
} else {
switch (location.pathname) {
case '/': // 首页
addChangesLink();
break;
case '/recent': // 最近主题页
curSite = DBSite.recent;
break;
case '/notifications': // 提醒消息页
curSite = DBSite.notifications;
break;
case '/balance': // 账户余额页
curSite = DBSite.balance;
break;
default:
if (location.pathname.indexOf('/go/') > -1) { // 分类主题页
curSite = DBSite.go;
} else if (location.pathname.indexOf('/t/') > -1) { // 帖子内容页
if(menu_value('menu_pageLoading_reply'))curSite = DBSite.reply; // 帖子内自动无缝翻页
if(menu_value('menu_quickReply'))quickReply(); // 快速回复(双击左右两侧空白处)
} else if (location.pathname.indexOf('/replies') > -1) { // 用户回复页
curSite = DBSite.replies;
}
}
curSite.pageUrl = ''; // 下一页URL
if(menu_value('menu_linksBlank')) linksBlank(); // 新标签页打开链接
if(menu_value('menu_fish')) fish(); // 标签页伪装为 Github(摸鱼)
if(menu_value('menu_autoClockIn')) setTimeout(qianDao, 1000); // 自动签到(后台),延迟 1 秒执行是为了兼容 [V2ex Plus] 扩展
if(menu_value('menu_pageLoading')) pageLoading(); // 自动翻页(无缝)
if(menu_value('menu_backToTop')) backToTop(); // 回到顶部(右键点击左右两侧空白处)
if(menu_value('menu_linksToImgs')) linksToImgs(); // 链接转图片
if(menu_value('menu_sov2ex')) setTimeout(soV2ex, 1000); // 替换为 sov2ex 搜索
}
// 自动签到(后台)
function qianDao() {
let timeNow = new Date().getUTCFullYear() + '/' + (new Date().getUTCMonth() + 1) + '/' + new Date().getUTCDate() // 当前 UTC-0 时间(V2EX 按这个时间的)
if (location.pathname == '/') { // 在首页
let qiandao = document.querySelector('.box .inner a[href="/mission/daily"]');
if (qiandao) { // 如果找到了签到提示
qianDao_(qiandao, timeNow); // 后台签到
} else if (document.getElementById('gift_v2excellent')) { // 兼容 [V2ex Plus] 扩展
document.getElementById('gift_v2excellent').click();
GM_setValue('menu_clockInTime', timeNow); // 写入签到时间以供后续比较
console.info('[V2EX 增强] 自动签到完成!')
} else { // 都没有找到,说明已经签过到了
console.info('[V2EX 增强] 已经签过到了。')
}
} else { // 不在首页
let timeOld = GM_getValue('menu_clockInTime')
if (!timeOld || timeOld != timeNow) {
qianDaoStatus_(timeNow) // 后台获取签到状态(并判断是否需要签到)
}/* else { // 新旧签到时间一致
console.info('[V2EX 增强] 已经签过到了。')
}*/
}
}
// 后台签到
function qianDao_(qiandao, timeNow) {
let url = (location.origin + "/mission/daily/redeem?" + RegExp("once\\=(\\d+)").exec(document.querySelector('div#Top .tools, #menu-body').innerHTML)[0]);
GM_xmlhttpRequest({
url: url,
method: 'GET',
timeout: 5000,
onload: function (response) {
let html = ShowPager.createDocumentByString(response.responseText);
//console.log(html)
if (html.querySelector('li.fa.fa-ok-sign')) {
html = html.getElementById('Main').textContent.match(/已连续登录 (\d+?) 天/)[0];
GM_setValue('menu_clockInTime', timeNow); // 写入签到时间以供后续比较
console.info('[V2EX 增强] 自动签到完成!')
if (qiandao) {
qiandao.textContent = `自动签到完成!${html}`;
qiandao.href = 'javascript:void(0);';
}
} else {
GM_notification({text: '自动签到失败!请访问 V2EX 首页试试。\n如果连续几天都签到失败,请联系作者解决!', timeout: 4000, onclick() {window.GM_openInTab('https://github.com/XIU2/UserScript#xiu2userscript', {active: true,insert: true,setParent: true});window.GM_openInTab('https://greasyfork.org/zh-CN/scripts/424246/feedback', {active: true,insert: true,setParent: true});}});
console.warn('[V2EX 增强] 自动签到失败!请访问 V2EX 首页试试。如果连续几天都签到失败,请联系作者解决!')
if (qiandao) qiandao.textContent = '自动签到失败!请尝试手动签到!';
}
}
});
}
// 后台获取签到状态(并判断是否需要签到)
function qianDaoStatus_(timeNow) {
GM_xmlhttpRequest({
url: 'https://www.v2ex.com/mission/daily',
method: 'GET',
timeout: 5000,
onload: function (response) {
let html = ShowPager.createDocumentByString(response.responseText);
if (html.querySelector('input[value^="领取"]')) { // 还没有签到...
qianDao_(null, timeNow); // 后台签到
} else { // 已经签到了...
console.info('[V2EX 增强] 已经签过到了。')
GM_setValue('menu_clockInTime', timeNow); // 写入签到时间以供后续比较
}
}
});
}
// 替换为 sov2ex 搜索,代码来自 v2ex-plus 扩展:https://github.com/sciooga/v2ex-plus (懒得重复造轮子了~
function soV2ex() {
document.body.appendChild(document.createElement('script')).textContent = `
var $search = $('#search')
var searchEvents = $._data($search[0], "events" )
var oKeydownEvent = searchEvents['keydown'][0]['handler']
var oInputEvent = searchEvents['input'][0]['handler']
$search.attr("placeholder","sov2ex")
$search.unbind('keydown', oKeydownEvent)
$search.unbind('input', oInputEvent)
$search.on('input', function(e) {
oInputEvent(e)
$('.search-item:last').attr('href', 'https://www.sov2ex.com/?q=' + $search.val()).text('sov2ex ' +$search.val());
})
$search.keydown(function(e) {
if (e.code == 'Enter' || e.code == 'NumpadEnter' || e.keyCode === 13) {
if ($('.search-item:last').is('.active')) {
$(this).val($(this).val().replace(/[#%&]/g,""));//用户输入不能包含特殊字符#%&
window.open("https://www.sov2ex.com/?q=" + $(this).val());
return 0
}
}
oKeydownEvent(e)
})
`;
}
// 回到顶部(右键左右两侧空白处)
function backToTop() {
document.getElementById('Wrapper').oncontextmenu = document.querySelector('#Wrapper > .content').oncontextmenu = function(event){
if (event.target == this) {
event.preventDefault();
window.scrollTo(0,0)
}
}
}
// 标签页伪装为 Github(摸鱼)
function fish() {
window.document.title = 'GitHub'
if (window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches) {
document.querySelector("link[rel*='shortcut icon']").href = 'https://github.githubassets.com/favicons/favicon-dark.png'
} else {
document.querySelector("link[rel*='shortcut icon']").href = 'https://github.githubassets.com/favicons/favicon.png'
}
}
// 链接转图片
function linksToImgs() {
let links = document.links;
Array.from(links).forEach(function (_this) {
if (/^https.*\.(?:jpg|jpeg|jpe|bmp|png|gif)/i.test(_this.href) && !(/<img\s/i.test(_this.innerHTML))) {
_this.innerHTML = `<img src="${_this.href}" style="max-width: 100%!important;" />`;
} else if (/^https:\/\/imgur\.com\/[a-z]+$/i.test(_this.href)) { // 针对没有文件后缀的 imgur 图床链接
_this.innerHTML = `<img src="${_this.href}.png" style="max-width: 100%!important;" />`;
}
});
}
// 快速回复(双击左右两侧空白处)
function quickReply() {
document.getElementById('Wrapper').ondblclick = document.querySelector('#Wrapper > .content').ondblclick = function(event){
if (event.target==this) {
if (document.querySelector('.box.reply-box-sticky')) {
document.getElementById('undock-button').click();
} else {
let _top = document.body.scrollTop + document.documentElement.scrollTop;
document.getElementById('reply_content').focus();
window.scrollTo(0,_top);console.log(_top);
}
}
}
}
// 新标签页打开链接
function linksBlank() {
if (location.pathname.indexOf('/settings') > -1) return
document.head.appendChild(document.createElement('base')).target = '_blank'; // 让所有链接默认以新标签页打开
Array.from(document.links).forEach(function (_this) {
if (_this.onclick || _this.href.slice(0,4) != 'http' || _this.href.indexOf('#;') > -1 || _this.href.indexOf('night/toggle') > -1 || _this.href.indexOf('/favorite') > -1 || _this.href.indexOf('/?tab=') > -1) {
_this.target = '_self'
}
})
document.querySelectorAll('form').forEach(function (_this) {
if (!_this.target) {_this.target = '_self'}
});
const callback = (mutationsList, observer) => {
for (const mutation of mutationsList) {
for (const target of mutation.addedNodes) {
if (target.nodeType != 1) return
if (target.tagName === 'A') {
if (target.onclick || target.href.slice(0,4) != 'http' || target.href.indexOf('#;') > -1 || target.href.indexOf('night/toggle') > -1 || target.href.indexOf('/favorite') > -1) {
target.target = '_self'
}
} else {
document.querySelectorAll('a').forEach(function (_this) {
if (_this.onclick || _this.href.slice(0,4) != 'http' || _this.href.indexOf('#;') > -1 || _this.href.indexOf('night/toggle') > -1 || _this.href.indexOf('/favorite') > -1) {
_this.target = '_self'
}
});
}
}
}
};
const observer = new MutationObserver(callback);
observer.observe(document, { childList: true, subtree: true });
}
// 添加全站最近更新主题链接
function addChangesLink() {
let links = document.querySelector('#Main .box .inner:last-child');if (!links) return
links.innerHTML = `<div style="float: left;"><span class="chevron">»</span> <a href="/recent" target="_blank">更多新主题</a></div><div style="text-align: right;"><a href="/changes" target="_blank" style="text-align: right;">全站最近更新主题</a> <span class="chevron">«</span></div>`
}
// 自动无缝翻页
function pageLoading() {
if (curSite.SiteTypeID > 0){
windowScroll(function (direction, e) {
// 下滑 且 未暂停翻页 且 SiteTypeID > 0 时,才准备翻页
if (direction != 'down' || !pausePage) return
let scrollTop = document.documentElement.scrollTop || window.pageYOffset || document.body.scrollTop,
scrollHeight = window.innerHeight || document.documentElement.clientHeight,
scrollDelta = curSite.pager.scrollDelta;
if (document.documentElement.scrollHeight <= scrollHeight + scrollTop + scrollDelta) {
if (curSite.pager.type === 1) {
ShowPager.loadMorePage();
}else{
let autopbn = document.querySelector(curSite.pager.nextLink);
if (autopbn){
autopbn.click();
pausePage = false
setTimeout(function(){pausePage = true;}, 500)
}
}
}
});
}
}
// 滚动条事件
function windowScroll(fn1) {
var beforeScrollTop = document.documentElement.scrollTop || document.body.scrollTop,
fn = fn1 || function () {};
setTimeout(function () { // 延时执行,避免刚载入到页面就触发翻页事件
window.addEventListener('scroll', function (e) {
var afterScrollTop = document.documentElement.scrollTop || document.body.scrollTop,
delta = afterScrollTop - beforeScrollTop;
if (delta == 0) return false;
fn(delta > 0 ? 'down' : 'up', e);
beforeScrollTop = afterScrollTop;
}, false);
}, 1000)
}
// 修改自 https://greasyfork.org/scripts/14178 , https://github.com/machsix/Super-preloader
var ShowPager = {
getFullHref: function (e) {
if (e != null && e.nodeType === 1 && e.href && e.href.slice(0,4) === 'http') return e.href;
return '';
},
createDocumentByString: function (e) {
if (e) {
if ('HTML' !== document.documentElement.nodeName) return (new DOMParser).parseFromString(e, 'application/xhtml+xml');
var t;
try { t = (new DOMParser).parseFromString(e, 'text/html');} catch (e) {}
if (t) return t;
if (document.implementation.createHTMLDocument) {
t = document.implementation.createHTMLDocument('ADocument');
} else {
try {((t = document.cloneNode(!1)).appendChild(t.importNode(document.documentElement, !1)), t.documentElement.appendChild(t.createElement('head')), t.documentElement.appendChild(t.createElement('body')));} catch (e) {}
}
if (t) {
var r = document.createRange(),
n = r.createContextualFragment(e);
r.selectNodeContents(document.body);
t.body.appendChild(n);
for (var a, o = { TITLE: !0, META: !0, LINK: !0, STYLE: !0, BASE: !0}, i = t.body, s = i.childNodes, c = s.length - 1; c >= 0; c--) o[(a = s[c]).nodeName] && i.removeChild(a);
return t;
}
} else console.error('没有找到要转成 DOM 的字符串');
},
loadMorePage: function () {
if (curSite.pager) {
let curPageEle = getElementByXpath(curSite.pager.nextLink);
var url = this.getFullHref(curPageEle);
console.log(`${url} ${curPageEle} ${curSite.pageUrl}`);
if(url === '') return;
if(curSite.pageUrl === url) return;// 不会重复加载相同的页面
curSite.pageUrl = url;
// 读取下一页的数据
curSite.pager.startFilter && curSite.pager.startFilter();
GM_xmlhttpRequest({
url: url,
method: "GET",
timeout: 5000,
onload: function (response) {
try {
var newBody = ShowPager.createDocumentByString(response.responseText);
let pageElems = getAllElements(curSite.pager.pageElement, newBody, newBody);
let toElement = getAllElements(curSite.pager.HT_insert[0])[0];
if (pageElems.length >= 0) {
// 如果有插入前函数就执行函数
if (curSite.function && curSite.function.before) {
if (curSite.function.parameter) { // 如果指定了参数
pageElems = curSite.function.before(curSite.function.parameter);
}else{
pageElems = curSite.function.before(pageElems);
}
}
// 插入位置
let addTo;
switch (curSite.pager.HT_insert[1]) {
case 1:
addTo = "beforebegin"
break;
case 2:
addTo = "afterbegin"
break;
case 3:
addTo = "beforeend"
break;
case 4:
addTo = "afterend"
break;
}
// 插入新页面元素
pageElems.forEach(function (one) {
toElement.insertAdjacentElement(addTo, one);
});
// 替换待替换元素
try {
let oriE = getAllElements(curSite.pager.replaceE);
let repE = getAllElements(curSite.pager.replaceE, newBody, newBody);
if (oriE.length === repE.length) {
for (var i = 0; i < oriE.length; i++) {
oriE[i].outerHTML = repE[i].outerHTML;
}
}
} catch (e) {
console.log(e);
}
// 如果有插入后函数就执行函数
if (curSite.function && curSite.function.after) {
if (curSite.function.parameter) { // 如果指定了参数
curSite.function.after(curSite.function.parameter);
}else{
curSite.function.after();
}
}
}
} catch (e) {
console.log(e);
}
}
});
}
},
};
function getElementByCSS(css, contextNode = document) {
return contextNode.querySelector(css);
}
function getAllElementsByCSS(css, contextNode = document) {
return [].slice.call(contextNode.querySelectorAll(css));
}
function getElementByXpath(xpath, contextNode, doc = document) {
contextNode = contextNode || doc;
try {
const result = doc.evaluate(xpath, contextNode, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null);
// 应该总是返回一个元素节点
return result.singleNodeValue && result.singleNodeValue.nodeType === 1 && result.singleNodeValue;
} catch (err) {
throw new Error(`Invalid xpath: ${xpath}`);
}
}
function getAllElementsByXpath(xpath, contextNode, doc = document) {
contextNode = contextNode || doc;
const result = [];
try {
const query = doc.evaluate(xpath, contextNode, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
for (let i = 0; i < query.snapshotLength; i++) {
const node = query.snapshotItem(i);
// 如果是 Element 节点
if (node.nodeType === 1) result.push(node);
}
} catch (err) {
throw new Error(`无效 Xpath: ${xpath}`);
}
return result;
}
function getAllElements(selector, contextNode = undefined, doc = document, win = window, _cplink = undefined) {
if (!selector) return [];
contextNode = contextNode || doc;
if (typeof selector === 'string') {
if (selector.search(/^css;/i) === 0) {
return getAllElementsByCSS(selector.slice(4), contextNode);
} else {
return getAllElementsByXpath(selector, contextNode, doc);
}
} else {
const query = selector(doc, win, _cplink);
if (!Array.isArray(query)) {
throw new Error('getAllElements 返回错误类型');
} else {
return query;
}
}
}
})();