贴吧显示真实ID

贴吧昵称掩盖了真实ID,认不出人了?这个脚本适合你

目前為 2017-07-10 提交的版本,檢視 最新版本

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

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

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

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

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

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

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         贴吧显示真实ID
// @version      0.9
// @namespace    https://github.com/8qwe24657913
// @description  贴吧昵称掩盖了真实ID,认不出人了?这个脚本适合你
// @author       8qwe24657913
// @match        http://tieba.baidu.com/*
// @match        https://tieba.baidu.com/*
// @run-at       document-start
// @grant        GM_getValue
// @grant        GM_setValue
// ==/UserScript==
(function() {
    'use strict';
    // add css
    var css = `
@keyframes showUserName {
    from {
        clip: rect(1px, auto, auto, auto);
    }
    to {
        clip: rect(0px, auto, auto, auto);
    }
}
.frs-author-name:not(.shownUN), .p_author_name:not(.shownUN), .userinfo_username:not(.shownUN), .lzl_cnt > .at, #j_u_username .u_ddl_con_top > ul:not(.shownUN) {
    animation-duration: 0.001s;
    animation-name: showUserName;
}
.card_userinfo_title .userinfo_username, .card_userinfo_title .userinfo_username:hover {
    max-width: 100%!important;
}
.userinfo_username + div[style]:not([class]):not([id]), img[src="//tb1.bdstatic.com/tb/cms/nickemoji/nickname_sign.png"] {
    display: none!important;
}
.frs-author-name > div[style*="color"], .userinfo_username > div[style*="color"] {
    color: inherit!important;
}
`;
    var style = document.createElement('style');
    style.appendChild(document.createTextNode(css));
    document.documentElement.appendChild(style);
    // add setting
    var setting = GM_getValue('showUNSetting', localStorage.showUNSetting || '${un} (${nickname})');

    function changeSetting() {
        var newSetting = prompt('${un}表示真实ID,${nickname}表示昵称,可使用html标签', setting);
        if (newSetting && newSetting !== setting) {
            GM_setValue('showUNSetting', newSetting);
            location.reload();
        }
    }

    function closestAttr(elem, attr) {
        while (elem && !elem.hasAttribute(attr)) elem = elem.parentElement;
        return elem ? elem.getAttribute(attr) : false;
    }
    // main
    document.addEventListener('animationstart', function(event) { // shouldn't use jQuery
        if (event.animationName !== 'showUserName') return;
        var target = event.target;
        target.classList.add('shownUN');
        if (target.nodeName === 'UL') { // 设置按钮
            target.insertAdjacentHTML('beforeend', '<li class="u_showUN"><a href="javascript:">显ID设置</a></li>');
            target.getElementsByClassName('u_showUN')[0].addEventListener('click', changeSetting, false);
            return;
        }
        var un, nickname, data = closestAttr(target, 'data-field');
        // 获取 un
        if (data) { // frs & pb & card
            un = JSON.parse(data.replace(/'/g, '"')).un; // 贴吧的畸形JSON用的是单引号,姑且先用replace凑合
        } else if (location.pathname.startsWith('/home/')) { // ihome
            un = target.nextElementSibling.getAttribute('data-username');
        } else if (target.href) { // unknown, trying to parse href
            console.warn('贴吧显示真实ID: 尝试解析未知元素', target);
            un = new URLSearchParams(target.href.split('?')[1]).get('un');
        } else { // can't find un
            return console.error('贴吧显示真实ID: 找不到真实ID', target);
        }
        // 获取 nickname
        if (target.classList.contains('frs-author-name')) { // frs 用户名可能被切掉,统一不用图片,保证格式美观
            nickname = closestAttr(target.parentElement, 'title').split(' ')[1];
            if (nickname === un) target.textContent = nickname; // 用户名尽量显示完整,不用图片
        } else { // pb & card & ihome
            nickname = target.innerHTML.replace(/^<div[^>]*>(.*)<\/div>$/, '$1').replace(/<img src="\/\/tb1\.bdstatic\.com\/tb\/cms\/nickemoji\/nickname_sign\.png"[^>]*>/, '');
        }
        // 修改显示内容
        if (nickname !== un) target.innerHTML = setting.replace(/\${un}/g, un).replace(/\${nickname}/g, nickname);
    }, false);
})();