weaming / douban_group_hide_article

// ==UserScript==
// @name         douban_group_hide_article
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  豆瓣小组隐藏、高亮帖子
// @author       https://github.com/weaming
// @match        https://www.douban.com/group/*
// @grant        none
// @require      https://raw.githubusercontent.com/tsironis/lockr/master/lockr.js
// @gist         https://gist.github.com/weaming/1f26e18c570396f167412c73a3321078
// @license      MIT
// ==/UserScript==

(function() {
    'use strict';
    let p = console.log.bind(console);
    $('tr').append('<button class="wm-btn-shadow" style="padding: 4px;">Hide</button>');
    $('tr').append('<button class="wm-btn-highlight" style="padding: 4px;">Love</button>');

    function get_key(tr) {
        return $(tr).find('a').text().trim();
    }

    // do the init action func for key k
    var init = (k, func) => {
        p('init', k);
        $('tr').toArray().map(tr => {
            var text = get_key(tr);
            if (text === k) {
                func(tr);
            }
        });
    };

    const shadow_key = 'keys';
    const highlight_key = 'highlights';

    // action 1
    function shadow($x) {
        $x = $($x);
        $x.css('opacity', '0.1');
        var key = get_key($x);
        Lockr.sadd(shadow_key, key);
    }

    // action 2
    function highlight($x) {
        $x = $($x);
        $x.css('background', 'yellow');
        var key = get_key($x);
        Lockr.sadd(highlight_key, key);
    }

    Lockr.prefix = 'weaming';

    // init
    Lockr.smembers(shadow_key).map(
        k => init(k, shadow)
    );
    Lockr.smembers(highlight_key).map(
        k => init(k, highlight)
    );

    // bind click
    $('.wm-btn-shadow').click(e => {
        let t = $(e.target);
        var x = t.parent('tr');
        p(x);
        shadow(x);
    });

    $('.wm-btn-highlight').click(e => {
        let t = $(e.target);
        var x = t.parent('tr');
        p(x);
        highlight(x);
    });

})();