Remove shopee tracker

Remove shopee trackers and shorten product link.

// ==UserScript==
// @name         Remove shopee tracker
// @name:zh-TW   移除蝦皮追蹤器
// @name:zh-CN   移除虾皮追踪器
// @namespace    https://greasyfork.org
// @version      0.0.2
// @description  Remove shopee trackers and shorten product link.
// @description:zh-TW 複製商品網址不再又臭又長了
// @description:zh-CN 复制商品网址不再又臭又长了
// @author       Pixmi
// @match        *://shopee.tw/*
// @match        *://shopee.ph/*
// @match        *://shopee.sg/*
// @match        *://shopee.com.my/*
// @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
            });
        }
    }
})();