京东.淘宝.天猫.把手机网页转为电脑网页,到达原始地址

手机分享的商品链接很长,电脑打开长链时,自动转原始网页,到达原始地址。

// ==UserScript==
// @name         京东.淘宝.天猫.把手机网页转为电脑网页,到达原始地址
// @namespace    http://tampermonkey.net/
// @version      1.0.0
// @description  手机分享的商品链接很长,电脑打开长链时,自动转原始网页,到达原始地址。
// @author       yezi_jinn
// @match        *://item.m.jd.com/product/*.html*
// @match        *://item.taobao.com/item.htm*
// @match        *://detail.tmall.com/item.htm*
// @grant        none
// @run-at       document-start
// @license      MIT
// ==/UserScript==

(function() {
    'use strict';

    // 京东链接处理
    if (location.hostname === 'item.m.jd.com') {
        // 直接从路径提取商品ID
        const path = location.pathname;
        const idStart = path.lastIndexOf('/') + 1;
        const idEnd = path.indexOf('.html', idStart);

        if (idEnd > idStart) {
            const id = path.substring(idStart, idEnd);
            window.stop();
            location.replace(`https://item.jd.com/${id}.html`);
        }
        return;
    }

    // 淘宝/天猫链接处理
    if (location.hostname.includes('taobao.com') ||
        location.hostname.includes('tmall.com')) {

        // 关键修复:检查是否已经是简洁链接
        const search = location.search;
        if (search.split('&').length <= 2 &&
            (search.includes('?id=') || search.includes('&id='))) {
            // 已经是电脑版链接,不处理
            return;
        }

        // 提取ID
        const idIndex = search.indexOf('id=');
        if (idIndex !== -1) {
            const idStart = idIndex + 3;
            let idEnd = search.indexOf('&', idStart);
            if (idEnd === -1) idEnd = search.length;

            const id = search.substring(idStart, idEnd);

            // 验证ID为纯数字
            if (/^\d+$/.test(id)) {
                window.stop();

                // 根据域名选择目标URL
                const isTmall = location.hostname.includes('tmall');
                const newUrl = isTmall
                    ? `https://detail.tmall.com/item.htm?id=${id}`
                    : `https://item.taobao.com/item.htm?id=${id}`;

                // 仅在不同时才重定向
                if (location.href !== newUrl) {
                    location.replace(newUrl);
                }
            }
        }
    }
})();