General URL Cleaner

Cleans URL's from various popular sites. Also, makes sure the sites are using HTTPS.

Per 23-08-2016. Zie de nieuwste versie.

Voor het installeren van scripts heb je een extensie nodig, zoals Tampermonkey, Greasemonkey of Violentmonkey.

Voor het installeren van scripts heb je een extensie nodig, zoals Tampermonkey of Violentmonkey.

Voor het installeren van scripts heb je een extensie nodig, zoals Tampermonkey of Violentmonkey.

Voor het installeren van scripts heb je een extensie nodig, zoals Tampermonkey of Userscripts.

Voor het installeren van scripts heb je een extensie nodig, zoals {tampermonkey_link:Tampermonkey}.

Voor het installeren van scripts heb je een gebruikersscriptbeheerder nodig.

(Ik heb al een user script manager, laat me het downloaden!)

Voor het installeren van gebruikersstijlen heb je een extensie nodig, zoals {stylus_link:Stylus}.

Voor het installeren van gebruikersstijlen heb je een extensie nodig, zoals {stylus_link:Stylus}.

Voor het installeren van gebruikersstijlen heb je een extensie nodig, zoals {stylus_link:Stylus}.

Voor het installeren van gebruikersstijlen heb je een gebruikersstijlbeheerder nodig.

Voor het installeren van gebruikersstijlen heb je een gebruikersstijlbeheerder nodig.

Voor het installeren van gebruikersstijlen heb je een gebruikersstijlbeheerder nodig.

(Ik heb al een beheerder - laat me doorgaan met de installatie!)

// ==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/*
// @include     http://www.imdb.com/*
// @exclude     https://apis.google.com/*
// @exclude     https://www.google.com/recaptcha/api2/*
// @version     2.5
// @license     GPL version 3 or any later version; http://www.gnu.org/copyleft/gpl.html
// ==/UserScript==
var doc = document; var numLinks = 0;
var docDomain = doc.URL.split('/')[2];
var ebay = /^[a-z]+\.ebay(\.com?)?\.[a-z]{2,3}$/;
var amazon = /^www\.amazon(\.com?)?\.[a-z]{2,3}$/;
var google = /^[a-z]+\.google(\.com?)?\.[a-z]{2,3}$/;
var amazonParams = /&(ie|pf_rd_[a-z]|bbn|rw_html_to_wsrp)=[^&#]*/;
var utmParams = /([?&#]utm_[a-z]+=[^&#]*|&)/g;
var bingParams = /&(go|qs|form|FORM|filt|pq|s[cpk]|qpvt|cvid)=[^&]*/g;
var youtubeParams = /&(feature|src_vid|annotation_id|[gh]l)=[^&]*/g;
var ebayParams = /&(_(o?sacat|odkw|from|trksid)|rt)=[^&]*/g;
var googleParams = /&(aqs|as_qdr|authuser|bav|bi[wh]|bs|bvm|cad|channel|complete|cp|s?client|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 --------
if (docDomain=='www.bing.com') {
    var newUrl = cleanBingSearch(doc.URL);
    if (doc.URL.startsWith('http:')) location.href = newUrl;
    else cleanPageUrl(newUrl);
    cleanLinks('all');
}
else if (docDomain=='www.youtube.com') {
    if (doc.URL.includes('/watch?')||doc.URL.includes('/watch/')) cleanPageUrl(cleanYoutubeVideo(doc.URL));
    else if (doc.URL.includes('redirect?')) location.replace(cleanYoutubeRedirect(doc.URL));
    cleanLinks('youtube');
}
else if (docDomain=='www.newegg.com' || docDomain=='www.newegg.ca') {
    if (doc.URL.includes('/Product/Product.aspx')) cleanPageUrl(cleanNeweggItem(doc.URL));
    cleanLinks('newegg');
}
else if (docDomain=='www.imdb.com') {
    if (doc.URL.includes('?')) cleanPageUrl(cleanImdb(doc.URL));
    cleanLinks('imdb');
}
else if (google.test(docDomain)) {
    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',true);
        googleInstant();
    }
}
else if (ebay.test(docDomain)) {
    if (doc.URL.includes('/itm/')) cleanPageUrl(cleanEbayItem(doc.URL));
    else if (doc.URL.includes('/sch/')||doc.URL.includes('/dsc/')) cleanPageUrl(cleanEbaySearch(doc.URL));
    cleanLinks('ebay');
}
else if (amazon.test(docDomain)) {
    if (doc.URL.includes('/dp/')) cleanPageUrl(cleanAmazonItemdp(doc.URL));
    else if (doc.URL.includes('/gp/product')) cleanPageUrl(cleanAmazonItemgp(doc.URL));
    else if (doc.URL.includes('?')) cleanAmazonParams(doc.URL);
    cleanLinks('amazon');
}
else if (doc.URL.startsWith('http://stat.dealtime.com/DealFrame/DealFrame.cmp?')) {
    location.replace(cleanDealtime(doc.URL));
}

// -------- Front functions --------
function cleanPageUrl(newUrl) {
    if (newUrl != doc.URL) history.replaceState(null,null,newUrl);
}

function cleanLinks(site, remain=false) { new MutationObserver(function(_,self) {
    links = doc.getElementsByTagName("a");
    if (links.length>numLinks) { numLinks=links.length;
        for (var i=numLinks; 0<i--;) { a = links[i];
            try { if (a.href.startsWith('http')) {
                old = a.href;
                linkCleaners[site](a);
                if (a.innerText==old) a.innerText=a.href;
                if (a.title==old) a.title=a.href;
            }} catch(TypeError) {}
        }
    }
    if (!remain && doc.readyState=='complete') setTimeout(function(){self.disconnect();}, 20000);
}).observe(doc,{childList:true,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=')) {
            newUrl = baseUrl(doc.URL) + cleanGoogleSearch((doc.URL.match(/\/[a-z]*\?/)||'/search?')+(doc.URL.match(/#(.*)/)[1]||doc.URL.match(/\?(.*)#/)[1]));
            history.replaceState(null,null,newUrl);
        }}).observe(search,{childList:true,attributes:true});
        self.disconnect();
    }
}).observe(doc,{childList:true,subtree:true});}

// -------- URL cleaning functions --------
function cleanGoogleSearch(url) {
    return url.replace('?','?&').replace(googleParams,'').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(bingParams,'').replace('?&','?').replace(/^http\:/,'https:');
}
function cleanYoutubeVideo(url) {
    return url.replace("?","?&").replace(youtubeParams,'').replace("?&","?");
}
function cleanYoutubeRedirect(url) {
    return decodeURIComponent(url.replace(/(.+?)\?q=/,'').replace(/&redir_token=.*/,''));
}
function cleanEbayItem(url) {
    return baseUrl(url)+'/itm'+url.match(/\/[0-9]{11,13}/)+(url.match(/#[A-Za-z]+$/)||'');
}
function cleanEbaySearch(url) {
    return url.replace('?','?&').replace(ebayParams,'').replace('?&','?');
}
function cleanAmazonItemgp(url) {
    return baseUrl(url)+url.match(/\/gp\/product\/[A-Z0-9]{10}/)+(url.match(/#[A-Za-z]+$/)||'');
}
function cleanAmazonItemdp(url) {
    return baseUrl(url)+url.match(/\/dp\/[A-Z0-9]{10}/)+(url.match(/#[A-Za-z]+$/)||'');
}
function cleanAmazonParams(url) {
    return url.replace('?','?&').replace(amazonParams,'').replace('?&','?');
}
function cleanNeweggItem(url) {
    return baseUrl(url)+'/Product/Product.aspx'+url.match(/\?Item=[^&]*/);
}
function cleanDealtime(url) {
    return decodeURIComponent(url.replace(/.*&url=/,'').replace(/(%26)?&linkin_id=.*$/,'')).replace(/&(url|partner)=[^&]*/g,'');
}
function cleanImdb(url) {
    return url.replace('?','?&').replace(/&(pf_rd_[a-z]|ref_)=[^&#]*/,'').replace('?&','?').replace(/\?$/,'');
}
function baseUrl(url) {
    d = url.split('/'); return d[0]+'//'+d[2];
}

// -------- Link cleaning functions --------
var linkCleaners = {
    all:function(a) {
        var d=a.href.split('/')[2];
        if (google.test(d))
            if (a.href.includes('/url=')) a.href = cleanGoogleRedirect(a.href);
            else if (/\/[a-z]+\?/.test(a.href)) a.href = cleanGoogleSearch(a.href);
        else if (d=='www.youtube.com')
            if (a.href.includes('/watch/')) a.href = cleanYoutubeVideo(a.href);
            else if (a.href.includes('/redirect?')) a.href = cleanGoogleRedirect(a.href);
        else if (a.href.includes('/Product/Product.aspx') && (d=='www.newegg.com'||d=='www.newegg.ca'))
            a.href = cleanNeweggItem(a.href);
        else if (d=='www.imdb.com' && a.href.includes('?'))
            a.href = cleanImdb(a.href);
        else if (amazon.test(d))
            if (a.href.includes('/dp/')) a.href = cleanAmazonItemdp(a.href);
            else if (a.href.includes('/gp/product')) a.href = cleanAmazonItemgp(a.href);
            else if (a.href.includes('?')) a.href = cleanAmazonParams(a.href);
        else if (ebay.test(d))
            if (a.href.includes('/itm/')) a.href = cleanEbayItem(a.href);
            else if (a.href.includes('/sch/')) a.href = cleanEbaySearch(a.href);
        a.href = a.href.replace(utmParams,'');
    },
    amazon:function(a) {
        if (amazon.test(a.href.split('/')[2]))
            if (a.href.includes('/dp/')) a.href = cleanAmazonItemdp(a.href);
            else if (a.href.includes('/gp/product')) a.href = cleanAmazonItemgp(a.href);
            else if (a.href.includes('?')) a.href = cleanAmazonParams(a.href);
    },
    ebay:function(a) {
        if (ebay.test(a.href.split('/')[2]))
            if (a.href.includes('/itm/')) a.href = cleanEbayItem(a.href);
            else if (a.href.includes('/sch/')) a.href = cleanEbaySearch(a.href);
    },
    newegg:function(a) {
        var d=a.href.split('/')[2];
        if (a.href.includes('/Product/Product.aspx') && (d=='www.newegg.com'||d=='www.newegg.ca'))
            a.href = cleanNeweggItem(a.href);
    },
    google:function(a) {
        a.removeAttribute('onmousedown');
        linkCleaners.all(a);
    },
    youtube:function(a) {
        linkCleaners.all(a);
        a.classList.remove('yt-uix-redirect-link');
        a.classList.remove('spf-link');
        a.removeAttribute('data-sessionlink');
    },
    imdb:function(a){
        var d=a.href.split('/')[2];
        if (d=='www.imdb.com' && a.href.includes('?')) a.href = cleanImdb(a.href);
    }
};