Pixiv-ForceSafeMode

While writing the last Pixiv user script, I realized that the official (Safe/R-18) filter didn't cover all the scenes. So I made this script to filter all bad information with one click.

You will need to install an extension such as Tampermonkey, Greasemonkey or Violentmonkey to install this script.

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

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

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

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

You will need to install a user script manager extension to install this script.

(I already have a user script manager, let me install it!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(I already have a user style manager, let me install it!)

// ==UserScript==
// @name                Pixiv-ForceSafeMode
// @name:zh-CN          Pixiv-强制和谐模式
// @name:ja             Pixiv-強制安全モード
// @namespace           https://github.com/TitanRGB
// @version             1.6
// @description         While writing the last Pixiv user script, I realized that the official (Safe/R-18) filter didn't cover all the scenes. So I made this script to filter all bad information with one click.
// @description:zh-CN   开发上一个 Pixiv 插件时,意识到官方的 (全年龄/R-18) 过滤器并不能覆盖所有场景,容易使我的身体吃不消。因此需要更强大的强制过滤插件来一键过滤所有不良信息。
// @description:ja      前回のPixivユーザースクリプトを作成している最中、公式の(セーフ/ R-18)フィルターがすべてのシーンをカバーしていないことに気づきました。 したがって、1つのクリックですべての不適切な情報をフィルタリングするより強力な強制フィルタリングスクリプトが必要です。
// @author              https://github.com/TitanRGB
// @icon                data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAYAAABzenr0AAAHVUlEQVRYheWXbXBUVxnHf+fsW7LJbrJ5IclCgABTYERaiG2hKTCdUam0M9qhiH5wOkO12urYCXSAsepQdTC0heL40lE6nao4Tqlgp63i1BZDQWgbgg0tYpCmQBpCmpiXhWw2+3If59x7N9ks6zB+8JNn5t65e/be8/zP/3me//Mc/u+HMgToZyysNPh80HKLYmYpjCQnqAm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p8U3sZq98oWoD9hqrlSGqmgJM/FSLGKHU3+JV/WZj37VaRemFTrs0JLU8NBrDhX2XOEjmdNBm54uiTfrz4JK6I4BlH0OnBghL5wagjGdoj6q93WP6HuUpgQLa6IH+k/GDVlJperCqROfa4j1ZsS079c/Uv1vB/Bvw2iXmm2L62oAAAAASUVORK5CYII=
// @match               http*://pixiv.net
// @match               http*://pixiv.net/*
// @match               http*://www.pixiv.net
// @match               http*://www.pixiv.net/*
// @license             MPL-2.0
// @license^            Mozilla Public License 2.0
// @grant               GM_setValue
// @grant               GM_getValue
// @grant               unsafeWindow
// @homepageURL         https://github.com/SynRGB/Pixiv-ForceSafeMode
// @contributionURL     https://github.com/SynRGB/Pixiv-ForceSafeMode
// @copyright           Copyright © 2022-PRESENT, TitanRGB (https://github.com/TitanRGB)
// @charset             UTF-8
// @run-at              document-end
// ==/UserScript==

if (GM_getValue('pixiv-quick-safemode') === undefined) {
    GM_setValue('pixiv-quick-safemode', false);
}

let last_run_time = new Date().getTime();

let delete_r18 = function () {
    let new_div = document.createElement('div');
    new_div.setAttribute('id', 'Pixiv-QuickSafeMode-Deleted');
    new_div.setAttribute('style', `
        width: 136px;
        height: 136px;
        background-color: #fff;
        border: 1px solid #ddd;
        border-radius: 4px;
        display: flex;
        justify-content: center;
        align-items: center;
        font-size: 50px;
        color: #999;
    `);
    new_div.innerText = '🔞';
    if (GM_getValue('pixiv-quick-safemode', false)) {
        let div = document.querySelectorAll('div[type="illust"]');
        for (let i = 0; i < div.length; i++) {
            let divs = div[i].querySelectorAll('div');
            for (let j = 0; j < divs.length; j++) {
                // if contains 'R-18' or 'R-18G' in a div
                if (divs[j].innerText.includes('R-18') || divs[j].innerText.includes('R-18G')) {
                    // let parent3 = div[i].parentNode.parentNode.parentNode;
                    let parent2 = div[i].parentNode.parentNode;
                    if (parent2.querySelectorAll('div[type="illust"]').length === 1 && parent2.tagName === 'LI') {
                        parent2.remove();
                        // console.log('2');
                    } else {
                        new_div.style.width = new_div.style.height = div[i].offsetWidth + 'px';
                        div[i].parentNode.replaceChild(new_div, div[i]);
                        // console.log('1');
                    }
                }
            }
        }
        let li = document.querySelectorAll('li[offset="0"]');
        for (let i = 0; i < li.length; i++) {
            let divs = li[i].querySelectorAll('div');
            for (let j = 0; j < divs.length; j++) {
                // if contains 'R-18' or 'R-18G' in a div
                if (divs[j].innerText.includes('R-18') || divs[j].innerText.includes('R-18G')) {
                    if (li[i].parentNode.tagName === 'UL') {
                        li[i].remove();
                    }
                }
            }
        }
        last_run_time = new Date().getTime();
    }
}

let button = function (style) {
    let div = document.createElement('div');
    div.style.display = 'flex';
    let span = document.createElement('span');
    if (style === 'nav') {
        span.setAttribute('style', `
            display: flex;
            -webkit-box-align: center;
            align-items: center;
            height: 46px;
            padding-left: 24px;
            padding-right: 24px;
            font-weight: bold;
            font-size: 16px;
            line-height: 24px;
            text-decoration: none;
            border-top: 4px solid transparent;
            transition: color 0.2s ease 0s;
            cursor: pointer;
            box-sizing: border-box;
            white-space: nowrap;
            background-color: transparent;
            color: #0096FA;
            cursor: default;
        `);
        div.setAttribute('style', `
            display: flex;
        `);
    } else if (style === 'artwork') {
        span.setAttribute('style', `
            display: flex;
            align-items: center;
            width: -webkit-fill-available;
            display: inline-grid;
            align-items: center;
            -webkit-box-align: center;
            -webkit-box-pack: center;
            justify-content: center;
            cursor: pointer;
            user-select: none;
            white-space: nowrap;
            font-size: 14px;
            line-height: 22px;
            font-weight: bold;
            padding-right: 16px;
            padding-left: 16px;
            border-radius: 999999px;
            box-sizing: border-box
            transition: color 0.2s ease 0s, background-color 0.2s ease 0s, box-shadow 0.2s ease 0s;
            height: 32px;
            background-color: transparent;
            color: #0096FA;
            position: relative;
            top: -7px;
        `);
        div.setAttribute('style', `
            display: block;
        `);
    }
    span.innerText = 'R-18';
    if (GM_getValue('pixiv-quick-safemode', false)) {
        span.style.backgroundColor = 'rgba(255, 255, 255, 0.12)';
        span.style.color = 'rgb(255, 64, 96)';
    }
    span.addEventListener('click', function () {
        if (GM_getValue('pixiv-quick-safemode', false)) {
            GM_setValue('pixiv-quick-safemode', false);
            span.style.backgroundColor = 'transparent';
            span.style.color = '#0096FA';
        } else {
            GM_setValue('pixiv-quick-safemode', true);
            span.style.backgroundColor = 'rgba(255, 255, 255, 0.12)';
            span.style.color = 'rgb(255, 64, 96)';
        }
    }, false);
    div.appendChild(span);
    div.setAttribute('class', 'Pixiv-QuickSafeMode');
    return div;
}

let main = function () {
    let nav = document.querySelectorAll('nav');
    let figcaption = document.querySelectorAll('figcaption');
    let section = document.querySelectorAll('section');
    // 确定为作品页面 (作品页面没有顶部 <nav> 菜单栏)
    if (section.length > 4 && figcaption.length === 1 && nav[0].parentNode.childNodes.length === 1) {
        if (section[3].parentNode === section[4].parentNode) {
            section[3].appendChild(button("artwork"));
        }
    }
    // 通过顶部 <nav> 菜单栏定位按钮
    else if (nav.length > 0 && figcaption.length === 0) {
        let div_nav = document.querySelector('nav');
        div_nav.appendChild(button("nav"));
    }
    delete_r18();
    last_run_time = new Date().getTime();
}

let MutationObserver = window.MutationObserver || window.WebKitMutationObserver || window.MozMutationObserver;
let observer = new MutationObserver(function (mutations) {
    mutations.forEach(function () {
        if (new Date().getTime() - last_run_time > 100) {
            // 主函数-添加交互按钮
            if (document.querySelectorAll('div[class="Pixiv-QuickSafeMode"]').length === 0) {
                main();
                delete_r18();
            }
            // 删除R-18
            else {
                delete_r18();
            }
            // console.log('Pixiv-QuickSafeMode: MutationObserver');
        }
    });
});
observer.observe(document.body, {
    childList: true,
    subtree: true
});

console.log("JS script Pixiv-ForceSafeMode (Pixiv-强制和谐模式) loaded. See more details at https://github.com/SynRGB/Pixiv-QuickSafeMode");