JD Mobile to PC Redirect

Redirect JD mobile product links to PC version automatically

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         JD Mobile to PC Redirect
// @namespace    http://tampermonkey.net/
// @version      0.3
// @description  Redirect JD mobile product links to PC version automatically
// @author       多看多学习
// @match        *://*.m.jd.com/*
// @match        *://mitem.jd.com/*
// @match        *://wqs.jd.com/*
// @match        *://wqitem.jd.com/*
// @match        *://item.m.jd.com/*
// @match        *://*.jd.hk/*
// @match        *://m.yiyaojd.com/*
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    // Function to convert mobile JD URL to PC URL
    function convertToPCUrl(mobileUrl) {
        // Handle different mobile URL patterns
        if (/\/\/item\.m\.jd\.com\/product\/\d+\.html/.test(mobileUrl)) {
            // Convert product page URL
            return mobileUrl.replace(/\/\/item\.m\.jd\.com\/product\/(\d+)\.html/, '//item.jd.com/$1.html');
        } else if (/\/\/mitem\.jd\.com\/product\/\d+\.html/.test(mobileUrl)) {
            // Convert another type of product page URL
            return mobileUrl.replace(/\/\/mitem\.jd\.com\/product\/(\d+)\.html/, '//item.jd.com/$1.html');
        } else if (/\/\/wqitem\.jd\.com\/product\/\d+\.html/.test(mobileUrl)) {
            // Convert another type of product page URL
            return mobileUrl.replace(/\/\/wqitem\.jd\.com\/product\/(\d+)\.html/, '//item.jd.com/$1.html');
        } else if (/\/\/m\.yiyaojd\.com\/product\/\d+\.html/.test(mobileUrl)) {
            // Convert another type of product page URL
            return mobileUrl.replace(/\/\/m\.yiyaojd\.com\/product\/(\d+)\.html/, '//item.jd.com/$1.html');
        } else if (/\/\/m\.jd\.hk\/product\/\d+\.html/.test(mobileUrl)) {
            // Convert another type of product page URL
            return mobileUrl.replace(/\/\/m\.jd\.hk\/product\/(\d+)\.html/, '//item.jd.hk/$1.html');
        } else {
            // Default conversion for other pages
            return mobileUrl
                .replace(/\/\/m\.jd\.com/, '//www.jd.com')
                .replace(/\/\/mitem\.jd\.com/, '//item.jd.com')
                .replace(/\/\/wqs\.jd\.com/, '//www.jd.com')
                .replace(/\/\/wqitem\.jd\.com/, '//item.jd.com')
                .replace(/\/\/item\.m\.jd\.com/, '//item.jd.com')
                .replace(/\/\/m\.yiyaojd\.com/, '//www.jd.com')
                .replace(/\/\/m\.jd\.hk/, '//www.jd.hk');
        }
    }

    // Check if the current URL is a mobile JD URL
    function isMobileJDUrl(url) {
        return /\/\/m\.jd\.com|\/\/mitem\.jd\.com|\/\/wqs\.jd\.com|\/\/wqitem\.jd\.com|\/\/item\.m\.jd\.com|\/\/m\.yiyaojd\.com|\/\/m\.jd\.hk/.test(url);
    }

    // Get the current URL
    const currentUrl = window.location.href;

    // If it is a mobile JD URL, redirect to the PC version
    if (isMobileJDUrl(currentUrl)) {
        const pcUrl = convertToPCUrl(currentUrl);
        window.location.replace(pcUrl);
    }
})();