Remove tracking elements from links
当前为
// ==UserScript==
// @name Link Untracker
// @namespace IzzySoft
// @description Remove tracking elements from links
// @license CC BY-NC-SA
// @include *
// @exclude *phpmyadmin*
// @version 7
// @grant unsafeWindow
// ==/UserScript==
/* badp: we strip parameters starting with that. Some are taken from NetURL, see
https://github.com/Smile4ever/firefoxaddons/tree/master/Neat%20URL-webext
fb_: Facebook; ga_: Google; hmb_: HumbleBundle(?); utm_: Urchin Tracker; wt_: Webtrekk; WT.: Webtrends
*/
var badp = ['fb_','ga_','hmb_','utm_',
'ei@google.','gws_rd@google.','sei@google.','ved@google.',
'pd_rd_r@amazon.','pd_rd_w@amazon.','pd_rd_wg@amazon.','psc@amazon.',' _encoding@amazon.',
'wt_','WT.','yclid','referrer'
];
var anch = '';
var replace_semicolon = true; /* some sites use ";" to separate URL params;
URLSearchParams can't deal with that and messes up.
If those sites don't work with "&", either set this
to "false" or add that site on the exclude list
*/
function getRealLinkFromGoogleUrl(a) { /* adapted from https://github.com/Rob--W/dont-track-me-google at 2018-01-03 */
if ((a.hostname === location.hostname || a.hostname === 'www.google.com') &&
/^\/(local_)?url$/.test(a.pathname)) {
// Google Maps / Dito (/local_url?q=<url>)
// Mobile (/url?q=<url>)
var url = /[?&](?:q|url)=((?:https?|ftp)[%:][^&]+)/.exec(a.search);
if (url) {
return decodeURIComponent(url[1]);
}
// Help pages, e.g. safe browsing (/url?...&q=%2Fsupport%2Fanswer...)
url = /[?&](?:q|url)=((?:%2[Ff]|\/)[^&]+)/.exec(a.search);
if (url) {
return a.origin + decodeURIComponent(url[1]);
}
}
return a.href;
}
for(var i = 0; i < document.links.length; i++) {
var elem = document.links[i];
if (elem.search == '') continue;
if (replace_semicolon) var purl = new URLSearchParams(elem.search.replace(new RegExp(';','g'),'&'));
else var purl = new URLSearchParams(elem.search);
var keysToDelete = [];
for(var pair of purl) {
for(let b of badp) {
if (pair[0].startsWith(b)) { keysToDelete.push(pair[0]); break; }
}
}
for(var key of keysToDelete) {
purl.delete(key);
}
/* for (let b of badp) { purl.delete(b); }*/
if (elem.href.indexOf('#') > 0) anch = '#' + elem.href.split('#')[1];
else anch = '';
elem.href = elem.href.split('?')[0] + '?' + purl.toString() + anch;
elem.href = getRealLinkFromGoogleUrl(elem);
}