// ==UserScript== // @name Taobao/TMall Link Shrinker // @namespace https://jest.one/ // @version 0.1 // @description Shrink Taobao/TMall links // @author You // @match *://*.taobao.com/item.htm* // @match *://*.tmall.com/item.htm* // @run-at document-start // @grant none // ==/UserScript== (function() { 'use strict'; if (getParamsCount() > 1) { redirectToSimpleUrl(); } // TMall would replace your simple URL with a longer one some time after the page is loaded; this reverts it let intervalID = window.setInterval(() => { if (getParamsCount() > 1) { replaceWithSimpleUrl(); } }, 500); /////////////////////////// function getParamsCount() { let urlParams = new URLSearchParams(window.location.search); // e.g. ?id=627682809552&skuId=4629184475544 let paramsCount = [...urlParams.entries()].length; console.log(paramsCount); return paramsCount; } function getItemID() { let urlParams = new URLSearchParams(window.location.search); // e.g. ?id=627682809552&skuId=4629184475544 let itemID = urlParams.get("id"); // e.g. 627682809552 return itemID; } function buildSimpleUrl() { let origin = window.location.origin; // e.g. https://detail.tmall.com let newUrl = `${origin}/item.htm?id=${getItemID()}`; return newUrl; } function redirectToSimpleUrl() { let newUrl = buildSimpleUrl(); console.log(`Redirecting to ${newUrl}`); window.location.href = newUrl; } function replaceWithSimpleUrl() { let newUrl = buildSimpleUrl(); console.log(`Replacing history state to ${newUrl}`); window.history.replaceState(null, null, newUrl); } })();