|
| 1 | +// ==UserScript== |
| 2 | +// @name No Track |
| 3 | +// @name:zh-CN 不跟踪 |
| 4 | +// @namespace https://github.com/smxl |
| 5 | +// @home https://github.com/smxl |
| 6 | +// @version 0.1.0 |
| 7 | +// @description URL searchParams Remover, white list mode |
| 8 | +// @description:zh-CN URL 跟踪参数移除, 仅保留白名单 |
| 9 | +// @author smxl |
| 10 | +// @include https://www.google.*/* |
| 11 | +// @include https://*.bilibili.com/* |
| 12 | +// @include https://*.bing.com/* |
| 13 | +// @grant none |
| 14 | +// @license MIT |
| 15 | +// ==/UserScript== |
| 16 | +(function () { |
| 17 | + const whitelist = ['q', 'keyword']; |
| 18 | + |
| 19 | + function removeQueryParams() { |
| 20 | + const url = new URL(window.location.href); |
| 21 | + const searchParams = url.searchParams; |
| 22 | + const paramsToDelete = []; |
| 23 | + |
| 24 | + for (const key of searchParams.keys()) { |
| 25 | + if (!whitelist.includes(key)) { |
| 26 | + paramsToDelete.push(key); |
| 27 | + } |
| 28 | + } |
| 29 | + |
| 30 | + paramsToDelete.forEach((key) => { |
| 31 | + searchParams.delete(key); |
| 32 | + }); |
| 33 | + |
| 34 | + const newUrl = url.origin + url.pathname + '?' + searchParams.toString(); |
| 35 | + |
| 36 | + if (newUrl !== window.location.href) { |
| 37 | + window.history.replaceState(null, null, newUrl); |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + removeQueryParams(); |
| 42 | + window.addEventListener('popstate', removeQueryParams); |
| 43 | + window.addEventListener('pushstate', removeQueryParams); |
| 44 | + window.addEventListener('replacestate', removeQueryParams); |
| 45 | +})(); |
| 46 | + |
0 commit comments