Last active
October 20, 2023 20:56
-
-
Save yujinlin0224/0a3db4c4d3e48377756918378f716430 to your computer and use it in GitHub Desktop.
Redirect shopee product page
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==UserScript== | |
// @name Redirect shopee product page | |
// @version 1.2 | |
// @namespace https://yurina.dev/ | |
// @downloadURL https://gist.github.com/yujinlin0224/0a3db4c4d3e48377756918378f716430/raw/RedirectShopeeProductPage.user.js | |
// @updateURL https://gist.github.com/yujinlin0224/0a3db4c4d3e48377756918378f716430/raw/RedirectShopeeProductPage.user.js | |
// @homepageURL https://github.com/yujinlin0224 | |
// @author yujinlin0224 | |
// @description Redirect shopee product page | |
// @match http*://shopee.tw/* | |
// ==/UserScript== | |
const fromRegexp = /^\/.+?-i\.(\d+)\.(\d+).*$/ | |
const toRegexp = '/product/$1/$2' | |
window.addEventListener("load", (event) => { | |
const { pathname } = location | |
if (!fromRegexp.test(pathname)) return | |
console.log(`Redirecting shopee product page...`) | |
const url = new URL(location.href) | |
url.pathname = url.pathname.replace(fromRegexp, toRegexp) | |
location.replace(url.href) | |
}); | |
function applyHistoryState(target, thisArg, argArray) { | |
const [, , pathname] = argArray | |
if (!fromRegexp.test(pathname)) return target.apply(thisArg, argArray) | |
console.log(`Redirecting shopee product page...`) | |
const newArgArray = argArray.with(2, pathname.replace(fromRegexp, toRegexp)) | |
return target.apply(thisArg, newArgArray) | |
} | |
window.history.pushState = new Proxy(window.history.pushState, { | |
apply: applyHistoryState, | |
}) | |
window.history.replaceState = new Proxy(window.history.replaceState, { | |
apply: applyHistoryState, | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment