X岛-EX

X岛揭示板增强 快捷切饼/顶部添加页码/默认关闭图片水印

// ==UserScript==
// @name         X岛-EX
// @namespace    http://tampermonkey.net/
// @version      1.2.5
// @description  X岛揭示板增强 快捷切饼/顶部添加页码/默认关闭图片水印
// @author       XY
// @match        https://www.nmbxd1.com/*/*
// @grant        GM_getValue
// @grant        GM_setValue
// @grant        GM_xmlhttpRequest
// @grant        GM_deleteValue
// @require      https://cdn.jsdelivr.net/npm/[email protected]/dist/jquery.min.js
// @license      WTFPL
// @note         致谢:切饼代码来自[XD-Enhance](https://gf.qytechs.cn/zh-CN/scripts/438164-xd-enhance)
// @note         联动:可使[增强x岛匿名版](https://gf.qytechs.cn/zh-CN/scripts/513156-%E5%A2%9E%E5%BC%BAx%E5%B2%9B%E5%8C%BF%E5%90%8D%E7%89%88)添加的预览中显示当前饼名(如ID:cOoKiEs),而非ID:cookies
// ==/UserScript==

(function($) {
    'use strict';

    // -------------------- COOKIE 功能(切换饼干、复制翻页、关闭水印) --------------------

    function toast(msg) {
        let toastDiv = $('#ae-toast-inline');
        if (!toastDiv.length) {
            toastDiv = $('<div id="ae-toast-inline" style="position: fixed; top: 10px; left: 50%; transform: translateX(-50%); background: rgba(0,0,0,0.7); color: white; padding: 10px 20px; border-radius: 5px; z-index: 9999; display: none;"></div>');
            $('body').append(toastDiv);
        }
        toastDiv.text(msg).fadeIn(300).delay(2000).fadeOut(300);
    }

    function getCookiesList() {
        return GM_getValue('cookies', {});
    }
    function getCurrentCookie() {
        return GM_getValue('now-cookie', null);
    }
    // 辅助函数:去掉 cookie.name 末尾 " - 0000-00-00 00:00:00" 部分
    function abbreviateName(name) {
        return name.replace(/\s*-\s*\d{4}-\d{2}-\d{2}\s+\d{2}:\d{2}:\d{2}$/, "");
    }
    function removeDateString() {
        $("#cookie-switcher-ui").find("*").addBack().contents().filter(function() {
            return this.nodeType === 3;
        }).each(function() {
            this.nodeValue = this.nodeValue.replace(/ - 0000-00-00 00:00:00/g, '');
        });
    }

    // 更新页面中用于显示当前饼干的区域
    function updateCurrentCookieDisplay(currentCookie) {
        const cookieDisplay = $('#current-cookie-display');
        if (cookieDisplay.length) {
            if (currentCookie) {
                const displayName = abbreviateName(currentCookie.name);
                const extra = currentCookie.desc ? (' - ' + currentCookie.desc) : '';
                cookieDisplay.text(displayName + extra).css('color', 'black');
            } else {
                cookieDisplay.text('已删除').css('color', 'red');
            }
        }
        removeDateString();
    }

    // 更新下拉菜单中的选项和默认选中
    function updateDropdownUI(cookies) {
        const dropdown = $('#cookie-dropdown');
        dropdown.empty();
        //dropdown.append('<option value="">选择饼干</option>');
        for (let id in cookies) {
            if (cookies.hasOwnProperty(id)) {
                const cookie = cookies[id];
                const displayName = abbreviateName(cookie.name);
                const optionText = displayName + (cookie.desc ? (' - ' + cookie.desc) : '');
                dropdown.append(`<option value="${id}">${optionText}</option>`);
            }
        }
        let currentCookie = getCurrentCookie();
        if (currentCookie && cookies.hasOwnProperty(currentCookie.id)) {
            dropdown.val(currentCookie.id);
        } else {
            dropdown.val("");
        }
        removeDateString();
    }

    // 切换饼干(切换后更新预览区域中的饼干显示)
    function switch_cookie(cookie) {
        if (!cookie || !cookie.id) {
            toast('无效的饼干信息!');
            return;
        }
        const url = 'https://www.nmbxd1.com/Member/User/Cookie/switchTo/id/' + cookie.id + '.html';
        $.ajax({
            type: 'GET',
            url: url,
            success: function() {
                toast('切换成功! 当前饼干为 ' + abbreviateName(cookie.name));
                GM_setValue('now-cookie', cookie);
                updateCurrentCookieDisplay(cookie);
                updateDropdownUI(getCookiesList());
                removeDateString();
                updatePreviewCookieId(); // 新增:更新预览区中当前饼干显示
            },
            error: function() {
                toast('切换失败,请重试');
            }
        });
    }

    // 刷新饼干列表(解析页面后更新)
    function refreshCookies() {
        GM_xmlhttpRequest({
            method: 'GET',
            url: 'https://www.nmbxd1.com/Member/User/Cookie/index.html',
            onload: function(response) {
                if (response.status === 200) {
                    let parser = new DOMParser();
                    let doc = parser.parseFromString(response.responseText, "text/html");
                    let rows = doc.querySelectorAll('tbody > tr');
                    let newCookies = {};
                    rows.forEach(function(row) {
                        let tds = row.querySelectorAll('td');
                        if (tds.length >= 4) {
                            let id = tds[1].textContent.trim();
                            let nameLink = tds[2].querySelector('a');
                            let nameOriginal = nameLink ? nameLink.textContent.trim() : '';
                            let desc = tds[3].textContent.trim();
                            newCookies[id] = { id: id, name: nameOriginal, desc: desc };
                        }
                    });
                    GM_setValue('cookies', newCookies);
                    updateDropdownUI(newCookies);
                    toast('饼干列表已刷新!');
                    let currentCookie = getCurrentCookie();
                    if (currentCookie && !newCookies[currentCookie.id]) {
                        currentCookie = null;
                    }
                    GM_setValue('now-cookie', currentCookie);
                    updateCurrentCookieDisplay(currentCookie);
                    removeDateString();
                    updatePreviewCookieId();
                } else {
                    toast('刷新失败,HTTP状态码:' + response.status);
                }
            },
            onerror: function() {
                toast('刷新失败,网络错误。');
            }
        });
    }

    // 构造并插入 Cookie 切换 UI——使用页面原有的 uk-grid 布局,确保按钮固定排列向右
    function createCookieSwitcherUI() {
        // 查找"回应模式"标题所在的元素
        const postFormTitle = $('.h-post-form-title:contains("回应模式")').first();
        let postFormGrid = null;

        // 如果找到“回应模式”,则定位到包含它的表单网格
        if (postFormTitle.length) {
            postFormGrid = postFormTitle.closest('.uk-grid.uk-grid-small.h-post-form-grid');
        }

        // 如果没有找到“回应模式”,改为查找“名 称”
        if (!postFormGrid || !postFormGrid.length) {
            const nameLabel = $('.h-post-form-title:contains("名 称")').first();
            if (nameLabel.length) {
                postFormGrid = nameLabel.closest('.uk-grid.uk-grid-small.h-post-form-grid');
            }
        }

        // 如果仍然未找到合适的插入位置,停止函数执行
        if (!postFormGrid || !postFormGrid.length) {
            return;
        }

        // 构造切换饼干的 UI
        const currentCookie = getCurrentCookie();
        const cookiesList = getCookiesList();
        const switcherUI = $(`
        <div class="uk-grid uk-grid-small h-post-form-grid" id="cookie-switcher-ui">
            <div class="uk-width-1-5">
                <div class="h-post-form-title">当前饼干</div>
            </div>
            <div class="uk-width-3-5 h-post-form-input" style="display: flex; align-items: center; justify-content: space-between;">
                <div class="uk-flex uk-flex-middle">
                    <span id="current-cookie-display"></span>
                    <select id="cookie-dropdown" style="margin-left: 10px;">
                        <!-- 饼干选项会通过 updateDropdownUI 填充 -->
                    </select>
                </div>
                <div class="uk-flex uk-flex-right uk-flex-nowrap">
                    <button type="button" id="apply-cookie-button" class="uk-button uk-button-default" style="margin-right: 5px;">应用</button>
                    <button type="button" id="refresh-cookie-button" class="uk-button uk-button-default">刷新</button>
                </div>
            </div>
        </div>
    `);

        // 将切换饼干的 UI 插入到表单网格上方
        postFormGrid.before(switcherUI);

        // 更新当前饼干显示和下拉选项
        updateCurrentCookieDisplay(currentCookie);
        updateDropdownUI(cookiesList);

        // 为“应用”按钮绑定点击事件
        $('#apply-cookie-button').on('click', function(e) {
            e.preventDefault();
            const selectedCookieId = $('#cookie-dropdown').val();
            if (selectedCookieId) {
                let cookiesListUpdated = getCookiesList();
                const selectedCookie = cookiesListUpdated[selectedCookieId];
                if (selectedCookie) {
                    switch_cookie(selectedCookie);
                } else {
                    toast('选择的饼干信息无效!');
                }
            } else {
                toast('请选择要切换的饼干!');
            }
        });

        // 为“刷新”按钮绑定点击事件
        $('#refresh-cookie-button').on('click', function(e) {
            e.preventDefault();
            refreshCookies();
        });
    }

    function duplicatePagination() {
        const targetElement = document.querySelector('h2.h-title');
        const paginationElement = document.querySelector('ul.uk-pagination.uk-pagination-left.h-pagination');
        if (targetElement && paginationElement) {
            const clonedPagination = paginationElement.cloneNode(true);
            targetElement.parentNode.insertBefore(clonedPagination, targetElement.nextSibling);
            console.log('Pagination duplicated successfully!');
        } else {
            console.log('Could not find target or pagination element.');
        }
    }

    function disableWatermark() {
        const watermarkCheckbox = document.querySelector('input[type="checkbox"][name="water"][value="true"]');
        if (watermarkCheckbox) {
            watermarkCheckbox.checked = false;
            console.log('Watermark checkbox unchecked!');
        } else {
            console.log('Could not find the watermark checkbox.');
        }
    }

    function mainCookie() {
        createCookieSwitcherUI();
        duplicatePagination();
        disableWatermark();
        removeDateString();
    }

    // --------------------- 新增功能 ---------------------
    // 检查页面是否存在 .h-preview-box,如果存在,则将其中的 "ID:cookies" 替换为当前实际显示的饼干
    // 此处“实际显示的饼干”取自当前饼干对象的名称经过 abbreviateName() 处理
    function updatePreviewCookieId() {
        if ($('.h-preview-box').length > 0) {
            const currentCookie = getCurrentCookie();
            if (currentCookie && currentCookie.name) {
                const displayName = abbreviateName(currentCookie.name);
                $('.h-preview-box .h-threads-info-uid').text("ID:" + displayName);
            } else {
                $('.h-preview-box .h-threads-info-uid').text("ID:cookies");
            }
        }
    }

    // --------------------- 程序入口 ---------------------
    $(document).ready(function(){
        mainCookie();
        updatePreviewCookieId(); // 初始更新预览区中的饼干显示
    });

})(jQuery);

QingJ © 2025

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