// ==UserScript== // @name 縮減蝦皮網址 // @namespace https://greasyfork.org/zh-TW/scripts/457084 // @match *shopee.tw/* // @author czh // @icon https://www.google.com/s2/favicons?sz=64&domain=shopee.tw // @run-at document-start // @license GNU GPLv3 // @description 網址只留下識別碼 // @version 0.0.2.0 // @downloadURL none // ==/UserScript== (function() { 'use strict'; // 定義正規表達式,用於提取 shopId 和 itemId const shopeeRegex = /shopee\.tw\/.*-i\.(\d+)\.(\d+)/; let lastHandledUrl = ''; // 處理網址縮短的核心函式 function processUrl() { const currentUrl = window.location.href; // 如果目前的網址和上次處理的網址相同,就不用再處理了 if (currentUrl === lastHandledUrl) { return; } const match = currentUrl.match(shopeeRegex); if (match) { const shopId = match[1]; const itemId = match[2]; const newUrl = `https://shopee.tw/0-i.${shopId}.${itemId}`; // 如果目前的網址已經是精簡格式,或與上次處理的網址相同,就跳過 if (currentUrl === newUrl || currentUrl === lastHandledUrl) { return; } // 使用 history.replaceState() 來修改網址 // 這樣做不會產生新的瀏覽歷史記錄,且不會重新載入頁面 window.history.replaceState({}, '', newUrl); // 更新上次處理的網址 lastHandledUrl = newUrl; console.log("網址已縮短為:", newUrl); } } // 使用 MutationObserver 監聽 DOM 變化 // 這是最可靠的方式,當蝦皮動態載入新內容時,都會觸發它 const observer = new MutationObserver(processUrl); // 監聽整個網頁主體(document.body)的子節點和子樹變化 observer.observe(document.body, { childList: true, subtree: true }); // 為了確保頁面剛載入時也能處理,手動呼叫一次 window.addEventListener('load', processUrl); })();