NGA楼主标识

简单的对NGA(nga.cn)楼主进行一个标注

目前為 2022-04-20 提交的版本,檢視 最新版本

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         NGA楼主标识
// @namespace    http://nickdoth.cc/
// @version      0.1.7
// @description  简单的对NGA(nga.cn)楼主进行一个标注
// @compatible   chrome
// @compatible   firefox
// @compatible   safari
// @license      MIT
// @author       nickdoth
// @match        http://bbs.nga.cn/*
// @match        https://bbs.nga.cn/*
// @match        http://nga.178.com/*
// @match        https://nga.178.com/*
// @grant        window
// ==/UserScript==

// MIT License
//
// Copyright (c) 2022 nickdoth
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

// @ts-check


(
/**
 * @param {Window & { __COLOR: { [k: string]: string } }} window
 */
async function(window) {
    'use strict';

    const sleep = (ms) => new Promise(resolve => setTimeout(resolve, ms));

    // Query Theme Colors
    const ngaDark = window.__COLOR.uitxt1 ?? '';
    const ngaLight = window.__COLOR.border2 ?? ''; // bg1

    // Sheet
    /** @type {*} */
    let styleEl = undefined;


    const tidCb = async (tid, sheet) => {
        const firstPageHtml = await (fetch(`/read.php?tid=${tid}`, { credentials: 'include' }).then(res => res.text()));
        const firstPageDoc = (new DOMParser).parseFromString(firstPageHtml, 'text/html');

        const qUid = '#postauthor0';
        /** @type {HTMLAnchorElement | null} */
        const elUid = firstPageDoc.querySelector(qUid);
        const uid = /uid=(\d+)/.exec(elUid?.href ?? '')?.[1];

        console.debug('楼主', uid);

        sheet.insertRule(`a.b[href*="uid=${uid}"]:after {
            content: "楼主";
            background: ${ngaDark};
            color: #fff;
            border: 1px transparent solid;
            border-radius: 5px;
            padding: 2px 4px;
            margin-left: 1px;
            margin-right: 1px;
            font-size: 12px;
        }`, 0);

        sheet.insertRule(`a.b[href*="uid=${uid}"] {
            background: ${ngaLight};
            padding: 2.7px 0px;
            border-radius: 5px;
            color: #000;
        }`, 1);
    };

    let currTid;
    while (true) {
        await sleep(160);

        const tid = /tid=(\d+)/.exec(location.search ?? '')?.[1];
        if (tid === currTid) continue;

        currTid = tid;

        // cleanup
        if (styleEl && window.document.head) {
            console.debug('cleanup style');
            styleEl.remove();
        }

        if (!currTid) continue;

        styleEl = window.document.createElement('style');
        window.document.head.appendChild(styleEl);
        /** @type {CSSStyleSheet} */
        let sheet = styleEl.sheet;

        await tidCb(currTid, sheet);
    }

})(unsafeWindow);