-
-
Save wudi/1d1f1c4cc25b8b5bf7b4bd410d020469 to your computer and use it in GitHub Desktop.
nospm 移除虾米、淘宝和天猫网址中的 spm 参数(包括地址栏和页面中的链接):https://chrome.google.com/webstore/detail/nospm/dlkfdpdjhnonlhjhelnfaninbdggnkgl
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 nospm | |
// @version 1.1 | |
// @description 移除虾米、淘宝和天猫网址中的 spm 参数(包括地址栏和页面中的链接) | |
// @include *://*.xiami.com/* | |
// @include *://*.taobao.com/* | |
// @include *://*.tmall.com/* | |
// ==/UserScript== | |
let forEach = Function.call.bind([].forEach) | |
let removeSpm = (url, next) => { | |
let search = url.search | |
if (!search) return | |
let query = parseQuery(search.slice(1)) | |
if (query.spm) { | |
delete query.spm | |
let param = toParam(query) | |
next(url.origin + url.pathname + (param ? '?' + param : '') + url.hash) | |
} | |
} | |
let parseQuery = (param) => { | |
return param.split('&') | |
.map((pair) => pair.split('=')) | |
.reduce((o, p) => { o[p[0]] = decodeURIComponent(p[1]); return o }, {}) | |
} | |
let toParam = (object) => { | |
return Object.keys(object) | |
.map((key) => key + '=' + encodeURIComponent(object[key])) | |
.join('&') | |
} | |
let removeLinkSpm = (link) => { | |
removeSpm(link, (newUrl) => { | |
link.href = newUrl | |
}) | |
} | |
let removeLocationSpm = () => { | |
removeSpm(location, (newUrl) => { | |
history.replaceState(null, document.title, newUrl) | |
}) | |
} | |
let isLink = (node) => { | |
return node && node.nodeName === 'A' | |
} | |
let observer = new MutationObserver((mutations) => { | |
mutations.forEach((mutation) => { | |
let type = mutation.type | |
if (type === 'attributes') { | |
if (mutation.attributeName === 'href' && isLink(mutation.target)) { | |
removeLinkSpm(mutation.target) | |
} | |
} else if (type === 'childList' || type === 'subtree') { | |
forEach(mutation.addedNodes, (node) => { | |
if (isLink(node)) { | |
removeLinkSpm(node) | |
} | |
}) | |
} | |
}) | |
}) | |
observer.observe(document.body, {attributes: true, childList: true, subtree: true}) | |
forEach(document.links, removeLinkSpm) | |
removeLocationSpm() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment