NGA_parse_uid

在未登录/实名NGA时解析uid为用户名

目前為 2018-06-15 提交的版本,檢視 最新版本

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         NGA_parse_uid
// @version      0.22
// @author       Inazuma
// @match        https://nga.178.com/*
// @match        https://bbs.ngacn.cc/*
// @match        https://bbs.nga.cn/*
// @match        http://nga.178.com/*
// @match        http://bbs.nga.cn/*
// @match        http://bbs.ngacn.cc/*
// @grant        none
// @namespace    https://greasyfork.org/users/163468
// @description  在未登录/实名NGA时解析uid为用户名
// ==/UserScript==

(() => {
    const pattern = 'nuke.php?func=ucp&uid=';
    const fetchedInfo = {};
    Object.values(document.querySelectorAll(`a[href*="${pattern}"]`)).map((v, i) => {
        const uid = v.href.split(pattern)[1];
        const setInfo = () => {
            v.innerText = fetchedInfo[uid].username;
            const avatarDiv = v.parentNode.parentNode;
            if (avatarDiv.className === 'posterinfo' && !(avatarDiv.childNodes[1].className === 'avatar')) {
                const img = document.createElement('img');
                img.style = 'max-width:280px;margin:5px auto;border-radius:50%';
                img.setAttribute('src', fetchedInfo[uid].avatar);
                avatarDiv.insertBefore(img, avatarDiv.childNodes[1]);
            }
        }

        if (fetchedInfo[uid]) {
            setInfo();
        } else {
            fetch(`/nuke.php?uid=${uid}&__output=12&__act=get&__lib=ucp`).then(res => res.json()).then(res => {
                //uid to username
                const username = res.result.username;

                //add avatar
                let avatar = res.result.avatar;
                if (avatar) {
                    if (avatar.indexOf("{") >= 0) {
                        avatar = JSON.parse(avatar);
                        avatar = avatar[`${parseInt(Math.random() * avatar.l)}`];
                    }
                    if (avatar.indexOf("http") < 0 || (avatar.indexOf("bbs") >= 0 && avatar.indexOf('ngabbs') < 0)) {
                        let hex = parseInt(avatar.match(/[0-9]+/)[0]).toString(16);
                        const file = avatar.match(/[0-9].*/)[0];
                        avatar = "http://img.ngacn.cc/avatars/2002/";

                        while (hex.length < 9) {
                            hex = `0${hex}`;
                        }
                        hex = hex.match(/.{1,3}/g);
                        for (let i = hex.length - 1; i >= 0; i--) {
                            avatar += `${hex[i]}/`;
                        }
                        avatar += file;
                    }

                }
                fetchedInfo[uid] = { avatar: avatar, username: username };
                setInfo();
            });
        }
    });

    Object.values(document.querySelectorAll('a[href="被禁止的链接"]')).map((v, i) => {
        let link;
        if (v.parentNode.className !== 'nobr') {
            link = v.previousElementSibling.firstChild.innerText.replace(/<[^>]+>/g, '');
            v.setAttribute('href', link);
            v.innerText = v.innerText.replace(/被禁止的链接/g, link);
        } else {
            link = v.parentNode.parentNode.firstChild.innerText.replace(/<[^>]+>/g, '');
            v.setAttribute('href', link);
        }
    });
})();