uniqueitemtrad

在编年史传奇物品旁添加交易集市跳转,仅支持国际服

// ==UserScript==
// @name         uniqueitemtrad
// @namespace    http://tampermonkey.net/
// @version      1.0.3
// @description  在编年史传奇物品旁添加交易集市跳转,仅支持国际服
// @author       Azure
// @match        https://poedb.tw/tw/*
// @match        https://poedb.tw/cn/*
// @match        https://poe2db.tw/tw/*
// @match        https://poe2db.tw/cn/*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=poedb.tw
// @grant        none
// @license      MIT
// ==/UserScript==

(function() {
    'use strict';

    function extractNValue(dataHover) {
        if (!dataHover) return null;

        // 方法1: 使用正则表达式直接匹配n参数
        const match = dataHover.match(/[?&]n=([^&]*)|[?&]name=n(\d+)/i);
        if (match && (match[1] || match[2])) {
            return match[1] || match[2];
        }

        // 方法2: 将字符串转换为URL参数
        const params = new URLSearchParams(dataHover);
        if (params.has('n')) return params.get('n');
        if (params.has('name') && params.get('name').startsWith('n')) {
            return params.get('name').substring(1);
        }

        return null;
    }

    // 页面完全加载后执行
    window.addEventListener('load', function() {
        // 获取class为'flex-grow-1 ms-2'的元素内部的class为'uniqueitem'的元素
        let uniqueItems = document.querySelectorAll('.uniqueitem .uniqueTypeLine');

        const hostname = window.location.hostname;
        let poe1 = hostname.toLowerCase().includes('poedb');

        if (uniqueItems.length > 0) {
            uniqueItems.forEach(function(item, index) {
                let itemname = extractNValue(item.parentElement.getAttribute('data-hover'));
                const tradeLink = document.createElement('a');
                tradeLink.href = poe1 ? `https://www.pathofexile.com/trade/search/Mercenaries?q={"query":{"name":"${itemname}"}}` : `https://www.pathofexile.com/trade2/search/poe2/Dawn%20of%20the%20Hunt?q={"query":{"name":"${itemname}"}}`;
                tradeLink.target = '_blank';
                tradeLink.textContent = '查看市集';
                tradeLink.className = 'trade-link';
                tradeLink.style= "font-size: 11px; margin-left: 8px;";
                item.parentElement.appendChild(tradeLink);
            });
        }
    }, false);
})();