Skip to content

Instantly share code, notes, and snippets.

@Goheeca
Last active August 29, 2015 13:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Goheeca/8921690 to your computer and use it in GitHub Desktop.
Save Goheeca/8921690 to your computer and use it in GitHub Desktop.
// ==UserScript==
// @name Toggler based on nicknames
// @namespace GHC
// @version 0.2
// @description to get rid of bullshits and tldrs
// @match http://danyk.cz/kniha/kniha.php*
// @require http://code.jquery.com/jquery-2.1.0.min.js
// @require http://cdnjs.cloudflare.com/ajax/libs/jquery-cookie/1.4.0/jquery.cookie.js
// @copyright 2014+, Goheeca
// ==/UserScript==
var nicks = {};
var comments = '.odpov .jmeno_o';
var commentsContent = '.text_o';
var threads = '.jmeno';
var threadsContent = '.text';
var threadsComments = '.odpov';
var maskLabel = ['#','*'];
var togLabel = ['+','-'];
function getNick(s) {
var matches = s.match(/((\S+)(?=:)|(\S+))/);
return matches[1];
};
var init;
function tog () {
var name = $(this);
var content;
if (name.hasClass(threads.slice(1))) {
content = name.nextUntil(name.next('hr'),threadsComments).add(name.next(threadsContent));
} else {
content = name.siblings(commentsContent);
}
content.hide();
name.append(
$('<a>',
{
class: 'toggler',
href: 'javascript:void(0)',
text: togLabel[0],
click: function() {
var toggler = name.children('.toggler');
if(toggler.text() == togLabel[0]) {
toggler.text(togLabel[1]);
content.show();
} else {
toggler.text(togLabel[0]);
content.hide();
}
}}));
};
function reinit(anchor) {
var beforePosition = anchor.offset().top - $(window).scrollTop();
$(comments).add(threads).each(function () {
$(this).children('.masker').remove();
$(this).children('.toggler').click().remove();
});
$.cookie('nicks', JSON.stringify(nicks));
init();
var afterPosition = anchor.offset().top - $(window).scrollTop();
$(window).scrollTop($(window).scrollTop()+afterPosition-beforePosition);
};
function mask() {
var name = $(this);
name.append(
$('<a>',
{
class: 'masker',
href: 'javascript:void(0)',
text: nicks[getNick(name.text())] ? maskLabel[1] : maskLabel[0],
click: function() {
var masker = name.children('.masker');
if(masker.text() == maskLabel[0]) {
masker.text(maskLabel[1]);
nicks[getNick(name.text())] = true;
reinit(name);
} else {
masker.text(maskLabel[0]);
nicks[getNick(name.text())] = false;
reinit(name);
}
}}));
};
init = function() {
if(typeof $.cookie('nicks') == 'undefined') $.cookie('nicks', JSON.stringify(nicks));
nicks = JSON.parse($.cookie('nicks'));
$(comments).add(threads).each(mask);
$(comments).add(threads).filter(function () {
var this_nick = getNick($(this).text());
var ret = false;
$.each(nicks, function(nick, mask) {
if(mask && nick === this_nick) ret = true;
});
return ret;
}).each(tog);
};
$(init);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment