General URL Cleaner

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

Versione datata 22/08/2015. Vedi la nuova versione l'ultima versione.

Dovrai installare un'estensione come Tampermonkey, Greasemonkey o Violentmonkey per installare questo script.

Dovrai installare un'estensione come Tampermonkey o Violentmonkey per installare questo script.

Dovrai installare un'estensione come Tampermonkey o Violentmonkey per installare questo script.

Dovrai installare un'estensione come Tampermonkey o Userscripts per installare questo script.

Dovrai installare un'estensione come ad esempio Tampermonkey per installare questo script.

Dovrai installare un gestore di script utente per installare questo script.

(Ho già un gestore di script utente, lasciamelo installare!)

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

(Ho già un gestore di stile utente, lasciamelo installare!)

// ==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|ca|ac|ad|ae|al|am|us|as|at|az|ba|be|bf|bg|bi|bj|bs|bt|by|cc|cd|cf|cat|cg)\/.*$/
// @include        /^https?://[a-z]*.google.(ch|ci|cl|cm|cn|cv|cz|de|dj|dk|dm|dz|ee|es|fi|fm|fr|ga|ge|gg|gl|gm|gp|gr|gy|hn)\/.*$/
// @include        /^https?://[a-z]*.google.(iq|ie|im|io|is|it|je|jo|ki|kg|kz|hr|ht|hu|ir|la|li|lk|lt|lu|lv|md|me|mg|mk|ml)\/.*$/
// @include        /^https?://[a-z]*.google.(mn|ms|mu|mv|mw|ne|nl|no|nr|nu|pl|pn|ps|vg|pt|ro|rs|ru|rw|sc|se|sh|si|sk|sn|sm)\/.*$/
// @include        /^https?://[a-z]*.google.(so|st|td|tg|tk|tl|tm|to|tn|tt|vu|ws|com.(sg|sl|sv|tj|tn|tr|tw|ua|uy|vc|vn))\/.*$/
// @include        /^https?://[a-z]*.google.com.(au|af|ag|ai|ar|bh|bn|bo|br|bz|kh|co|cu|cy|do|ec|eg|et|fj|gh|gi|gt|hk|jm)\/.*$/
// @include        /^https?://[a-z]*.google.com.(kw|lb|lc|ly|mm|mt|mx|my|na|nf|ng|ni|np|om|pa|pe|ph|pk|pg|pr|py|qa|sa|sb)\/.*$/
// @include        /^https?://[a-z]*.google.co.(uk|nz|ao|ar|bw|ck|cr|id|il|in|jp|ke|kr|ls|ma|mz|th|tz|ug|uz|ve|vi|za|zm|zw)\/.*$/
// @include        /^https?://[a-z]*.amazon.(cn|in|co.jp|fr|de|it|nl|es|co.uk|ca|com.mx|com|com.au|com.br)\/.*$/
// @include        /^https?://[a-z]*.newegg.(com|ca|cn)\/.*$/
// @include        /^https?://[a-z]*.ebay.(com.au|at|be|ca|fr|de|com.hk|in|ie|co.il|it|com.my|nl|co.za|ph|pl|com.sg|co.za|es|ch|co.th|co.uk|com|vn)\/.*$/
// @include        /^https?://[a-z]*.bing.com\/.*$/
// @include        /^https?://[a-z]*.youtube.com\/.*$/
// @include        /^https?://[a-z]*.dealtime.com\/.*$/
// @exclude        https://apis.google.com/*
// @exclude        https://www.google.com/recaptcha/api2/*
// @version        2.0.0.3
// @license        GPL version 3 or any later version; http://www.gnu.org/copyleft/gpl.html
// ==/UserScript==

// -------- Variables -----------
// ------------------------------

// These regex's are for checking which site the URL belongs to
var includeLinks =    new RegExp(/(^https?|^ftp)\:\/\//);
var bingSearch =      new RegExp(/^https?:\/\/www\.bing\.(.+?)\/search\?/);
var youtubeVideo =    new RegExp(/^https?:\/\/www\.youtube\.com\/watch/);
var youtubeRedirect = new RegExp(/^https?:\/\/www\.youtube\.com\/redirect\?q\=/);
var ebayItem =        new RegExp(/^https?:\/\/www\.ebay\.(.+?)\/itm/);
var ebaySearch =      new RegExp(/^https?:\/\/www\.ebay\.(.+?)\/sch\//);
var amazonItemdp =    new RegExp(/^https?:\/\/www\.amazon\.(.+?)\/dp\//);
var amazonItemgp =    new RegExp(/^https?:\/\/www\.amazon\.(.+?)\/gp\/product\//);
var amazonGeneral =   new RegExp(/^https?:\/\/www\.amazon\.(.+?)\//);
var neweggItem =      new RegExp(/^http:\/\/www\.newegg\.(com|ca)\/Product\/Product\.aspx/);
var dealtime =        new RegExp(/http:\/\/stat\.dealtime\.com\/DealFrame\/DealFrame\.cmp\?/);
var googleRedirect =  new RegExp(/^https?:\/\/www\.google\.(.+?)\/url\?.*\&url\=/);
var googleSearch =    new RegExp(/^https?:\/\/[a-z]*\.google\.(.+?)\/[a-z]*\?.*$/);

//  These regex's are for replacing parts of the URL
var utmParameters =          new RegExp(/((\?|\&|)utm_(source|medium|campaign)\=[^&]*|\&amp\;)/g);
var googleSearchParameters = new RegExp(
    '\&(aqs|es_sm|channel|tab|num|hl|safe|tbo|sclient|sourceid|spell|site|sa|ei|client|complete|as_qdr|um|sa|tab|' +
    'authuser|rlz|cad|rct|ved|usg|source|oe|oq|ie|dpr|gs_l|ved|tbas|sei|biw|bih|gpsrc|gfe_rd|gws_rd)\=[^&]*', 'g');

// -------- Main section --------
// ------------------------------

if (bingSearch.test(document.URL)) {
    var newPageUrl = cleanBingSearch(document.URL);
    if (newPageUrl != document.URL) location.replace(newPageUrl);
    document.addEventListener("DOMContentLoaded", cleanLinksAll, false);
}
else if (googleRedirect.test(document.URL)) {
    var newPageUrl = cleanGoogleRedirect(document.URL);
    if (newPageUrl != document.URL) location.replace(newPageUrl);
}
else if (googleSearch.test(document.URL)) {
    var newPageUrl = cleanGoogleSearch(document.URL);
    if (newPageUrl != document.URL) location.replace(newPageUrl);
    document.addEventListener("DOMContentLoaded", cleanLinksGoogleSearch, false);
    window.onhashchange = googleInstant;
}
else if (youtubeVideo.test(document.URL)) {
    var newPageUrl = cleanYoutubeVideo(document.URL);
    if (newPageUrl != document.URL) location.replace(newPageUrl);
    document.addEventListener("DOMContentLoaded", cleanLinksYoutubeVideo, false);
}
else if (youtubeRedirect.test(document.URL)) {
    var newPageUrl = cleanYoutubeRedirect(document.URL);
    if (newPageUrl != document.URL) location.replace(newPageUrl);
}
else if (ebayItem.test(document.URL)) {
    var newPageUrl = cleanEbayItem(document.URL);
    if (newPageUrl != document.URL) location.replace(newPageUrl);
    document.addEventListener("DOMContentLoaded", cleanLinksEbay, false);
}
else if (ebaySearch.test(document.URL)) {
    var newPageUrl = cleanEbaySearch(document.URL);
    if (newPageUrl != document.URL) location.replace(newPageUrl);
    document.addEventListener("DOMContentLoaded", cleanLinksEbay, false);
}
else if (amazonItemdp.test(document.URL)) {
    var newPageUrl = cleanAmazonItemdp(document.URL);
    if (newPageUrl != document.URL) location.replace(newPageUrl);
    document.addEventListener("DOMContentLoaded", cleanLinksAmazonItem, false);
}
else if (amazonItemgp.test(document.URL)) {
    var newPageUrl = cleanAmazonItemgp(document.URL);
    if (newPageUrl != document.URL) location.replace(newPageUrl);
    document.addEventListener("DOMContentLoaded", cleanLinksAmazonItem, false);
}
else if (amazonGeneral.test(document.URL)) {
    document.addEventListener("DOMContentLoaded", cleanLinksAmazonItem, false);
}
else if (neweggItem.test(document.URL)) {
    var newPageUrl = cleanNeweggItem(document.URL);
    if (newPageUrl != document.URL) location.replace(newPageUrl);
    document.addEventListener("DOMContentLoaded", cleanLinksNeweggItem, false);
}
else if (dealtime.test(document.URL)) {
    var newPageUrl = cleanDealtime(document.URL);
    if (newPageUrl != document.URL) location.replace(newPageUrl);
}

// -------- Site-specific cleaning functions --------
// --------------------------------------------------

function cleanGoogleSearch(oldurl) {
    return oldurl.replace('?','?&').replace(googleSearchParameters,'').replace('?&','?').replace(/^http\:/,'https:');
}
function cleanGoogleRedirect(oldurl) {
    return decodeURIComponent(oldurl.replace(googleRedirect,'').replace(/\&(psig|ei|bvm)\=.*$/g,''));
}
function cleanBingSearch(oldurl) {
    return oldurl.replace('?','?&').replace(/\&(go|qs|form|FORM|filt|pq|sc|sp|sk|qpvt)\=[^&]*/g,'').replace('?&','?').replace(/^http\:/,'https:');
}
function cleanYoutubeVideo(oldurl) {
    return 'https://www.youtube.com/watch?' + oldurl.match(/v\=[^&]*/);
}
function cleanYoutubeRedirect(oldurl) {
    return decodeURIComponent(oldurl.replace(youtubeRedirect,'').replace(/\&redir_token\=.*/,''));
}
function cleanEbayItem(oldurl) {
    return 'http://' + oldurl.split('/')[2] + '/itm' + oldurl.match(/\/[0-9]{11,13}[^#?&\/]/);
}
function cleanEbaySearch(oldurl) {
    return oldurl.replace('?','?&').replace(/\&(\_osacat|\_odkw|\_from|rt|\_trksid|\_sacat)\=[^&]*/g,'').replace('?&','?');
}
function cleanAmazonItemgp(oldurl) {
    return 'http://' + oldurl.split('/')[2] + oldurl.match(/\/gp\/product\/[A-Z0-9]{10}\/?/);
}
function cleanAmazonItemdp(oldurl) {
    return 'http://' + oldurl.split('/')[2] + oldurl.match(/\/dp\/[A-Z0-9]{10}\/?/);
}
function cleanNeweggItem(oldurl) {
    return 'http://' + oldurl.split('/')[2] + oldurl.match(/\/Product\/Product\.aspx\?Item\=[^&]*/);
}
function cleanDealtime(oldurl) {
    return decodeURIComponent(oldurl.replace(/.*\&url\=/,'').replace(/(\%26|)\&linkin_id\=.*$/,'')).replace(/\&(url|partner)\=[^&]*/g,'');
}

// -------- Page link cleaning functions --------
// ----------------------------------------------

function cleanLinksGoogleSearch() {
    var links = document.links;
    for (var i = links.length; i--;) {
        if      (includeLinks.test(links[i].href))    links[i].removeAttribute('onmousedown'); // Results redirection
        if      (googleRedirect.test(links[i].href))  links[i].href = cleanGoogleRedirect(links[i].href);
        else if (googleSearch.test(links[i].href))    links[i].href = cleanGoogleSearch(links[i].href);
        else if (amazonItemgp.test(links[i].href))    links[i].href = cleanAmazonItemgp(links[i].href);
        else if (amazonItemdp.test(links[i].href))    links[i].href = cleanAmazonItemgp(links[i].href);
        else if (ebaySearch.test(links[i].href))      links[i].href = cleanEbaySearch(links[i].href);
        else if (ebayItem.test(links[i].href))        links[i].href = cleanEbayItem(links[i].href);
        else if (neweggItem.test(links[i].href))      links[i].href = cleanNeweggItem(links[i].href);
        else if (youtubeVideo.test(links[i].href))    links[i].href = cleanYoutubeVideo(links[i].href);
        else if (youtubeRedirect.test(links[i].href)) {
            links[i].href = cleanYoutubeRedirect(links[i].href);
            links[i].removeAttribute("class");
        }
    }
    this.removeEventListener('DOMContentLoaded', cleanLinksGoogleSearch, false);
}
function cleanLinksAll() {
    var links = document.links;
    for (var i = links.length; i--;) {
        if      (googleRedirect.test(links[i].href))  links[i].href = cleanGoogleRedirect(links[i].href);
        else if (googleSearch.test(links[i].href))    links[i].href = cleanGoogleSearch(links[i].href);
        else if (amazonItemgp.test(links[i].href))    links[i].href = cleanAmazonItemgp(links[i].href);
        else if (amazonItemdp.test(links[i].href))    links[i].href = cleanAmazonItemgp(links[i].href);
        else if (ebaySearch.test(links[i].href))      links[i].href = cleanEbaySearch(links[i].href);
        else if (ebayItem.test(links[i].href))        links[i].href = cleanEbayItem(links[i].href);
        else if (neweggItem.test(links[i].href))      links[i].href = cleanNeweggItem(links[i].href);
        else if (youtubeVideo.test(links[i].href))    links[i].href = cleanYoutubeVideo(links[i].href);
        else if (youtubeRedirect.test(links[i].href)) {
            links[i].href = cleanYoutubeRedirect(links[i].href);
            links[i].removeAttribute("class");
        }
    }
    this.removeEventListener('DOMContentLoaded', cleanLinksAll, false);
}
function cleanLinksAmazonItem() {
    var links = document.links;
    for (var i = links.length; i--;) {
        if      (amazonItemgp.test(links[i].href))    links[i].href = cleanAmazonItemgp(links[i].href);
        else if (amazonItemdp.test(links[i].href))    links[i].href = cleanAmazonItemgp(links[i].href);
    }
    this.removeEventListener('DOMContentLoaded', cleanLinksAmazonItem, false);
}
function cleanLinksEbay() {
    var links = document.links;
    for (var i = links.length; i--;) {
        if      (ebaySearch.test(links[i].href))      links[i].href = cleanEbaySearch(links[i].href);
        else if (ebayItem.test(links[i].href))        links[i].href = cleanEbayItem(links[i].href);
    }
    this.removeEventListener('DOMContentLoaded', cleanLinksEbay, false);
}
function cleanLinksYoutubeVideo() {
    var links = document.links;
    for (var i = links.length; i--;) {
        if      (youtubeVideo.test(links[i].href))    links[i].href = cleanYoutubeVideo(links[i].href);
        else if (youtubeRedirect.test(links[i].href)) {
            links[i].href = cleanYoutubeRedirect(links[i].href);
            links[i].removeAttribute("class");
        }
    }
    this.removeEventListener('DOMContentLoaded', cleanLinksYoutubeVideo, false);
}
function cleanLinksNeweggItem() {
    var links = document.links;
    for (var i = links.length; i--;) {
        if      (neweggItem.test(links[i].href))      links[i].href = cleanNeweggItem(links[i].href);
    }
    this.removeEventListener('DOMContentLoaded', cleanLinksNeweggItem, false);
}

// -------- Extra functions --------
// ---------------------------------

// Google Instant document URL cleaning
// This is called whenever the search terms change
function googleInstant() {
    if (!document.URL.includes('#imgrc=')) { // Don't rewrite anything if an image is clicked in image searches
        var newSearchTerms = String(document.URL.match(/\#.*/)).replace(/^\#/,'');
        var newSearchUrl = String(document.URL.replace(/search\?.*/, 'search?' + newSearchTerms));
        location.replace(newSearchUrl);
    }
}