Skip to content

Instantly share code, notes, and snippets.

@ambar
Last active March 16, 2024 01:44
Show Gist options
  • Star 13 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save ambar/9706385 to your computer and use it in GitHub Desktop.
Save ambar/9706385 to your computer and use it in GitHub Desktop.
nospm 移除虾米、淘宝和 Bilibili 网址中的 spm 参数(包括地址栏和页面中的链接),推荐使用 Tampermonkey 或者 Violentmonkey 安装,UserScript 安装地址 https://gist.github.com/ambar/9706385/raw/nospm.user.js
// ==UserScript==
// @name nospm
// @version 1.4.0
// @run-at document-start
// @updateURL https://gist.github.com/ambar/9706385/raw/nospm.user.js
// @downloadURL https://gist.github.com/ambar/9706385/raw/nospm.user.js
// @description 移除 Bilibili、淘宝、天猫、优酷网址中的 spm 参数(包括地址栏和页面中的链接)
// @include https://*.bilibili.com/*
// @include https://*.taobao.com/*
// @include https://*.tmall.com/*
// @include https://*.youku.com/*
// ==/UserScript==
let forEach = Function.call.bind([].forEach)
let config = [
[new URLPattern('https://{*.}?bilibili.com'), ['spm_id_from', 'vd_source']],
[new URLPattern('https://{*.}?taobao.com'), ['spm', 'scm', 'pvid']],
[new URLPattern('https://{*.}?tmall.com'), ['spm', 'scm', 'pvid']],
[new URLPattern('https://{*.}?youku.com'), ['spm', 'scm']],
]
/**
* @param {Location | HTMLLinkElement} urlLike
* @param {(url: string) => void} next
*/
let removeSpm = (urlLike, next) => {
if (!urlLike.search) return
let url = new URL(urlLike.href)
for (const [pattern, params] of config) {
if (pattern.test(url)) {
let size = url.searchParams.size
params.forEach(x => url.searchParams.delete(x))
if (size !== url.searchParams.size) {
next(url.toString())
}
break
}
}
}
/**
* @param {HTMLLinkElement} link
*/
let removeLinkSpm = link => {
removeSpm(link, newUrl => {
link.href = newUrl
})
}
let removeSpmInNavigation = () => {
removeSpm(location, newUrl => {
history.replaceState(null, document.title, newUrl)
})
// listen history change
navigation.addEventListener('navigate', e => {
// exit early if this navigation shouldn't be intercepted
if (!e.canIntercept || e.hashChange || e.downloadRequest !== null) {
return
}
const url = new URL(e.destination.url)
removeSpm(url, () => {
e.preventDefault()
})
})
}
let observer = new MutationObserver(mutations => {
let isLink = node => {
return node && node.nodeName === 'A'
}
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)
}
})
}
})
})
let domReady = new Promise(resolve => {
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', resolve)
} else {
resolve()
}
})
let removeSpmInLinks = async () => {
await domReady
observer.observe(document.body, {
attributes: true,
childList: true,
subtree: true,
})
forEach(document.links, removeLinkSpm)
}
removeSpmInNavigation()
removeSpmInLinks()
@ambar
Copy link
Author

ambar commented Sep 13, 2016

## 本地 Chrome 安装

打开 chrome://extensions/,把文件拖放到窗口中。

UserScript 安装:

推荐使用 Tampermonkey 或者 Violentmonkey 安装,安装链接为 https://gist.github.com/ambar/9706385/raw/nospm.user.js

更新

  • 1.2: 移除虾米,增加 Bilibili
  • 1.3: Youku 支持
  • 1.4: 阻止地址栏参数动态更新

@chegg007
Copy link

请问这个的作用仅仅是用于精简网址吗?

@chenzz
Copy link

chenzz commented Nov 27, 2018

chrome不管用了,请看下吧

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment