移除虾皮追踪器

复制商品网址不再又臭又长了

当前为 2023-12-12 提交的版本,查看 最新版本

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         Remove shopee tracker
// @name:zh-TW   移除蝦皮追蹤器
// @name:zh-CN   移除虾皮追踪器
// @namespace    https://greasyfork.org
// @version      0.0.1
// @description  Remove shopee trackers and shorten product link.
// @description:zh-TW 複製商品網址不再又臭又長了
// @description:zh-CN 复制商品网址不再又臭又长了
// @author       Pixmi
// @match        *://shopee.com/*
// @match        *://shopee.com.my/*
// @match        *://shopee.tw/*
// @match        *://shopee.cn/*
// @match        *://shopee.ph/*
// @match        *://shopee.sg/*
// @icon         https://icons.duckduckgo.com/ip2/shopee.com.ico
// @grant        none
// @license      MPL-2.0
// @run-at       document-body
// ==/UserScript==

(function() {
    'use strict';

    const URL = window.location.href;
    const urlRegex = new RegExp(/\-i\.([\d]+)\.([\d]+)/);
    if (URL.match(urlRegex)) {
        let match = URL.match(urlRegex)
        window.location.replace(`${window.location.origin}/product/${match[1]}/${match[2]}`);
    } else {
        const rootmatch = document.evaluate('//div[@id="main"]', document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null);
        const rootnode = rootmatch.singleNodeValue;
        if (rootnode) {
            const callback = (mutationsList, observer) => {
                for (const mutation of mutationsList) {
                    const target = mutation.target;
                    if (target.nodeName !== 'DIV') continue;
                    const links = target.querySelectorAll('a');
                    if (links.length) {
                        for (const link of links) {
                            let match = link.href.match(urlRegex);
                            if (!match) continue;
                            link.href = `${window.location.origin}/product/${match[1]}/${match[2]}`;
                        }
                    } else {
                        const link = target.closest('a') || false;
                        if (!link) continue;
                        let match = link.href.match(urlRegex);
                        if (!match) continue;
                        link.href = `${window.location.origin}/product/${match[1]}/${match[2]}`;
                    }
                }
            }
            const observer = new MutationObserver(callback);
            // start observe
            observer.observe(document.body, {
                attributes: true,
                childList: true,
                subtree: true
            });
        }
    }
})();