Pixiv 小说屏蔽

按照pixiv小说的文章简介,系列标题,文章标题,作者,tag来屏蔽小说的一个脚本

// ==UserScript==
// @name         Pixiv 小说屏蔽
// @namespace    http://tampermonkey.net/
// @version      1.3
// @description  按照pixiv小说的文章简介,系列标题,文章标题,作者,tag来屏蔽小说的一个脚本
// @author       DeanShaw
// @match        https://www.pixiv.net/tags/*/novels?*s_mode=s_tag*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=chrxw.com
// @grant        GM_getValue
// @grant        GM_setValue
// @grant        GM_addStyle
// @license      MIT
// ==/UserScript==

(function() {
    'use strict';

    const ContentStorageKey = 'blockedContentList';
    const authorStorageKey = 'blockedAuthorsList';
    const titleStorageKey = 'blockedTitlesList';
    const seriesStorageKey = 'blockedSeriesList';
    const TagsStorageKey = 'blockedTagsList';
    let buttonsGenerated = 0;
    let currentPageUrl = window.location.href;

     // Function to get the blocked Tag list from storage
     function getBlockedTags() {
        return GM_getValue(TagsStorageKey, "").split(/[,,]/).filter(tag => tag.trim() !== "");
    }

    // Function to set the blocked Tag list in storage
    function setBlockedTags(list) {
        GM_setValue(TagsStorageKey, list.join(','));
    }

     // Function to get the blocked Content list from storage
    function getBlockedContents() {
        return GM_getValue(ContentStorageKey, "").split(/[,,]/).filter(content => content.trim() !== "");
    }

    // Function to set the blocked Content list in storage
    function setBlockedContents(list) {
        GM_setValue(ContentStorageKey, list.join(','));
    }

    // Function to get the blocked authors list from storage
    function getBlockedAuthors() {
        return GM_getValue(authorStorageKey, "").split(/[,,]/).filter(author => author.trim() !== "");
    }

    // Function to set the blocked authors list in storage
    function setBlockedAuthors(list) {
        GM_setValue(authorStorageKey, list.join(','));
    }

    // Function to get the blocked titles list from storage
    function getBlockedTitles() {
        return GM_getValue(titleStorageKey, "").split(/[,,]/).filter(title => title.trim() !== "");
    }

    // Function to set the blocked titles list in storage
    function setBlockedTitles(list) {
        GM_setValue(titleStorageKey, list.join(','));
    }

    // Function to get the blocked series list from storage
    function getBlockedSeries() {
        return GM_getValue(seriesStorageKey, "").split(/[,,]/).filter(series => series.trim() !== "");
    }

    // Function to set the blocked series list in storage
    function setBlockedSeries(list) {
        GM_setValue(seriesStorageKey, list.join(','));
    }

    // Function to create the "Edit List" button
    function createEditButton(text, storageKey, buttonText, position) {
        const button = document.createElement('button');
        button.innerText = text;
        button.style.position = 'fixed';
        button.style.bottom = '20px';
        button.style.left = position;
        button.style.transform = 'translateX(-50%)';
        button.style.zIndex = 1000;
        button.style.fontFamily = '微软雅黑'; // 设置字体为微软雅黑
        button.onclick = () => editList(storageKey, buttonText);
        document.body.appendChild(button);
        return button;
    }

    // Function to edit the blocked authors, titles, or series list
    function editList(storageKey, buttonText) {
        const currentList = storageKey === authorStorageKey ? getBlockedAuthors().join(',') :
                            storageKey === titleStorageKey ? getBlockedTitles().join(',') :
                            storageKey === ContentStorageKey ? getBlockedContents().join(',') :
                            storageKey === TagsStorageKey ? getBlockedTags().join(',') :
                            getBlockedSeries().join(',');
        const newList = prompt(`编辑${buttonText}(用逗号分隔):`, currentList);
        if (newList !== null) {
            storageKey === authorStorageKey ? setBlockedAuthors(newList.split(/[,,]/)) :
            storageKey === titleStorageKey ? setBlockedTitles(newList.split(/[,,]/)) :
            storageKey === ContentStorageKey ? setBlockedContents(newList.split(/[,,]/)) :
            storageKey === TagsStorageKey ? setBlockedTags(newList.split(/[,,]/)) :
            setBlockedSeries(newList.split(/[,,]/));
            location.reload();
        }
    }


    // Function to hide blocked authors' content
    function hideBlockedAuthorsContent() {
        const blockedAuthors = getBlockedAuthors();
        const authorElements = document.querySelectorAll('a.sc-d98f2c-0.gtm-novel-searchpage-result-user.sc-1rx6dmq-2.sc-1c4k3wn-15.kghgsn.kkSLPs');
        authorElements.forEach(el => {
            const authorName = el.innerText;
            if (blockedAuthors.includes(authorName)) {
                el.closest('li').style.display = 'none';
            }
        });
    }

    // Function to hide blocked titles
    function hideBlockedTitles() {
        const blockedTitles = getBlockedTitles();
        const titleElements = document.querySelectorAll('a.sc-d98f2c-0.sc-iasfms-7.sc-1c4k3wn-14.jMTyeP.ZgTBh.gtm-novel-searchpage-result-title');
        titleElements.forEach(el => {
            const title = el.innerText;
            if (blockedTitles.some(keyword => title.includes(keyword))) {
                el.closest('li').style.display = 'none';
            }
        });
    }

    // Function to hide blocked series
    function hideBlockedSeries() {
        const blockedSeries = getBlockedSeries();
        const seriesElements = document.querySelectorAll('a.sc-d98f2c-0.sc-1c4k3wn-16.hFkYWC.gtm-novel-searchpage-result-series-title');
        seriesElements.forEach(el => {
            const series = el.innerText;
            if (blockedSeries.some(keyword => series.includes(keyword))) {
                el.closest('li').style.display = 'none';
            }
        });
    }

    function hideBlockedContent() {
        const blockedContents = getBlockedContents();
        const contentElements = document.querySelectorAll('div.sc-1utla24-0.kJgShy');

        contentElements.forEach(el => {
            const content = el.innerText;
            if (blockedContents.some(keyword => content.includes(keyword))) {
                const articleElement = el.closest('li'); // 获取包含该正文的最接近的 <li> 元素
                if (articleElement) {
                    articleElement.style.display = 'none';
                }
            }
        });
    }

    function hideBlockedTags() {
        const blockedTags = getBlockedTags(); // 获取屏蔽标签列表
        const articleElements = document.querySelectorAll('ul.sc-1c4k3wn-18.kbpYuc'); // 选择每篇文章的标签元素

        articleElements.forEach(article => {
            let tags = [];
            article.querySelectorAll('a.sc-d98f2c-0.sc-1c4k3wn-19.mmZcW.gtm-novel-searchpage-result-tag span').forEach(tagElement => {
                tags.push(tagElement.innerText);
            });

            // Check if any of the tags contain any of the blocked keywords
            if (tags.some(tag => blockedTags.some(blockedTag => tag.includes(blockedTag)))) {
                article.closest('li').style.display = 'none'; // Hide the whole article
            }
        });
    }



    // Function to add a "Block" button next to each author name
    function addBlockButtons() {
        const authorElements = document.querySelectorAll('a.sc-d98f2c-0.gtm-novel-searchpage-result-user.sc-1rx6dmq-2.sc-1c4k3wn-15.kghgsn.kkSLPs');
        authorElements.forEach(el => {
            const authorName = el.innerText;
            if (!el.nextSibling || el.nextSibling.innerText !== '屏蔽该作者') {
                const blockButton = document.createElement('button');
                blockButton.innerText = '屏蔽该作者';
                blockButton.style.padding = '5px 10px'; // 设置按钮大小
                blockButton.style.fontSize = '12px'; // 设置字体大小
                blockButton.style.fontFamily = '微软雅黑'; // 设置字体为微软雅黑
                blockButton.style.marginLeft = '10px'; // 添加左边距
                blockButton.style.cursor = 'pointer'; // 设置鼠标指针样式
                blockButton.style.border = '1px solid #ccc'; // 设置边框样式
                blockButton.style.borderRadius = '5px'; // 设置边框圆角
                blockButton.style.backgroundColor = '#f0f0f0'; // 设置背景颜色
                blockButton.style.color = '#333'; // 设置文字颜色
                blockButton.onclick = () => {
                    const blockedAuthors = getBlockedAuthors();
                    if (!blockedAuthors.includes(authorName)) {
                        blockedAuthors.push(authorName);
                        setBlockedAuthors(blockedAuthors);
                        alert(`${authorName} 已加入屏蔽列表`);
                        location.reload();
                    } else {
                        alert(`${authorName} 已在屏蔽列表中`);
                    }
                };
                el.parentNode.insertBefore(blockButton, el.nextSibling); // 插入到作者名后面
            }
        });
    }


    // Function to add a "Block" button next to each series name
    function addBlockSeriesButtons() {
        const seriesElements = document.querySelectorAll('a.sc-d98f2c-0.sc-1c4k3wn-16.hFkYWC.gtm-novel-searchpage-result-series-title');
        seriesElements.forEach(el => {
            const seriesName = el.innerText;
            if (!el.nextSibling || el.nextSibling.innerText !== '屏蔽该系列') {
                const blockButton = document.createElement('button');
                blockButton.innerText = '屏蔽该系列';
                blockButton.style.padding = '5px 10px'; // 设置按钮大小
                blockButton.style.fontSize = '12px'; // 设置字体大小
                blockButton.style.fontFamily = '微软雅黑'; // 设置字体为微软雅黑
                blockButton.style.marginLeft = '10px'; // 添加左边距
                blockButton.style.cursor = 'pointer'; // 设置鼠标指针样式
                blockButton.style.border = '1px solid #ccc'; // 设置边框样式
                blockButton.style.borderRadius = '5px'; // 设置边框圆角
                blockButton.style.backgroundColor = '#f0f0f0'; // 设置背景颜色
                blockButton.style.color = '#333'; // 设置文字颜色
                blockButton.onclick = () => {
                    const blockedSeries = getBlockedSeries();
                    if (!blockedSeries.includes(seriesName)) {
                        blockedSeries.push(seriesName);
                        setBlockedSeries(blockedSeries);
                        alert(`${seriesName} 已加入屏蔽列表`);
                        location.reload();
                    } else {
                        alert(`${seriesName} 已在屏蔽列表中`);
                    }
                };
                el.parentNode.insertBefore(blockButton, el.nextSibling); // 插入到系列名后面
            }
        });
    }


    // Function to create and add the "Edit Title List" button next to each title
    function addEditTitleListButtons() {
        const titleElements = document.querySelectorAll('a.sc-d98f2c-0.sc-iasfms-7.sc-1c4k3wn-14.jMTyeP.ZgTBh.gtm-novel-searchpage-result-title');
        titleElements.forEach(el => {
            if (!el.nextSibling || el.nextSibling.innerText !== '编辑标题屏蔽关键词列表') {
                const editButton = document.createElement('button');
                editButton.innerText = '编辑标题屏蔽关键词列表';
                editButton.style.padding = '5px 10px'; // 设置按钮大小
                editButton.style.fontSize = '12px'; // 设置字体大小
                editButton.style.fontFamily = '微软雅黑'; // 设置字体为微软雅黑
                editButton.style.marginLeft = '10px'; // 添加左边距
                editButton.style.cursor = 'pointer'; // 设置鼠标指针样式
                editButton.style.border = '1px solid #ccc'; // 设置边框样式
                editButton.style.borderRadius = '5px'; // 设置边框圆角
                editButton.style.backgroundColor = '#f0f0f0'; // 设置背景颜色
                editButton.style.color = '#333'; // 设置文字颜色
                editButton.onclick = () => editList(titleStorageKey, '标题屏蔽关键词');
                el.parentNode.insertBefore(editButton, el.nextSibling); // Insert the button next to the title element
            }
        });
    }

    // Function to add a "Block" button next to each content element
    function addBlockContentButtons() {
        const contentElements = document.querySelectorAll('div.sc-1utla24-0.kJgShy');
        contentElements.forEach(el => {
            // Check if the parent element has the correct class name to avoid adding button next to title elements
            const parentElement = el.closest('li');
            if (parentElement && parentElement.querySelector('div.sc-1utla24-0.kJgShy')) {
                // Check if the button already exists to avoid duplication
                if (!el.nextSibling || el.nextSibling.innerText !== '编辑正文描述屏蔽关键词列表') {
                    const blockButton = document.createElement('button');
                    blockButton.innerText = '编辑正文描述屏蔽关键词列表';
                    blockButton.style.padding = '5px 10px'; // 设置按钮大小
                    blockButton.style.fontSize = '12px'; // 设置字体大小
                    blockButton.style.fontFamily = '微软雅黑'; // 设置字体为微软雅黑
                    blockButton.style.marginLeft = '10px'; // 添加左边距
                    blockButton.style.cursor = 'pointer'; // 设置鼠标指针样式
                    blockButton.style.border = '1px solid #ccc'; // 设置边框样式
                    blockButton.style.borderRadius = '5px'; // 设置边框圆角
                    blockButton.style.backgroundColor = '#f0f0f0'; // 设置背景颜色
                    blockButton.style.color = '#333'; // 设置文字颜色
                    blockButton.onclick = () => editList(ContentStorageKey, '屏蔽正文');
                    el.parentNode.insertBefore(blockButton, el.nextSibling); // Insert the button after the content element
                }
            }
        });
    }


    // Function to add a "Block Tag" button next to each author name
    function addBlockTagButton() {
        const blockButtons = document.querySelectorAll('button');
        blockButtons.forEach(button => {
            if (button.innerText === '屏蔽该作者') {
                if (!button.nextSibling || button.nextSibling.innerText !== '编辑Tag屏蔽列表') {
                    const blockTagButton = document.createElement('button');
                    blockTagButton.innerText = '编辑Tag屏蔽列表';
                    blockTagButton.style.padding = '5px 10px'; // 设置按钮大小
                    blockTagButton.style.fontSize = '12px'; // 设置字体大小
                    blockTagButton.style.fontFamily = '微软雅黑'; // 设置字体为微软雅黑
                    blockTagButton.style.marginLeft = '10px'; // 添加左边距
                    blockTagButton.style.cursor = 'pointer'; // 设置鼠标指针样式
                    blockTagButton.style.border = '1px solid #ccc'; // 设置边框样式
                    blockTagButton.style.borderRadius = '5px'; // 设置边框圆角
                    blockTagButton.style.backgroundColor = '#f0f0f0'; // 设置背景颜色
                    blockTagButton.style.color = '#333'; // 设置文字颜色
                    blockTagButton.onclick = () => editList(TagsStorageKey, '屏蔽Tag');
                    button.parentNode.insertBefore(blockTagButton, button.nextSibling); // Insert after the "Block" button
                }
            }
        });
    }



    // Function to repeatedly execute the script to ensure it remains active
    function repeatExecution() {
        setInterval(() => {
            if (currentPageUrl !== window.location.href) {
                buttonsGenerated = 0; // Reset the flag when the URL changes
                currentPageUrl = window.location.href; // Update the current URL
            }
            if (buttonsGenerated === 0) {
                addBlockButtons();
                addEditTitleListButtons();
                addBlockSeriesButtons();
                addBlockTagButton();
                addBlockContentButtons();
                removeIncorrectButtons();
                buttonsGenerated = 1; // Set the flag to indicate buttons have been generated
            }
            hideBlockedAuthorsContent();
            hideBlockedTitles();
            hideBlockedSeries();
            hideBlockedContent();
            hideBlockedTags();
        }, 3000); // Repeat every 3 seconds
    }

    // Function to remove incorrect buttons and keep correct ones
    function removeIncorrectButtons() {
        const correctButtons = document.querySelectorAll('div.sc-1utla24-0.kJgShy + button');
        correctButtons.forEach(button => {
            // Check if the button's parent node matches the incorrect structure
            if (button.parentNode.classList.contains('sc-1c4k3wn-20')) {
                button.remove(); // Remove the button if it's in the incorrect structure
            }
        });
    }



    // Initialize the script
    function init() {
        createEditButton('编辑正文描述屏蔽列表', ContentStorageKey, '屏蔽正文描述', '30%');
        createEditButton('编辑作者屏蔽列表', authorStorageKey, '屏蔽作者', '45%');
        createEditButton('编辑标题屏蔽关键词列表', titleStorageKey, '屏蔽标题', '60%');
        createEditButton('编辑系列屏蔽关键词列表', seriesStorageKey, '屏蔽系列', '75%');
        createEditButton('编辑Tag屏蔽关键词列表', TagsStorageKey, '屏蔽Tag', '15%');

        addBlockButtons();
        addEditTitleListButtons();
        addBlockSeriesButtons();
        addBlockTagButton();
        addBlockContentButtons();
        removeIncorrectButtons();

        hideBlockedAuthorsContent();
        hideBlockedTitles();
        hideBlockedSeries();
        hideBlockedContent();
        hideBlockedTags();

        repeatExecution(); // Start the repeating execution
    }

    // Run the script on page load
    window.addEventListener('load', init);

})();