Kbin Federation Awareness

Color posts and comments based on moderation rules of the origin server

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         Kbin Federation Awareness
// @namespace    http://tampermonkey.net/
// @version      0.2
// @description  Color posts and comments based on moderation rules of the origin server
// @author       CodingAndCoffee
// @match        https://*/*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=kbin.social
// @grant        none
// @license      MIT
// ==/UserScript==

(function() {
    'use strict';

    var isKbin = (typeof(window.KBIN_POST_ID) === 'string');

    const hasStrictModerationRules = [
        'beehaw.org'
    ];

    function isStrictlyModerated(hostname) {
        return hasStrictModerationRules.indexOf(hostname) !== -1;
    }

    //special thanks to StackOverflow - the one true source of all code, amen.
    function GM_addStyle(css) {
        const style = document.getElementById("GM_addStyleBy8626") || (function() {
            const style = document.createElement('style');
            style.type = 'text/css';
            style.id = "GM_addStyleBy8626";
            document.head.appendChild(style);
            return style;
        })();
        const sheet = style.sheet;
        sheet.insertRule(css, (sheet.rules || sheet.cssRules || []).length);
    }

    if (isKbin) {
        GM_addStyle('[data-is-strictly-moderated="true"] { background-color: rgba(55, 20, 20, 1.0); }');
        GM_addStyle('[data-is-federated-content="true"] { background-color: rgba(20, 45, 20, 1.0); }');

        document.querySelectorAll('#content article.entry').forEach(function(article) {
            var hostname = new URL(article.querySelector('footer menu .dropdown li:nth-child(4) a').href).hostname;
            article.setAttribute('data-hostname', hostname);

            if (isStrictlyModerated(hostname)) {
                article.setAttribute('data-is-strictly-moderated', true);
            } else if (hostname !== window.location.hostname) {
                article.setAttribute('data-is-federated-content', true);
            }
        });

        document.querySelectorAll('.comments blockquote.entry-comment').forEach(function(comment) {
            var userInfo = comment.querySelector('header a:nth-child(1)');
            if (userInfo) {
                var userHostname = userInfo.title.split('@').reverse()[0];

                if (isStrictlyModerated(userHostname)) {
                    comment.setAttribute('data-is-strictly-moderated', true);
                } else if (userHostname !== window.location.hostname) {
                    comment.setAttribute('data-is-federated-content', true);
                }
            }
        });
    }

})();