User:Neiltyson1fan/UserInteractionBlock.js

From RationalWiki
Jump to navigation Jump to search

Note: After saving, you may have to bypass your browser's cache to see the changes.

  • Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
  • Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
  • Internet Explorer / Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5
  • Opera: Go to Menu → Settings (Opera → Preferences on a Mac) and then to Privacy & security → Clear browsing data → Cached images and files.
//DECLARE THE USERS YOU WANT BLOCKED IN THIS LIST HERE
var users = ['☭Comrade GC☭'];

//OPTIONS FOR BLOCKING ARE HERE, SWITCH USESPIC TO FALSE IF YOU DONT WANT TO SEE CATS
hideByText(users, {
    'usespic': 'true',
    'sensitive': 'false',
    'pic': 'https://upload.wikimedia.org/wikipedia/commons/1/1b/Cute_Cat_Wearing_A_Tie.jpg'
});


function hideByText(text, opts) {
    if (!text) {
        return false;
    } else {
        var defaults = {
                // the element we look within, expects:
                // 1: node reference, eg the result of: document.getElementsByTagName('div')[0]
                // 2: an element's id, as a string, eg: 'test'
                'within': document.body,
                // case-sensitivity, as a string:
                // 'true' : is case sensitive, 'Some' will not match 'some',
                // 'false' : is case insensitive, 'Some' will match 'some'
                'sensitive': 'true',
                // 'true' : replaces text with pic in 'pic' field,
                // 'false' : just hides text and doesn't use pic
                'usespic': 'false',
                // 'true' : removes white-space from beginning, and end, of the text,
                // 'false' : does not remove white-space
                'trim': 'true',
                // the pic to use if usespic is true, hides text with pic
                'pic': 'https://upload.wikimedia.org/wikipedia/commons/1/1b/Cute_Cat_Wearing_A_Tie.jpg'
            },
            opts = opts || {};

        for (var setting in defaults) {
            if (defaults.hasOwnProperty(setting)) {
                opts[setting] = opts[setting] || defaults[setting];
            }
        }

        var within = opts.within.nodeType == 1 ? opts.within : document.getElementById(opts.within);
        var elems = within.querySelectorAll('p,li,dl');
        var flags = opts.sensitive == 'true' ? 'i' : '';
        for (c = 0; c < text.length; c++) {
            var needle = opts.trim == 'true' ? text[c].replace(/^(\s+) || (\s+)$/g, '') : text[c];
            var haystack;
            var reg = new RegExp(needle, flags);

            if (opts.usespic == 'true') {
                var count;
                var found = true;
                while (found) {
                    found = false;
                    var i = true;
                    //increase count until find something
                    for (count = 0; elems[count] != undefined && i; count++) {
                        if ((elems[count].textContent || elems[count].innerText).match(reg)) {
                            console.log(elems[count]);
                            elems[count].innerHTML = "<img height='150' width='150' src='" + opts.pic + "' />";
                            i = false;
                            found = true;
                        }
                    }
                }
            } else if (opts.usespic == 'false') {
                for (var i = 0, len = elems.length; i < len; i++) {
                    if ((elems[i].textContent || elems[i].innerText).match(reg)) {
                        elems[i].style.display = "none";
                    }
                }
            }
        }
    }
}