在2DFan搜索 / Add 2DFan Search Button

Adds a 'Jump to 2DFan' button next to the <h1> tag on moyu.moe

目前為 2025-02-27 提交的版本,檢視 最新版本

// ==UserScript==
// @name         在2DFan搜索 / Add 2DFan Search Button
// @namespace    http://tampermonkey.net/
// @version      0.8
// @description  Adds a 'Jump to 2DFan' button next to the <h1> tag on moyu.moe
// @author       owninnn
// @match        https://www.moyu.moe/*
// @match        http://www.moyu.moe/*
// @match        https://www.ai2.moe/files/file/*
// @grant        none
// @license GUN-V3
// ==/UserScript==

(function() {
    'use strict';


    function extractAndList(input) {
        let cleanedString = input.replace(/\t|\n|\r/g, '');
        // Remove everything inside square brackets, curly braces, and parentheses
        let cleanedString2 = cleanedString.replace(/\[.*?\]|\{.*?\}|\(.*?\)/g, '@=@=@');

        // Split the cleaned string by spaces, but keep multi-word phrases together
        let result = cleanedString2.split(/@=@=@/).filter(Boolean);

        return result;
    }

    function findLongestCJKString(strings) {
        // Regular expression to match CJK characters
        const cjkRegex = /[\u3040-\u309F\u30A0-\u30FF\u31F0-\u31FF\u3200-\u32FF\u3300-\u33FF\u3400-\u4DBF\u4E00-\u9FFF\uF900-\uFAFF\uFF00-\uFFEF]/;

        // If there is only one string, return it
        if (strings.length === 1) {
            return strings[0];
        }

        // Filter the array to only include strings with CJK characters
        const cjkStrings = strings.filter(str => cjkRegex.test(str));

        if (cjkStrings.length > 0) {
            // Find the longest string among the CJK strings
            return cjkStrings.reduce((longest, current) => {
                return current.length > longest.length ? current : longest;
            }, '');
        } else {
            // If no CJK strings are found, return the longest string from the original list
            return strings.reduce((longest, current) => {
                return current.length > longest.length ? current : longest;
            }, '');
        }
    }

    // Function to add the button
    function add2DFanButton() {
        console.log('add2DFanButton called');
        // Find all <h1> tags
        const h1Tags = document.querySelectorAll('h1');
        // Loop through each <h1> tag
        h1Tags.forEach(h1 => {
            console.log('Found <h1>:', h1.textContent);
            // Check if the button already exists
            if (!h1.nextElementSibling || !h1.nextElementSibling.classList.contains('jump-to-2dfan')) {
                // Create the button element
                const button = document.createElement('button');
                button.textContent = 'Search in 2dfan';
                button.className = 'jump-to-2dfan';
                // Get the text content of the <h1> tag
                

                const post_keyword = findLongestCJKString(extractAndList(h1.textContent.trim()))
                const url_keyword = encodeURIComponent(post_keyword)

                // Set the button's click event to open the 2DFan search URL
                button.addEventListener('click', () => {
                    window.open('https://2dfan.com/subjects/search?keyword=' + url_keyword, '_blank');

                });
                // Insert the button right after the <h1> tag
                h1.parentNode.insertBefore(button, h1.nextSibling);
                console.log('Button added next to <h1>:', h1.textContent);
            } else {
                console.log('Button already exists next to <h1>:', h1.textContent);
            }
        });
    }

    // Function to observe mutations in the DOM
    function observeDOMChanges() {
        const observer = new MutationObserver((mutations) => {
            mutations.forEach(mutation => {
                if (mutation.addedNodes.length > 0) {
                    // If new nodes are added, check for <h1> tags and add buttons
                    add2DFanButton();
                }
            });
        });

        // Start observing the body for changes
        observer.observe(document.body, {
            childList: true,
            subtree: true
        });
    }


    //observeDOMChanges();
    add2DFanButton();



})();

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址