// ==UserScript==
// @run-at document-start
// @name General URL Cleaner
// @namespace
// @description Cleans URL's from various popular sites. Also, makes sure the sites are using HTTPS.
// @include /^https?://[a-z]+\.google(\.com?)?\.[a-z]{2,3}/.*$/
// @include /^https?://www\.amazon(\.com?)?\.[a-z]{2,3}/.*$/
// @include /^https?://www\.newegg.c(om|a)/.*$/
// @include /^https?://[a-z]+\.ebay(\.com?)?\.[a-z]{2,3}/.*$/
// @include /^https?://www\.bing\.com/*$/
// @include /^https?://www\.youtube\.com/*$/
// @include http://stat.dealtime.com/*
// @exclude https://apis.google.com/*
// @exclude https://www.google.com/recaptcha/api2/*
// @version 2.2.2.0
// @license GPL version 3 or any later version; http://www.gnu.org/copyleft/gpl.html
// ==/UserScript==
// -------- Pre-compile regexes -----------
var bing = /^https?:\/\/www\.bing\.com\/(profile\/)?[a-z]*\?/;
var ebay = /^https?:\/\/[a-z]+\.ebay(\.com?)?\.[a-z]{2,3}/;
var amazon = /^https?:\/\/www\.amazon(\.com?)?\.[a-z]{2,3}\//;
var newegg = /^http:\/\/www\.newegg\.c(om|a)/;
var youtube = /^https?:\/\/www\.youtube\.com/;
var google = /^https?:\/\/[a-z]*\.google(\.com?)?\.[a-z]{2,3}\//;
var dealtime = /http:\/\/stat\.dealtime\.com\/DealFrame\/DealFrame\.cmp\?/;
var utmParameters = /([?&]?utm_[a-z]+=[^&#]*|&)/g;
var googleSearchParameters = new RegExp(
'&(aqs|as_qdr|authuser|bav|bi[wh]]|bs|bvm|cad|channel|s?client|complete|cp|dpr|es_sm|g(fe|ws)_rd|gpsrc|h[ls]|ie|n?um'+
'|btnG|o[eq]|pbx|p[fq]]|rct|rlz|sa(fe)?|s?ei|site|source(id)?|spell|tab|tbas|tbo|usg|ved|xhr|gs_(l|r[ni]]|mss|id))=[^&]*','g'
);
// -------- Main --------
var doc = document;
if (bing.test(doc.URL)) {
var newUrl = cleanBingSearch(doc.URL);
if (/^http:/.test(newUrl)) location.href = newUrl.replace(/^http:/,'https:');
else cleanPageUrl(newUrl);
cleanLinks('all');
}
else if (google.test(doc.URL)) {
if (doc.URL.includes('/url?')) location.replace(cleanGoogleRedirect(doc.URL));
else if (doc.URL.includes('/imgres?imgurl=')) location.replace(cleanGoogleImageRedirect(doc.URL));
else if (/\.[a-z]{2,3}\/[a-z]*\?/.test(doc.URL)) {
cleanPageUrl(cleanGoogleSearch(doc.URL));
cleanLinks('google');
googleInstant();
}
}
else if (youtube.test(doc.URL)) {
if (doc.URL.includes('/watch/')) cleanPageUrl(cleanYoutubeVideo(doc.URL));
else if (doc.URL.includes('redirect?')) location.replace(cleanYoutubeRedirect(doc.URL));
cleanLinks('youtube');
}
else if (ebay.test(doc.URL)) {
if (doc.URL.includes('/itm/')) cleanPageUrl(cleanEbayItem(doc.URL));
else if (doc.URL.includes('/sch/')) cleanPageUrl(cleanEbaySearch(doc.URL));
cleanLinks('ebay');
}
else if (amazon.test(doc.URL)) {
if (doc.URL.includes('/dp/')) cleanPageUrl(cleanAmazonItemdp(doc.URL));
else if (doc.URL.includes('/gp/product')) cleanPageUrl(cleanAmazonItemgp(doc.URL));
cleanLinks('amazon');
}
else if (newegg.test(doc.URL)) {
if (doc.URL.includes('/Product/')) cleanPageUrl(cleanNeweggItem(doc.URL));
cleanLinks('newegg');
}
else if (dealtime.test(doc.URL)) {
location.replace(cleanDealtime(doc.URL));
}
// -------- Front functions --------
function cleanPageUrl(newUrl) {
if (newUrl != doc.URL) history.replaceState(null,null,newUrl);
}
function cleanLinks(site) { new MutationObserver(function(M) { M.forEach(function(m) {
links = m.target.getElementsByTagName('a');
for (i=links.length; i--;) {
linkCleaners[site](links[i]);
}
});}).observe(doc,{childList:true,attributes:true,attributeFilter:['href'],subtree:true});}
function googleInstant() { new MutationObserver(function(_,self){
var search = doc.getElementById('lst-ib');
if (search) {
new MutationObserver( function() {
if (doc.URL.includes('#') && !doc.URL.includes('#imgrc='))
history.replaceState(null,null,cleanGoogleSearch(doc.URL.match(/.+?\?/)+doc.URL.match(/#(.*)/)[1]));
}).observe(search,{childList:true,attributes:true});
self.disconnect();
}
}).observe(doc,{childList:true,subtree:true});}
// -------- URL cleaning functions --------
function cleanGoogleSearch(url) {
return url.replace('?','?&').replace(googleSearchParameters,'').replace('?&','?').replace(/^http\:/,'https:');
}
function cleanGoogleRedirect(url) {
return decodeURIComponent(url.replace(/.+\/url\?.*(url|q)=/,'').replace(/&(rct|psig|ei|bvm|sa)=.*$/g,''));
}
function cleanGoogleImageRedirect(url) {
return decodeURIComponent(url.replace(/(.+?)\?imgurl=/,'').replace(/&imgrefurl=.*/,''));
}
function cleanBingSearch(url) {
return url.replace('?','?&').replace(/&(go|qs|form|FORM|filt|pq|s[cpk]|qpvt)=[^&]*/g,'').replace('?&','?');
}
function cleanYoutubeVideo(url) {
return url.replace("?","?&").replace(/&(feature|src_vid|annotation_id|[gh]l)=[^&]*/g,'').replace("?&","?").replace(/^http:/,'https:');
}
function cleanYoutubeRedirect(url) {
return decodeURIComponent(url.replace(/(.+?)\?q=/,'').replace(/&redir_token=.*/,''));
}
function cleanEbayItem(url) {
return 'http://' + url.split('/')[2] + '/itm' + url.match(/\/[0-9]{11,13}/) + (url.match(/#[A-Za-z]+$/)||'');
}
function cleanEbaySearch(url) {
return url.replace('?','?&').replace(/&(_o?sacat|_odkw|_from|_trksid|rt)=[^&]*/g,'').replace('?&','?');
}
function cleanAmazonItemgp(url) {
return 'http://' + url.split('/')[2] + url.match(/\/gp\/product\/[A-Z0-9]{10}/);
}
function cleanAmazonItemdp(url) {
return 'http://' + url.split('/')[2] + url.match(/\/dp\/[A-Z0-9]{10}/);
}
function cleanNeweggItem(url) {
return 'http://' + url.split('/')[2] + '/Product/Product.aspx' + url.match(/\?Item=[^&]*/);
}
function cleanDealtime(url) {
return decodeURIComponent(url.replace(/.*&url=/,'').replace(/(%26)?&linkin_id=.*$/,'')).replace(/&(url|partner)=[^&]*/g,'');
}
// -------- Link cleaning functions --------
var linkCleaners = {
all:function(a) {
wbr = a.getElementsByTagName('wbr');
for (k = wbr.length; k--;) a.removeChild(wbr[k]);
linkCleaners._google(a); linkCleaners._youtube(a); linkCleaners.utm(a);
linkCleaners.amazon(a); linkCleaners.ebay(a); linkCleaners.newegg(a);
},
amazon:function(a) { if (amazon.test(a.href)) {
if (a.href.includes('/dp/')) a.href = cleanAmazonItemdp(a.href);
else if (a.href.includes('/gp/product')) a.href = cleanAmazonItemgp(a.href);
}},
ebay:function(a) { if (ebay.test(a.href)) {
if (a.href.includes('/itm/')) a.href = cleanEbayItem(a.href);
else if (a.href.includes('/sch/')) a.href = cleanEbaySearch(a.href);
}},
newegg:function(a) { if (newegg.test(a.href)) {
if (a.href.includes('/Product/')) a.href = cleanNeweggItem(a.href);
}},
_google:function(a) { if (google.test(a.href)) {
if (a.href.includes('/url=')) a.href = cleanGoogleRedirect(a.href);
else if (/\/[a-z]+\?/.test(a.href)) a.href = cleanGoogleSearch(a.href);
}},
_youtube:function(a) { if (youtube.test(a.href)) {
if (a.href.includes('/watch/')) a.href = cleanYoutubeVideo(a.href);
else if (a.href.includes('/redirect?')) a.href = cleanGoogleRedirect(a.href);
}},
utm:function(a) { if (utmParameters.test(a.href)) {
url = a.href.replace(utmParameters,'');
if (a.innerText==a.href) a.innerText = url;
if (a.title==a.href) a.title = url;
a.href = url;
}},
google:function(a) {
a.removeAttribute('onmousedown');
linkCleaners.all(a);
},
youtube:function(a) {
linkCleaners._youtube(a);
if (/yt-uix-redirect-link/.test(a.className)) {
a.removeAttribute('class');
if (a.title == a.href) {
linkCleaners.all(a);
a.title = a.href;
}
else linkCleaners.all(a);
}
else if (/spf-link/.test(a.className)) {
a.className = 'content-link';
a.removeAttribute('data-sessionlink');
a.removeAttribute('rel');
}
}
};