魔兽世界新闻/商城区域切换

在Tampermonkey菜单中添加选项,切换魔兽世界新闻和战网商城商品的不同区域版本

// ==UserScript==
// @name         魔兽世界新闻/商城区域切换
// @namespace    http://tampermonkey.net/
// @version      2.1
// @description  在Tampermonkey菜单中添加选项,切换魔兽世界新闻和战网商城商品的不同区域版本
// @author       电视卫士
// @match        https://wow.blizzard.cn/news/*
// @match        https://worldofwarcraft.blizzard.com/*/news/*
// @match        https://shop.battlenet.com.cn/*/product/*
// @match        https://*.shop.battle.net/*/product/*
// @match        https://news.blizzard.com/*/article/*
// @grant        GM_registerMenuCommand
// @license      MIT
// ==/UserScript==

(function() {
    'use strict';

    // 提取新闻文章 ID
    function getNewsArticleId() {
        const path = window.location.pathname;
        // 匹配 /news/ 后的数字,忽略可能的标题后缀
        const match = path.match(/(?:\/news\/|\/article\/)(\d+)/);
        return match ? match[1] : null;
    }

    // 提取商城产品路径(包括产品名和查询参数)
    function getProductPath() {
        const path = window.location.pathname;
        const query = window.location.search;
        // 提取 /product/ 后的完整路径和查询参数
        const match = path.match(/\/product\/(.+)/);
        return match ? `${match[1]}${query}` : null;
    }

    // 新闻区域配置
    const newsRegions = [
        { name: '简体中文', url: 'https://wow.blizzard.cn/news/', code: 'cn' },
        { name: 'English', url: 'https://worldofwarcraft.blizzard.com/en-us/news/', code: 'en-us' },
        { name: '繁體中文', url: 'https://worldofwarcraft.blizzard.com/zh-tw/news/', code: 'zh-tw' },
        //{ name: 'Русский', url: 'https://worldofwarcraft.blizzard.com/ru-ru/news/', code: 'ru-ru' },
        //自2022年底开始,暴雪不再更新俄语新闻,暂时关闭入口
        { name: '한국어', url: 'https://worldofwarcraft.blizzard.com/ko-kr/news/', code: 'ko-kr' }
    ];

    // 商城服务器配置
    const shopRegions = [
        { name: '中国', url: 'https://shop.battlenet.com.cn/zh-cn/product/', code: 'com.cn' },
        { name: '中國台灣', url: 'https://tw.shop.battle.net/zh-tw/product/', code: 'tw' },
        { name: 'Americas', url: 'https://us.shop.battle.net/en-us/product/', code: 'us' },
        //{ name: '한국', url: 'https://kr.shop.battle.net/ko-kr/product/', code: 'kr' },
        //韩服商店似乎不能直接通过URL跳转,暂时关闭入口
        { name: 'Europe', url: 'https://eu.shop.battle.net/en-us/product/', code: 'eu' }
    ];

    // 判断当前页面类型并获取相应 ID 或路径
    const isNewsPage = window.location.href.includes('news');
    const idOrPath = isNewsPage ? getNewsArticleId() : getProductPath();
    if (!idOrPath) return;

    // 根据页面类型选择配置
    const regions = isNewsPage ? newsRegions : shopRegions;

    // 注册菜单命令
    regions.forEach(region => {
        GM_registerMenuCommand(`切换到 ${region.name}`, () => {
            // 新闻页面加 /,商城页面保持原路径
            window.location.href = `${region.url}${idOrPath}${isNewsPage ? '/' : ''}`;
        });
    });
})();