-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsamlib-optimized.user.js
More file actions
301 lines (236 loc) · 9.98 KB
/
samlib-optimized.user.js
File metadata and controls
301 lines (236 loc) · 9.98 KB
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
// ==UserScript==
// @name Самиздат (samlib.ru) как надо
// @namespace Azazar's Scripts
// @description Убирает рекламу, позволяет читать скрытые комментарии, постить комментарии там, где они отключены, смотреть отключенные оценки.
// @match *://samlib.ru/*
// @version 1.03.1
// @grant none
// @license MIT
// @updateURL https://raw.githubusercontent.com/azazar/userscripts/main/samlib-optimized.user.js
// @downloadURL https://raw.githubusercontent.com/azazar/userscripts/main/samlib-optimized.user.js
// @license MIT
// ==/UserScript==
(function() {
var _$;
var A$;
function _(s) {
A$ = document.querySelectorAll(s);
_$ = A$.length > 0 ? A$[0] : null;
return _$;
}
function findElementByText(s, text, partialMatch) {
if (_(s) === null) {
return null;
}
text = text.trim();
for(var i = 0; i < A$.length; i++) {
if (partialMatch) {
if (A$[i].innerText.indexOf(text) !== -1) {
return A$[i];
}
}
else {
if (text === A$[i].innerText.trim()) {
return A$[i];
}
}
}
return null;
}
function C$(tagName, attrs, parent, children) {
var e = document.createElement(tagName);
if (attrs === undefined) {
}
else if (attrs !== null && typeof attrs === "object") {
for(var n in attrs) {
e.setAttribute(n, attrs[n]);
}
}
else {
console.warn("Bad attrs parameter:", attrs);
}
if (children === undefined) {
}
else if (children !== null) {
if (typeof children === "object") {
if (children.constructor.name === "Array") {
children.forEach(function(c) {
e.appendChild(c)
});
}
else {
e.appendChild(children);
}
}
else if (typeof children === "string") {
e.appendChild(document.createTextNode(children));
}
else {
console.warn("Bad children parameter:", children);
}
}
if (parent !== null) {
parent.appendChild(e);
}
return e;
}
function removeNode() {
var node = arguments.length === 0 ? _$ : arguments[0];
if (node === null || node.parentNode === null) {
return;
}
node.parentNode.removeChild(node);
}
// Реклама всё равно сделана бестолково, и вряд ли приносит прибыль.
// Лучше бы пожертвования собирали.
if (_("TD[BGCOLOR='#fbfafa']") !== null) {
console.log("Реклама боковая:", _$);
removeNode();
}
else if (_("TABLE[STYLE='border: solid #8e5373 2px; margin-bottom: 3px;']")) {
console.log("Реклама в шапке:", _$);
removeNode();
}
var section = null;
var authorLetter = null;
var authorId = null;
var bookId = null;
var authorUri = null;
var bookUri = null;
{
var m;
if ((m = /^\/([A-Za-z0-9]{1,2})\/([A-Za-z0-9_-]+)\/(?:([A-Za-z0-9_-]+)\.shtml)?(?:$|\?)/.exec(location.pathname)) !== null) {
authorLetter = m[1];
authorId = m[2];
bookId = m[3];
section = bookId ? "book" : "author";
console.log("Совпал шаблон пути:", section, authorLetter, authorId, bookId);
}
else if ((m = /^\/(comment)\/([A-Za-z0-9-]{1,2})\/([A-Za-z0-9_-]+)\/([A-Za-z0-9_-]+)?(?:$|\?)/.exec(location.pathname)) !== null) {
section = m[1];
authorLetter = m[2];
authorId = m[3];
bookId = m[4];
console.log("Совпал шаблон пути:", section, authorLetter, authorId, bookId);
}
else {
console.log("Не совпал шаблон пути:", location.pathname);
}
if (authorId && authorLetter) {
authorUri = "/" + authorLetter + "/" + authorId;
}
if (authorUri && bookId) {
bookUri = authorUri + "/" + bookId;
}
}
if ("book" === section) {
if (_("FORM[ACTION='/cgi-bin/votecounter']") !== null) {
if (_$.innerText === "") {
console.log("Оценки отключены");
_$.innerHTML = _$.innerHTML.replace(/<!--[\r\n\t ]+1/, "").replace(/1[\r\n\t ]+-->/, "");
console.log("Но мы это исправили");
}
else if (/<!--[\t\r\n ]+1[\t\r\n ]+Оценка:/) {
console.log("Отображение оценок отключено");
_$.innerHTML = _$.innerHTML.replace(/<!--[\r\n\t ]+1/, "").replace(/1[\r\n\t ]+-->/, "");
console.log("Но мы это исправили");
}
}
if (_("UL>LI>A[HREF]") !== null) {
var list = null;
var addComments = true;
for(var i = 0; i < A$.length; i++) {
if (list === null) {
list = A$[i].parentNode.parentNode;
}
var href = A$[i].getAttribute("HREF");
if (/^\/cgi-bin\/comment\?/.test(href)) {
addComments = false;
break;
}
else if (/^\/comment\//.test(href)) {
addComments = false;
break;
}
}
console.log("Проверка комментов:", addComments, list);
if (addComments) {
console.log("Добавляем линки на комменты");
C$("A", {"HREF": "/comment" + bookUri}, C$("LI", undefined, list), "Комментарии (отключены)");
C$("A", {"HREF": "/cgi-bin/comment?COMMENT=" + bookUri}, C$("LI", undefined, list), "Оставить комментарий (хоть и отключены они)");
}
console.log("Перенос текста");
var b = document.body;
var s = null;
var container = null;
var lastComment = null;
for(var i = 0; i < b.childNodes.length; i++) {
var n = b.childNodes[i];
//console.log(n);
if (s === null) {
var startingComment = (n.nodeType === Node.COMMENT_NODE && n.textContent === "--------- Собственно произведение -------------");
if (startingComment) {
console.log("Стартовый коммент найден:", n);
container = document.createElement("DIV");
n.parentNode.insertBefore(container, b.childNodes[i + 1]);
i++;
s = "text";
}
continue;
}
if (s === "text") {
console.log(n);
if (n.nodeType === Node.COMMENT_NODE) {
lastComment = n;
}
var endingComment = (lastComment !== null && lastComment.nodeType === Node.COMMENT_NODE && lastComment.textContent === "-----------------------------------------------");
if (endingComment && n.tagName === "HR") {
console.log("Конец произведения найден:", n);
s = "end";
break;
}
n.parentNode.removeChild(n);
container.appendChild(n);
i--;
continue;
}
}
if (container) {
var style = "font-size: 2em; line-height: 1em";
function applyStyles() {
var sw = window.innerWidth - 800;
if (sw <= 0) {
sw = 0;
}
var hsw = sw / 2;
container.setAttribute("style", style + '; padding: 0 ' + hsw + 'px 0 ' + hsw + 'px');
}
window.addEventListener("resize", function () {
applyStyles();
});
applyStyles();
}
}
// Add download attribute for FB2 anchor
let authorTitleElement = _('center h2');
let fb2AnchorElement = _('small ul li a[href$=".fb2.zip"]');
if (authorTitleElement && fb2AnchorElement) {
let authorTitle = authorTitleElement.innerText.split("\n");
let author = authorTitle[0].trim();
let title = authorTitle[1].trim();
let originalFileExtension = 'fb2.zip';
fb2AnchorElement.setAttribute("download", `${title}. ${author}.${originalFileExtension}`);
}
}
if ("comment" === section) {
var addCommentElement = findElementByText("A", "Добавить комментарий");
if (addCommentElement === null) {
var addLinkBefore = findElementByText("BODY>CENTER>SMALL", "Отсортировано по:", true);
console.log("Комменты отключены, но линк мы добавим перед этим:", addLinkBefore);
if (addLinkBefore) {
addLinkBefore.parentNode.insertBefore(C$("A", {"HREF": "/cgi-bin/comment?COMMENT=" + bookUri}, null, C$("B", null, null, "Добавить комментарий(они отключены как-бы)")), addLinkBefore);
}
}
}
console.log("Userscript для Самиздата отработал");
})();