-
Notifications
You must be signed in to change notification settings - Fork 0
/
remove-bilibili-url-spam.user.js
85 lines (81 loc) · 2.31 KB
/
remove-bilibili-url-spam.user.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
// ==UserScript==
// @name 移除 Bilibili 链接垃圾参数
// @namespace https://github.com/s0urcelab/userscripts
// @match https://www.bilibili.com/video/*
// @match https://live.bilibili.com/*
// @grant none
// @version 1.3
// @author s0urce
// @description 移除Bilibili地址栏链接中的垃圾参数,如spm_id_from、from_sourse、from等
// @icon https://www.bilibili.com/favicon.ico
// @run-at document-start
// @license MIT
// ==/UserScript==
// 垃圾参数列表
// Thanks to [Bilibili 干净链接](https://greasyfork.org/zh-CN/scripts/393995)
const SPAM_PARAMS = new Set([
'spm_id_from',
'from_source',
'msource',
'bsource',
'seid',
'source',
'session_id',
'visit_id',
'sourceFrom',
'from_spmid',
'share_source',
'share_medium',
'share_plat',
'share_session_id',
'share_tag',
'unique_k',
'csource',
'vd_source',
'tab',
'is_story_h5',
'share_from',
'plat_id',
'-Arouter',
'launch_id',
'live_from',
'hotRank',
'broadcast_type',
])
const removeSpam = function (url) {
const [withoutHash, hash] = url.split('#')
const [pathname, qs] = withoutHash.split('?')
const params = new URLSearchParams(qs)
const newParams = new URLSearchParams()
for (const [key, val] of params) {
if (!SPAM_PARAMS.has(key)) {
newParams.append(key, val)
}
}
const newQs = newParams.toString()
return `${pathname}${newQs ? `?${newQs}` : ''}${hash ? `#${hash}` : ''}`
}
// 劫持history原生方法
function hijackHistoryNative(name) {
const historyFunc = history[name]
return function () {
// 修改url参数
const { 2: url } = arguments
arguments[2] = removeSpam(url)
// console.error(name, arguments[2])
return historyFunc.apply(this, arguments)
}
}
history.pushState = hijackHistoryNative('pushState')
history.replaceState = hijackHistoryNative('replaceState')
// 劫持window.open原生方法(live直播跳转)
function hijackOpenNative() {
const openFunc = window.open
return function () {
// 修改url参数
const { 0: url } = arguments
arguments[0] = removeSpam(url)
return openFunc.apply(this, arguments)
}
}
window.open = hijackOpenNative()