-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathopennet.surr.user.js
140 lines (123 loc) · 3.63 KB
/
opennet.surr.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
// ==UserScript==
// @name OpenNet.ru_surogate
// @description Allows to unfold comments and vote on OpenNet.ru
// @author KOLANICH
// @version 0.1
// @license Unlicense
// @grant none
// @run-at document-idle
// @include /https:\/\/(www\.)?opennet\.ru\/.+/
// ==/UserScript==
"use strict";
function getBaseUriStr() {
const u = new URL(document.querySelector("form[method=post]").action);
const pn = u.pathname.split("/");
pn[pn.length - 1] = "ajax2.cgi";
u.pathname = pn.join("/");
const apiAddr = u.href;
pn[pn.length - 1] = "vsluhboard.cgi";
u.pathname = pn.join("/");
const forumAddr = u.href;
return [apiAddr, forumAddr].map(e => e + "?");
}
const [apiAddr, forumAddr] = getBaseUriStr();
function getRespondURI(om, omm, forum) {
return forumAddr + (new URLSearchParams([
["az", "post"],
["om", om],
["forum", "vsluhforumID3"],
["omm", omm],
//["news_key", news_key],
]));
}
function getViewURI(om, omm, forum) {
return forumAddr + (new URLSearchParams([
["az", "show_thread"],
["om", om],
["forum", "vsluhforumID3"],
["omm", omm],
]));
}
function getVoteURI(id, delta) {
return apiAddr + (new URLSearchParams([
["rs", "vote"],
["id", id],
["vote", delta]
]));
}
function getHiddenThreadURI(om, omm, forum) {
return apiAddr + (new URLSearchParams([
["rs", "get_thread"],
["rsargs", om],
["rsargs", omm],
["rsargs", forum],
]));
}
function vote(id, delta) {
alert("voting...");
return fetch(getVoteURI(id, delta)).then(r => r.text())
//OK FORUM {id}=1
//ERR1 {id}=2
}
function getHiddenThread(om, omm, forum) {
return fetch(getHiddenThreadURI(om, omm, forum)).then(r => r.blob()).then(b => b.arrayBuffer()).then(b => {
const dec = new TextDecoder(document.charset);
const t = dec.decode(b);
const status = t.substring(0, 2);
const text = t.substring(2);
return [status, text];
});
}
function votingLinkClickProcessor(evt) {
const spanEl = evt.target
const linkEl = spanEl.parentElement;
const containerEl = linkEl.parentElement;
const voteId = containerEl.id.substring(3);
const cn = spanEl.className;
vote(voteId, (cn.substring(cn.length - 2) == "p" ? 1 : -1)).then(t => alert(t));
}
function setupVoting() {
[...document.getElementsByClassName("vt_d"), ...document.getElementsByClassName("vt_d2")].forEach((el) => {
const id = el.id;
if (id.substring(0, 3) == "vt_") {
for (const cls of ["vt_p", "vt_m"]) {
el.getElementsByClassName(cls)[0].addEventListener("click", votingLinkClickProcessor, true);
}
}
});
}
setupVoting();
const showThreadCallRx = /do_show_thread0\s*\(\s*(\d+)\s*,\s*(\d+)\s*,\s*(\d+)\s*\)\s*;.*/;
function parseExpansionParams(el) {
const oc = el.getAttribute("onclick");
const m = showThreadCallRx.exec(oc);
if (m) {
const args = m.slice(1);
return args;
}
}
function expandLinkClickProcessor(evt) {
evt.preventDefault();
evt.stopPropagation();
const el = evt.target
const args = JSON.parse(el.dataset["args"]);
if (args) {
getHiddenThread(...args).then(([status, text]) => {
const cells = el.parentElement.parentElement.parentElement.parentElement.getElementsByTagName("td");
const textEl = cells[cells.length - 1];
textEl.innerHTML = text;
el.removeEventListener("click", expandLinkClickProcessor, true);
el.href = getRespondURI(...args);
el.textContent = "[ответ]";
});
}
}
function setupExpansion() {
[...document.querySelectorAll("a[onclick]")].filter(e => e.getAttribute("onclick").indexOf("do_show_thread0") == 0).forEach(el => {
const args = parseExpansionParams(el);
el.dataset["args"] = JSON.stringify(args);
el.href = getViewURI(...args);
el.addEventListener("click", expandLinkClickProcessor, true);
});
}
setupExpansion();