Greasy Fork

Greasy Fork is available in English.

F**k 三观

try to take over the world!

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

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         F**k 三观
// @namespace    http://tampermonkey.net/
// @version      0.4
// @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);
    const iptTypes = {
        Array: {
            type: 'text',
            processor: arr => arr.join(','),
            extractor: $node => $node.value.split(',').filter(x => !!x),
        },
        String: {
            type: 'text',
        },
        Boolean: {
            type: 'checkbox',
            extractor: $node => $node.checked,
        },
        Number: {
            type: 'number',
        },
    };
    const settingDef = {
        kws: {
            type: 'Array',
            label: '关键词(懒得做好看了,英文逗号隔开,别怪我没提醒你)',
            iptStyle: 'width: 300px;',
            default: ['三观'],
            getProcessor: kws => kws.filter(x => !!x),
            setProcessor: kws => kws.filter(x => !!x),
        },
        forward: {
            type: 'Boolean',
            label: '包括转发的原内容?',
            default: true,
        },
        blockThreshold: {
            type: 'Number',
            label: '提示拉黑的阈值',
            default: 3,
            getProcessor: x => x | 0,
            setProcessor: x => x | 0,
        },
    };
    const setting = makeSetting();
    renderSettingArea();
    process();

    function makeSetting() {
        let props = Object.keys(settingDef).reduce((p, c) => {
            let key = `fk-setting-${c}`;
            let { type = 'String', default: dft, getProcessor = x => x, setProcessor = x => x } = settingDef[c];
            return {
                ...p,
                [c]: {
                    get() {
                        let v = GM_getValue(key);
                        v = getType(v) === type ? v : dft;
                        return getProcessor(v);
                    },
                    set(v) {
                        v = setProcessor(v);
                        GM_setValue(key, v);
                    }
                }
            };
        }, {});
        return Object.defineProperties({}, props);
    }

    function renderSettingFields() {
        return Object.keys(settingDef).map(k => {
            let { type = 'String', label = k, iptStyle = '' } = settingDef[k];
            let { type: iptType = 'text', processor = x => x } = iptTypes[type];
            let value = setting[k];
            return `<p><label for="fk-${k}">${label}:</label><input type="${iptType}" style="${iptStyle}" id="fk-${k}" name="fk-${k}" value="${processor(value)}" ${iptType === 'checkbox' ? (value ? 'checked' : '') : ''}/></p>`;
        }).join('');
    }

    function setSetting($form) {
        Object.keys(settingDef).forEach(k => {
            let { type = 'String', label = k } = settingDef[k];
            let { extractor = $node => $node.value } = iptTypes[type];
            let $ipt = $form.querySelector(`[name=fk-${k}]`);
            let value = extractor($ipt);
            setting[k] = value;
        });
    }

    function renderSettingArea() {
        let $w = document.querySelector('.aside');
        if ($w === null) {
            return;
        }
        let $div = document.createElement('div');
        $div.style.padding = '8px 0';
        let $a = document.createElement('a');
        $div.append($a);
        $a.innerText = 'F**k 设置';
        $a.href = 'javascript:;';
        $a.style.color = '#ccc';
        $a.addEventListener('click', function() {
            let $area = $div.querySelector('.fk-setting');
            if ($area !== null) {
                $area.remove();
                return;
            }
            $area = document.createElement('div');
            $area.classList.add('fk-setting');
            $area.style.padding = '8px 0';
            let $form = document.createElement('form');
            $form.innerHTML = `${renderSettingFields()}<p><input type="submit" value="保存"/></p>`;
            $area.appendChild($form);
            $form.addEventListener('submit', function(e) {
                e.preventDefault();
                setSetting($form);
                alert('保存成功!');
            }, true);
            $div.append($area);
        }, true);
        $w.prepend($div);
    }

    function getType(obj) {
        return Object.prototype.toString.call(obj).slice(8, -1);
    }

    function process() {
        let $statuses = document.querySelectorAll('.status-wrapper, .item-status');
        $statuses.forEach($x => {
            if ($x.dataset.fk) {
                return;
            }
            let kws = search($x);
            $x.dataset.fk = true;
            if (kws.size === 0) {
                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}的广播包含你设置的${Array.from(kws).map(x => `「${x}」`).join('、')},已折叠`;
            $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 = `imangry-${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 >= setting.blockThreshold) {
                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 kws = new Set();

        if (setting.kws.length === 0) {
            console.log('您没有设置关键词!!!');
            return kws;
        }
        let pattern = new RegExp(setting.kws.join('|'), 'g');
        for (let $node of $parent.childNodes) {
            if ($node.nodeType !== 3) {
                if ($node.nodeType === 1 && !setting.forward && $node.classList.contains('status-real-wrapper')) {
                    continue;
                }
                for (let kw of search($node)) {
                    kws.add(kw);
                }
                continue;
            }
            let text = $node.textContent;
            let lastIdx = 0;
            text.replace(pattern, (c, idx, t) => {
                kws.add(c);
                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 kws;
    }
})();