Greasy Fork

来自缓存

Greasy Fork is available in English.

Bangumi 屏蔽色情条目

使用前请登出bangumi.tv的账号,登录并使用bgm.tv的账号

当前为 2024-12-24 提交的版本,查看 最新版本

// ==UserScript==
// @name         Bangumi 屏蔽色情条目
// @version      0.4
// @description  使用前请登出bangumi.tv的账号,登录并使用bgm.tv的账号
// @author       sedoruee
// @match        *://bgm.tv/*
// @match        *://bangumi.tv/*
// @match        *://chii.in/*
// @grant        none
// @namespace http://greasyfork.icu/users/1383632
// ==/UserScript==

(function() {
    'use strict';

    const host = window.location.host;
    const isBgm = host === 'bgm.tv' || host === 'chii.in';

    function replaceLinks(targetHost) {
        const links = document.querySelectorAll('a[href]');
        for (const link of links) {
            // 排除 div.page_inner 元素
            if (link.closest('div.page_inner')) continue;

            const href = link.href;
            const regex = /^(https?:\/\/)(bgm\.tv|chii\.in|bangumi\.tv)(\/(subject|game|book|anime)(\/(\d+|(tag.*)?))?)/;
            if (regex.test(href)) {
                if (isBgm) {
                    link.href = href.replace(regex, `$1bangumi.tv$3`);
                } else {
                    link.href = href.replace(regex, `$1bgm.tv$3`);
                }
            }
        }
    }

    if (isBgm) {
        replaceLinks('bangumi.tv');
    } else {
        replaceLinks('bgm.tv');
    }

    // 监听 DOM 变化,处理动态加载的内容
    const observer = new MutationObserver(() => {
        if (isBgm) {
            replaceLinks('bangumi.tv');
        } else {
            replaceLinks('bgm.tv');
        }
    });
    observer.observe(document.body, { childList: true, subtree: true });
})();