Gitter Done for FreeCodeCamp

Fix annoyances of of gitter.im FreeCodeCamp channels

当前为 2016-04-16 提交的版本,查看 最新版本

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey,才能安装此脚本。

您需要先安装用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         Gitter Done for FreeCodeCamp
// @namespace    http://aimless.info/
// @version      0.2
// @description  Fix annoyances of of gitter.im FreeCodeCamp channels
// @author       Chuck Adams
// @match        https://gitter.im/FreeCodeCamp/*
// @grant        unsafeWindow
// @require      http://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js
// @require      https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3/underscore-min.js
// ==/UserScript==

(function() {
    'use strict';

    //// main

    var $ = unsafeWindow.jQuery;

    var STFU = ['@camperbot'];

    var targetNodes         = $("#chat-container");
    var MutationObserver    = window.MutationObserver || window.WebKitMutationObserver;
    var myObserver          = new MutationObserver(mutationObserved);
    var obsConfig           = { childList: true, characterData: false, attributes: false, subtree: true };

    targetNodes.each(function () {
        myObserver.observe (this, obsConfig);
    });

    //// end main

    function mutationObserved(recs) {
        _.each(recs, handleMutation);
    }

    function handleMutation(rec) {
        _.each(rec.addedNodes, nodeAdded);
    }
    
    function nodeAdded(node) {
        var msg = "nodeAdded:";
        var from = $('.chat-item__username', node);
        if (!from) return;

        var who = from.text();
        if (who === '') return;

        msg += ' from:' + who;
        if (_.contains(STFU, who)) {
            msg += ' (STFU)';
            gag(node);

        }
        // console.log(msg);

    }

    function gag(node) {
        // extreme, makes it hard to expand later
        // $(node).hide();

        // This at least hints something was said, plus a handle to expand later
        $('.chat-item__content', node).hide();
        $('.chat-item__actions', node).hide();
        var avatar = $('img.avatar__image', node);
        avatar.css('border', '5px double #fcf');
        avatar.click(function () { ungag(node); });
    }

    function ungag(node) {
        $('.chat-item__content', node).show();
        $('.chat-item__actions', node).show();
        var avatar = $('img.avatar__image', node);
        avatar.css('border', 'unset');
        avatar.click(function () { gag(node); });
    }

})();