Greasy Fork

Greasy Fork is available in English.

F**k 三观

try to take over the world!

当前为 2018-07-18 提交的版本,查看 最新版本

// ==UserScript==
// @name         F**k 三观
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  try to take over the world!
// @author       yetone
// @match        https://*.douban.com/*
// @grant        GM_setValue
// @grant        GM_getValue
// ==/UserScript==

(function() {
    'use strict';

    let $style = document.createElement('style');
    $style.innerText = `
.my-hl {
-webkit-animation: highlight 1.6s ease-out;
animation: highlight 1.6s ease-out
}
@-webkit-keyframes highlight {
0% {
background: #ebebeb
}

html[data-theme=dark] 0% {
background: #444
}

to {
background: transparent none repeat 0 0/auto auto padding-box border-box scroll;
background: initial
}
}

@keyframes highlight {
0% {
background: #ebebeb
}

html[data-theme=dark] 0% {
background: #444
}

to {
background: transparent none repeat 0 0/auto auto padding-box border-box scroll;
background: initial
}
}`;
    document.head.appendChild($style);
    process();

    function process() {
        let $statuses = document.querySelectorAll('.status-wrapper, .item-status');
        $statuses.forEach($x => {
            if ($x.dataset.fk) {
                return;
            }
            let hide = search($x);
            $x.dataset.fk = true;
            if (!hide) {
                return;
            }
            let info = getStatusInfo($x);
            let $div = document.createElement('div');
            $div.style.color = '#bfbfbf';
            $div.style.fontSize = '12px';
            $div.style.textAlign = 'center';
            $div.style.borderBottom = '1px solid #e5e5e5';
            $div.style.padding = '8px 0';
            let $tip = document.createElement('div');
            $tip.innerText = `${info.user.name}的广播包含「三观」,已折叠`;
            $tip.style.cursor = 'pointer';
            $tip.style.display = 'inline-block';
            $div.addEventListener('click', function() {
                $div.style.display = 'none';
                $x.style.display = 'block';
                $x.classList.add('my-hl');
            }, false);
            $div.appendChild($tip);
            let key = `hehe1-${info.user.id}`;
            let sids = GM_getValue(key) || [];
            if (sids.indexOf(info.id) === -1) {
                sids.push(info.id);
            }
            GM_setValue(key, sids);
            if (sids.length >= 3) {
                let $dd = document.createElement('div');
                $dd.style.display = 'inline-block';
                $dd.style.marginLeft = '8px';
                let $span = document.createElement('span');
                $span.innerText = `已发布${sids.length}次`;
                $dd.appendChild($span);
                let $btn = document.createElement('a');
                $btn.href = 'javasript:;';
                $btn.innerText = '拉黑';
                $btn.style.marginLeft = '8px';
                $btn.addEventListener('click', function(e) {
                    e.stopPropagation();
                    if (!confirm(`确定拉黑${info.user.name}?`)) {
                        return;
                    }
                    $.postJSON_withck('https://www.douban.com/j/contact/addtoblacklist', {
                        people: info.user.id
                    }, function() {
                        alert(`已拉黑${info.user.name}`);
                    });
                }, false);
                $dd.appendChild($btn);
                $div.appendChild($dd);
            }
            insertAfter($div, $x);
            $x.style.display = 'none';
        });
    }

    function getUserInfo($node) {
        let $pic = $node.querySelector('.usr-pic img') || $node.querySelector('img.avatar');
        let id = $pic === null ? '' : $pic.src.match(/u(\d+)-\d+/)[1];
        let $lnk = $node.querySelector('.lnk-people') || $node.querySelector('a.author');
        let name = $lnk === null ? 'unknow' : $lnk.innerText;
        return {id, name};
    }
    function getStatusInfo($node) {
        let id = $node.dataset.sid;
        let $text = $node.querySelector('.status-saying') || $node.querySelector('.status-preview');
        let text = $text === null ? '' : $text.innerText;
        let user = getUserInfo($node);
        return { id, text, user };
    }
    function insertAfter($new, $target) {
        let $p = $target.parentNode;

        if($p.lastChild === $target) {
            $p.appendChild($new);
        } else {
            $p.insertBefore($new, $target.nextSibling);
        }
    }
    function search($parent) {
        let nodes = [];
        let need = false;
        let hide = false;
        for (let $node of $parent.childNodes) {
            if ($node.nodeType !== 3) {
                hide = hide || search($node);
                continue;
            }
            let text = $node.textContent;
            let lastIdx = 0;
            text.replace(/三观/g, (c, idx, t) => {
                hide = true;
                need = true;
                nodes.push(new Text(t.substring(lastIdx, idx)));
                lastIdx = idx + c.length;
                let $b = document.createElement('b');
                $b.style.background = '#ffb56e';
                $b.style.fontWeight = 'normal';
                $b.innerText = c;
                nodes.push($b);
            });
            nodes.push(new Text(text.substring(lastIdx)));
        }
        if (need) {
            while ($parent.childNodes.length > 0) {
                $parent.childNodes.forEach($x => {
                    $parent.removeChild($x);
                });
            }
            nodes.forEach($x => {
                $parent.appendChild($x);
            });
        }
        return hide;
    }
})();