PoE Wiki redirect

Adds a floating button to go from the PoE 1 wiki to its PoE 2 sister site for the current search/page

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name        PoE Wiki redirect
// @namespace   TripeTrouble
// @match       https://www.poewiki.net/*
// @grant       none
// @version     1.2
// @author      .
// @description Adds a floating button to go from the PoE 1 wiki to its PoE 2 sister site for the current search/page
 // @license MIT
// ==/UserScript==

(function() {
    'use strict';

    // Create the floating button
    const button = document.createElement('button');
    button.innerText = 'Go To PoE2 Wiki';
    button.style.position = 'fixed';
    button.style.right = '20px';
    button.style.top = '35px';
    button.style.zIndex = '9999';
    button.style.padding = '10px';
    button.style.backgroundColor = '#4CAF50';
    button.style.color = 'white';
    button.style.border = 'none';
    button.style.borderRadius = '5px';
    button.style.cursor = 'pointer';
    button.style.boxShadow = '0 2px 5px rgba(0,0,0,0.2)';
    document.body.appendChild(button);

    function rewriteUrl(currentUrl) {
        try {
            const url = new URL(currentUrl);
            const searchParams = url.searchParams;
            const keyword = searchParams.get('search');
            if (keyword) {
                const newBaseUrl = 'https://www.poe2wiki.net/index.php';
                const newUrl = new URL(newBaseUrl);
                newUrl.searchParams.set('search', keyword);
                return newUrl.toString();
            } else {

                const path = url.pathname;
                const wikiIndex = path.indexOf('wiki/');
                if (wikiIndex !== -1) {
                    const wikiPath = path.substring(wikiIndex + 5); // Extract after 'wiki/'
                    if (wikiPath) {
                        const newBaseUrl = 'https://www.poe2wiki.net';
                        const newUrl = new URL(newBaseUrl);
                        newUrl.pathname = 'wiki/' + wikiPath;
                        return newUrl.toString();
                    }
                }
                return 'No ?search= parameter or wiki/ path found in the URL';
            }
        } catch (e) {
            console.error('Error rewriting URL:', e);
            return currentUrl;
        }
    }


    button.addEventListener('click', () => {
        const currentUrl = window.location.href;
        const newUrl = rewriteUrl(currentUrl);
        window.location.href = newUrl;
    });
})();