forked from 1024--/govnokod.ru-userscripts
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgquery.user.js
447 lines (366 loc) · 12.4 KB
/
gquery.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
// ==UserScript==
// @name gQuery
// @version 1.0.3
// @description initializes gQuery plugin
// @match *://govnokod.ru/*
// @match *://www.govnokod.ru/*
// @require https://code.jquery.com/jquery-1.4.min.js
// @grant none
// ==/UserScript==
/*
Run $.gk('help') to get help
*/
(function($) {
'use strict';
var VERSION = '1.0.3';
var PLUGIN_NAME = 'gk'; // $.fn._gk_, $._gk_
var SELECTOR_PREFIX = 'gk'; // $(...).filter(':_gk_')
var GETTER_PREFIX = '@'; // $.fn.gk.('_@_name')
var gk = {}, gkSelectors = {};
var gkHelp = [];
var TYPES = {
OPTION: 1,
GETTER: 2,
SELECTOR: 3,
};
function HelpEntry(type, name, help) {
this.type = type;
this.name = name;
this.help = help;
}
function helpEntryByName(x, y) {
return x.name < y.name ? -1 : x.name > y.name ? 1 : 0;
}
function fail(message) {
throw new Error(message);
}
function bind(xs, f) {
if(xs.length <= 1) return f(xs);
var r = [];
xs.each(function() {
f($(this)).each(function(){
r.push(this);
});
});
return $.merge($(), r);
}
function select($el, sel1, sel2) {
var r = $el.find(sel1);
if(r.length) return r;
return $el.find(sel2);
}
function selectFirst($el, sel1, sel2) {
if(!$el.length) return $el;
return select($el.first(), sel1, sel2);
}
function addOption(name, help, func) {
gk[name] = func;
gkHelp.push(new HelpEntry(TYPES.OPTION, name, help));
}
function addGetter(name, help, func) {
gk[GETTER_PREFIX + name] = func;
gkHelp.push(new HelpEntry(TYPES.GETTER, GETTER_PREFIX + name, help));
}
function addSelector(name, help, func) {
gkSelectors[SELECTOR_PREFIX + name] = func;
gkHelp.push(new HelpEntry(TYPES.SELECTOR, SELECTOR_PREFIX + name, help));
}
function addParametrizedSelector(name, help, func) {
gkSelectors[SELECTOR_PREFIX + name] = func;
gkHelp.push(new HelpEntry(TYPES.SELECTOR, SELECTOR_PREFIX + name + '(...)', help));
}
// OPTIONS
addOption('', 'select all the posts in the elements/body', function($el) {
return $el.find('li.hentry');
});
addOption('comments', 'select all the comments in the elements/body', function($el) {
return $el.find('li.hcomment');
});
addOption('children', 'select children of the elements/body', function($el) {
return $el.children('ul').children('li.hcomment');
});
addOption('parent', 'select parent comments/posts of th comments (one per element)', function($el) {
return bind($el, function($el) {
var parent = $el.parent().closest('li.hcomment');
if(!parent.length) parent = $el.parent().closest('li.hentry');
return parent;
});
});
addOption('siblings', 'select all the siblings of the comments', function($el) {
return gk.children(gk.parent($el)).not($el);
});
addOption('post', 'select parent posts of the comments (one per comment)', function($el) {
return $el.closest('li.hentry');
});
addOption('posts', 'select all the posts in the elements/body', function($el) {
return $el.find('li.hentry');
});
addOption('uinfo', 'select user info of the comments', function($el) {
return bind($el, function($el) {
return select($el, 'p.author', 'p.entry-info');
});
});
addOption('user', 'select the authors links', function($el) {
return bind($el, function($el) {
return select($el,
'p.author>a:nth-child(2)',
'.entry-info:first>.entry-author>a');
});
});
addGetter('name', 'select the author name of the first element', function($el) {
return selectFirst($el,
'p.author>a:nth-child(2)',
'.entry-info:first>.entry-author>a').text();
});
addOption('ava', 'select the avatars info', function($el) {
return bind($el, function($el) {
return select($el,
'p.author img.avatar',
'.entry-info:first>img.avatar');
});
});
addGetter('ava', 'select the avatar link', function($el) {
return selectFirst($el,
'p.author img.avatar',
'.entry-info:first>img.avatar').attr('src');
});
addGetter('uid', 'select the author ID', function($el) {
var author = selectFirst($el,
'p.author>a:nth-child(2)',
'.entry-info:first>.entry-author>a');
var href = author.attr('href');
if(!href) return null;
var m = href.match(/\/user\/(\d+)$/);
return m && Number(m[1]);
});
addOption('link', 'select the authors links', function($el) {
return bind($el, function($el) {
return select($el,
'.entry-title',
'.entry-info:first>.comment-link');
});
});
addGetter('link', 'select the author link of the first item', function($el) {
return selectFirst($el,
'.entry-title',
'.entry-info:first>.comment-link').attr('href');
});
addGetter('id', 'select the author ID of the first item', function($el) {
var link = selectFirst($el,
'.entry-title',
'.entry-info:first>.comment-link');
var href = link.attr('href');
if(!href) return null;
var m = href.match(/(\d+)$/);
return m && Number(m[1]);
});
addOption('date', 'select the dates', function($el) {
return bind($el, function($el) {
return select($el,
'p.author>abbr',
'.entry-info:first>.published');
});
});
addGetter('date', 'select the date of the first item', function($el) {
var date = selectFirst($el,
'p.author>abbr',
'.entry-info:first>.published');
return new Date(date.attr('title'));
});
addOption('container', 'select the container elements', function($el) {
return bind($el, function($el) {
if($el.has('.entry-title').length) return $el;
return $el.find('.entry-comment-wrapper:first');
});
});
addOption('text', 'select the text container elements', function($el) {
return bind($el, function($el) {
return select($el,
'.entry-content code',
'.entry-comment:first');
});
});
addGetter('text', 'select the text of the first item', function($el) {
return selectFirst($el,
'.entry-content code',
'.entry-comment:first').text();
});
addOption('chapter', 'select the chapter links', function($el) {
return $el.find('a[rel=chapter]');
});
addGetter('chapter', 'select the chapter of the first item', function($el) {
return $el.first().find('a[rel=chapter]').text();
});
addGetter('chapterlink', 'select the chapter link of the first item', function($el) {
return $el.first().find('a[rel=chapter]').attr('href');
});
addOption('descr', 'select the description containers', function($el) {
return $el.find('.description');
});
addGetter('descr', 'select the description of the first item', function($el) {
return $el.first().find('.description').text();
});
addOption('votes', 'select the vote info containers', function($el) {
return bind($el, function($el) {
return select($el,
'.vote>strong',
'.entry-info:first>.comment-vote>strong');
});
});
addGetter('rating', 'select rating of the first item', function($el) {
var votes = selectFirst($el,
'.vote>strong',
'.entry-info:first>.comment-vote>strong');
return Number(votes.text().replace('−', '-'));
});
addGetter('votes', 'select [pluses, minuses, rating] of the first item', function($el) {
var votes = selectFirst($el,
'.vote>strong',
'.entry-info:first>.comment-vote>strong');
var title = votes.attr('title');
if(!title) return null;
var m = title.match(/(\d+) за и (\d+) против/);
return m && [Number(m[1]), Number(m[2]), Number(votes.text().replace('−', '-'))];
});
addOption('onbtn', 'select "vote on" buttons', function($el) {
return bind($el, function($el) {
return select($el,
'.vote>.vote-on',
'.entry-info:first>.comment-vote>.comment-vote-on');
});
});
addOption('againstbtn', 'select "vote against" buttons', function($el) {
return bind($el, function($el) {
return select($el,
'.vote>.vote-against',
'.entry-info:first>.comment-vote>.comment-vote-against');
});
});
addOption('answerbtn', 'select "answer" buttons', function($el) {
return bind($el, function($el) {
return select($el,
'.entry-comments>h3>a',
'.entry-comment-wrapper:first a.answer');
});
});
addGetter('answerlink', 'select "answer" link', function($el) {
return selectFirst($el,
'.entry-comments>h3>a',
'.entry-comment-wrapper:first a.answer').attr('href');
});
addOption('hide', 'hide the comments', function($el) {
$el.each(function() {
var $comment = $(this), text='показать всё, что скрыто';
// from http://userscripts.org/scripts/source/393166.user.js (version 3.2.0) by Vindicar
var $ec = $comment.find('.entry-comment:eq(0)');
var $lnk;
if (!$ec.hasClass('entry-comment-hidden')) {
$lnk = $('<span class="hidden-text"><a class="ajax" href="#">'+text+'</a></span>');
$ec
.addClass('entry-comment-hidden')
.find('.comment-text:eq(0)')
.before($lnk);
} else {
$lnk = $ec.find('.hidden-text:eq(0)').find('a.ajax');
$lnk.text(text);
}
});
return $el;
});
addOption('show', 'show the comments hidden', function($el) {
$el.each(function() {
var $comment = $(this);
// from http://userscripts.org/scripts/source/393166.user.js (version 3.2.0) by Vindicar
var $ec = $comment.find('.entry-comment:eq(0)');
if ($ec.hasClass('entry-comment-hidden'))
$ec
.removeClass('entry-comment-hidden')
.find('.hidden-text:eq(0)')
.remove();
});
return $el;
});
addGetter('version', 'get the plugin version', function($el) {
return VERSION;
});
addOption('help', 'show gQuery help', function($el) {
function showEntries(t) {
gkHelp
.filter(function(x){ return x.type == t; })
.sort(helpEntryByName)
.forEach(function(x) {
console.log(' ' + x.name +
' '.substr(0, 15 - x.name.length) +
' ' + x.help);
});
}
console.log('Use $(...).' + PLUGIN_NAME + '("sel1 > sel2:pseudoclass1 > :pseudoclass2 > sel3 > ...")');
console.log('where sel* for selecting element is')
showEntries(TYPES.OPTION);
console.log('or where sel* for getting value')
showEntries(TYPES.GETTER);
console.log('or where pseudoclass* for filter value');
console.log('(you may use the following pseudoclasses in jQuery selectors as well)');
showEntries(TYPES.SELECTOR);
console.log();
console.log('Example: $.' + PLUGIN_NAME + '("comments:' + SELECTOR_PREFIX + 'user(1024--) > parent > container").css("color", "red")');
return null;
});
// SELECTORS
addParametrizedSelector('user', 'if author\'s name is the value passed',
function(el, index, meta) {
return gk[GETTER_PREFIX + 'name']($(el)) == meta[3];
});
addParametrizedSelector('uid', 'if author\'s ID is the value passed',
function(el, index, meta) {
return gk[GETTER_PREFIX + 'uid']($(el)) == meta[3];
});
addParametrizedSelector('id', 'if ID of the item is the value passed',
function(el, index, meta) {
return gk[GETTER_PREFIX + 'id']($(el)) == meta[3];
});
addParametrizedSelector('better', 'if rating of the item is more than the value passed',
function(el, index, meta) {
return gk[GETTER_PREFIX + 'rating']($(el)) > meta[3];
});
addParametrizedSelector('worse', 'if rating of the item is less than the value passed',
function(el, index, meta) {
return gk[GETTER_PREFIX + 'rating']($(el)) < meta[3];
});
addSelector('commented', 'if the item has children', function(el) {
return gk.children($(el)).length > 0;
});
addSelector('good', 'if rating of the item is positive', function(el) {
return gk[GETTER_PREFIX + 'rating']($(el)) > 0;
});
addSelector('bad', 'if rating of the item is negative', function(el) {
return gk[GETTER_PREFIX + 'rating']($(el)) < 0;
});
addSelector('neutral', 'if rating of the item equals zero', function(el) {
return gk[GETTER_PREFIX + 'rating']($(el)) == 0;
});
addSelector('hidden', 'if the comment is hidden', function(el) {
return $(el).find('.entry-comment:first').hasClass('entry-comment-hidden');
});
addSelector('shown', 'if the comment is not hidden', function(el) {
return !($(el).find('.entry-comment:first').hasClass('entry-comment-hidden'));
});
function runGK(command) {
var obj = this.length ? this : $('body');
if(!command || !command.trim()) return obj.find('li.hentry');
command.split('>').forEach(function(command) {
var commands = command.split(':');
if(!commands.length) return;
var command = commands[0].trim();
if(command) {
if(!(command in gk)) fail('Invalid command: "' + command + '"');
obj = gk[command](obj);
}
for(var i=1; i<commands.length; ++i) obj = obj.filter(':' + commands[i]);
});
return obj;
}
$.fn[PLUGIN_NAME] = runGK;
$[PLUGIN_NAME] = runGK.bind($());
$.extend($.expr[':'], gkSelectors);
})(window.jQuery || window.$);