oldFUP

ignore, old-style citation, remove new features

Від 25.06.2016. Дивіться остання версія.

// ==UserScript==
// @name        oldFUP
// @namespace   http://forum.pravda.com.ua
// @version     0.4
// @description ignore, old-style citation, remove new features
// @match       https://forum.pravda.com.ua/index.php?board*
// @match       https://forum.pravda.com.ua/index.php?topic*
// @grant       GM_getValue
// @grant       GM_setValue
// @copyright   2014+, forum.pravda.com.ua
// @author		тарадайка
// ==/UserScript==

(function ignore(dialog) {

    var ignored,

        load = function () {
            ignored = [];
            var str = GM_getValue('FUP_IGNORED', '');
            if (str !== '')
                try {
                    ignored = JSON.parse(str);
                } catch (error) {
                    console.error("Ігнор список пошкоджено: ", error);
                }
        },

        save = function () {
            GM_setValue('FUP_IGNORED', JSON.stringify(ignored));
        },

        getUserIndex = function (name) {
            for (var i = 0; i < ignored.length; i++)
                if (ignored[i].name == name)
                    return i;
            return -1;
        },

        createLink = function (id, innerHTML, title, target) {
            var result = document.createElement('a');
            result.style.cursor = 'pointer';
            result.id = id;
            result.innerHTML = innerHTML;
            result.title = title;
            if (typeof target == 'string')
                result.href = target;
            else
                result.addEventListener('click', target);
            return result;
        },

        updateDialog = function () {
            if (dialog.style.display != 'inline')
                return;
            while (dialog.firstChild)
                dialog.removeChild(dialog.firstChild);
            load();
            if (ignored.length === 0)
                dialog.appendChild(document.createElement('div')).innerHTML = '(пусто)';
            else
                for (i = 0; i < ignored.length; i++) {
                    var userDiv = document.createElement('div');
                    var userLink = createLink(null, ignored[i].name, 'Перегляд профілю ' + ignored[i].name, ignored[i].profile);
                    userDiv.appendChild(userLink);
                    userDiv.appendChild(createToggleButton(userLink, false));
                    dialog.appendChild(userDiv);
                }
            var link = document.getElementById('ignored-link');
			var linkRect = link.getBoundingClientRect();
        	dialog.style.left = linkRect.right - 158 + 'px';
    	    dialog.style.top = linkRect.bottom + 'px';
        },

        createToggleButton = function (userLink, toIgnore) {
            var result = createLink(null, 'x', toIgnore ? 'Ігнорувати ' : 'Не ігнорувати ' + userLink.innerHTML,
                function () {
                    load();
                    var ignoredIndex = getUserIndex(userLink.innerHTML);
                    if (toIgnore == (ignoredIndex == -1)) {
                        if (toIgnore)
                            ignored.push({profile: userLink.href, name: userLink.innerHTML});
                        else
                            ignored.splice(ignoredIndex, 1);
                        save();
                        ignore(dialog);
                        updateDialog();
                    }
                }
            );
            result.style.fontWeight = 'normal';
            result.style.paddingLeft = toIgnore ? '5px' : '9px';
            return result;
        };

    load();
    if (document.location.href.indexOf('board') !== -1) {
        var i, topicRows = document.getElementsByClassName('list')[0].rows;
        for (i = 1; i < topicRows.length; i++) {
            var autherLink = topicRows[i].cells[3].getElementsByTagName('a')[0];
            if (dialog == null) {
                topicRows[i].cells[3].appendChild(createToggleButton(autherLink, true));
                var lastWriterLink = topicRows[i].cells[4].getElementsByTagName('a')[1];
                topicRows[i].cells[4].appendChild(createToggleButton(lastWriterLink, true));
            }
            topicRows[i].style.display = getUserIndex(autherLink.innerHTML) == -1 ? 'table-row' : 'none';
        }
    } else {
        var postDivs = document.getElementsByClassName('message');
        for (i = 0; i < postDivs.length; i++) {
            var userLink = postDivs[i].getElementsByTagName('a')[1];
            if(userLink == null)
                continue;
            if (dialog == null) {
                var userDiv = postDivs[i].getElementsByClassName('message-author icon-user')[0];
                userDiv.insertBefore(createToggleButton(userLink, true), userDiv.children[1]);
            }
            postDivs[i].style.display = getUserIndex(userLink.innerHTML) == -1 ? 'inline' : 'none';
        }
    }
    if (dialog == null) {
        dialog = document.createElement('div');
        dialog.id = 'dialog';
        var container = document.getElementById('user-info');
        container.innerHTML += " » ";
        var link = createLink('ignored-link', 'Ігноровані', 'Показати/сховати',
            function () {
                dialog.style.display = dialog.style.display == 'none' ? 'inline' : 'none';
                updateDialog();
            }
        );
        container.appendChild(link);
        dialog.style.display = 'none';
        dialog.style.position = 'absolute';
        dialog.style.textAlign = 'left';
        dialog.style.width = '150px';
        dialog.style.padding = '5px';
        dialog.style.border = '1px solid black';
        dialog.style.backgroundColor = 'white';
        container.appendChild(dialog);
    }
})(null);

var postDivs = document.getElementsByClassName('message');

var cite = function(userLink, textDiv) {

    return function() {
        
        var selection = window.getSelection();
        var lines = (selection.type == 'Range' && selection.anchorNode.parentNode == textDiv)
        	? selection.toString().split('\n')
        	: textDiv.innerText.split('\n');
        
        var text = '> ' + userLink.innerText + ' написав:\n';

        for(i = 0; i < lines.length; i++)
            if (lines[i] != '')
        		text += '> ' + lines[i] + '\n';

		document.getElementById('body').innerHTML += text;
	}
};

for (i = 0; i < postDivs.length; i++) {
    var userLink = postDivs[i].getElementsByTagName('a')[1];
    if(userLink == null)
        continue;
    var messageDiv = postDivs[i].getElementsByClassName('message-body')[0];
    var textDiv = messageDiv.getElementsByClassName('msg')[0];
    var buttonsHolder = messageDiv.getElementsByClassName('message-options')[0].childNodes[1].childNodes[1].childNodes[0].childNodes[1];
    messageDiv.getElementsByClassName('message-options')[0].childNodes[1].childNodes[1].childNodes[0].removeChild(messageDiv.getElementsByClassName('message-options')[0].childNodes[1].childNodes[1].childNodes[0].childNodes[2]);

    var citeButton = document.createElement('a');
    citeButton.style.cursor = 'pointer';    
	citeButton.innerHTML = '>>>';
    citeButton.title = 'цитувати ' + userLink.innerText;
    citeButton.addEventListener('mousedown', cite(userLink, textDiv));
    
    var scrollDownButton = document.createElement('a');
    scrollDownButton.style.cursor = 'pointer';    
	scrollDownButton.innerHTML = 'VVV';
    scrollDownButton.title = 'до редагування';
    scrollDownButton.addEventListener('mousedown', function() { document.getElementById('body').scrollIntoView(true); });
        
    scrollDownButton.style.paddingRight = '10px';
    citeButton.style.paddingRight = '10px';

    buttonsHolder.insertBefore(scrollDownButton, buttonsHolder.children[0]);
    buttonsHolder.insertBefore(citeButton, buttonsHolder.children[0]);
    //buttonsHolder.removeChild(buttonsHolder.children[2]);
    //buttonsHolder.removeChild(buttonsHolder.children[2]);
    
}