Gitter Done for FreeCodeCamp

Fix annoyances of of gitter.im FreeCodeCamp channels

目前為 2016-04-16 提交的版本,檢視 最新版本

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

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

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 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); });
    }

})();