forked from ipcjs/bilibili-helper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paths1_show_all_post.user.js
126 lines (116 loc) · 4.24 KB
/
s1_show_all_post.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
// ==UserScript==
// @name 列出S1一条帖子的所有内容
// @namespace https://github.com/ipcjs
// @version 0.1.1
// @description 在帖子的导航栏添加[显示全部]按钮, 列出帖子的所有内容
// @author ipcjs
// @include *://bbs.saraba1st.com/2b/thread-*-*-*.html
// @include *://bbs.saraba1st.com/2b/forum.php*
// @grant GM_xmlhttpRequest
// @grant GM_addStyle
// @grant unsafeWindow
// @connect bbs.saraba1st.com
// ==/UserScript==
(function () {
let group, filter;
if (!(group = /thread-(\d+)-(\d+)-(\d+)/.exec(location.pathname))
&& !(group = /tid=(\d+)/.exec(location.search))) {
return; // 不匹配则返回
}
const POST_PAGE_MAX_COUNT = 1000; // 一次最多拉取多少条
const TID = group[1];
switch (TID) {
case '1494926': filter = f_1494926; break;
default: filter = f_all; break;
}
addButton();
GM_addStyle(`
#list-s1-table tr {
border-top: 1px solid #888;
}
#load-all-post {
margin: 0px 10px;
}
`)
function addButton() {
let button = document.createElement('a');
button.innerText = '[显示全部]';
button.id = 'load-all-post';
button.href = 'javascript:;';
button.addEventListener('click', () => {
loadAllPost();
});
document.querySelector('#pt > div.z').appendChild(button);
}
function loadAllPost() {
(async function () {
let json, page = 1, list = [];
while (true) {
let resp = await ajaxPromise({ url: `http://bbs.saraba1st.com/2b/api/mobile/index.php?module=viewthread&ppp=${POST_PAGE_MAX_COUNT}&tid=${TID}&page=${page}&version=1` });
json = JSON.parse(resp.responseText);
Array.prototype.push.apply(list, json.Variables.postlist);
log('>>', page, list.length, json.Variables.thread);
if (list.length <= +json.Variables.thread.replies) { // 总post条数为replies + 1
page++;
} else {
break;
}
}
show(list.filter(filter), [
item => `<a href='forum.php?mod=redirect&goto=findpost&ptid=${item.ptid}&pid=${item.pid}'>${item.number}</a>`,
'username',
'dateline',
'message'
], json.Variables.thread.subject);
})().catch((e) => {
show([{ error: e }], ['error'], 'Result: error');
});
}
function f_1494926(item) {
if (item.username === 'ipcjs') {
return true;
} else if (['SUNSUN', '蒹葭公子', '木水风铃'].includes(item.username) && item.message.includes('ipcjs 发表于')) {
return true;
}
return false;
}
function f_all() {
return true;
}
// show([{ name: 'ipcjs', age: 17 }, { name: 'fuck', age: 1 }], ['name', 'age']);
function show(list, colNames, title) {
let table = document.createElement('table');
list.forEach(item => {
let tr = document.createElement('tr');
colNames.forEach(name => {
let td = document.createElement('td');
td.innerHTML = typeof name === 'function' ? name(item) : item[name];
tr.appendChild(td);
});
table.appendChild(tr);
});
table.id = 'list-s1-table';
let div = document.createElement('div');
let h1 = document.createElement('h1');
h1.innerText = (title ? title : 'Title') + `(${list.length})`;
div.appendChild(h1);
div.appendChild(table);
// document.querySelector('body').appendChild(div);
document.getElementById('ct').insertBefore(div, document.getElementById('postlist'));
}
function log(...args) {
console.log(...args);
}
function ajaxPromise(options) {
return new Promise((resolve, reject) => {
options.method = options.method || 'GET';
options.onload = function (resp) {
resolve(resp);
}
options.onerror = function (resp) {
reject(resp);
};
GM_xmlhttpRequest(options);
});
}
})();