WykopSeen

Add Seen button to hide seen wykops

目前為 2015-01-04 提交的版本,檢視 最新版本

// ==UserScript==
// @name         WykopSeen
// @namespace    http://www.wykop.pl/
// @require      http://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js
// @version      0.4
// @description  Add Seen button to hide seen wykops
// @author       axem.pl
// @match        http://www.wykop.pl/
// @match        http://www.wykop.pl/link/*
// @match        http://www.wykop.pl/wykopalisko/*
// @grant        GM_getValue
// @grant        GM_setValue
// @grant        GM_deleteValue
// ==/UserScript==

var jq = this.$ = this.jQuery = jQuery.noConflict(true);
var seenWykopClass = 'seen';

function eachLi() {
    var self = jq(this);
    var parent = self.parent();
    var btn = jq('<button class=\'seen-btn\'></button>');
    var href = self.find('h2 a').attr('href');
    
    function getSeenBtnText() {
        if (self.hasClass(seenWykopClass)) {
            return 'UNSEE';
        } else {
            return 'SEEN';
        }
    }
    function btnOnClick() {
        self.toggleClass(seenWykopClass);
        btn.text(getSeenBtnText());
        if (self.hasClass(seenWykopClass)) {
            GM_setValue(href, 1);
            parent.append(self);
        } else {
            GM_deleteValue(href);
            parent.prepend(self);
        }
    }
    if (GM_getValue(href)) {
        self.addClass(seenWykopClass);
        parent.append(self);
    }
    
    btn.text(getSeenBtnText());
    btn.on("click", btnOnClick);
    self.children('div').append(btn);   
}
function cssRules() {
    var rules = [
        'li.seen { background: lightgray }',
        'li.seen * { color: gray !important }',
        'li.seen .diggbox { display: none }',
        'li.seen .media-content { display: none }',
        'li.seen .fix-tagline { display: none }',
        'li.seen .description { display: none }',
        'li.seen .elements { display: none }',
        'li.seen .article { min-height: 0 }',
        '.seen-btn { position: absolute; top: 5px; right: 5px; z-index: 9999 }'
    ];
    return rules.join('\n');
}
function seenFullBtn() {
    var btn = jq('<li><a class=\'affect\' href=\'#\'><i class=\'fa fa-star\'></i><span></span></a>');
    var href = document.location.toString().split('#');
    
    function refreshBtnText() {
        if (GM_getValue(href)) {
            btn.find('span').text('UNSEE');
        } else {
            btn.find('span').text('SEEN');
        }
    }
    
    btn.find('a').on('click', function () {
        if (btn.find('span').text() === 'SEEN') {
            GM_setValue(href, 1);
        } else {
            GM_deleteValue(href);
        }
		refreshBtnText();
        event.preventDefault();
        return false;
    });

    refreshBtnText();
    
    jq('.article.fullview .actions ul').append(btn);
}
function exec() {
    jq(document.body).append(jq('<style type=\'text/css\'></style>').html(cssRules()));
    jq('#itemsStream li').each(eachLi);
    seenFullBtn();
}
exec();