General URL Cleaner

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

Versione datata 29/05/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        *
// @version        1.7.7
// @license       GPL version 3 or any later version; http://www.gnu.org/copyleft/gpl.html
// ==/UserScript==

// compile these regexes beforehand to improve efficiency
var bing = new RegExp(/^https?:\/\/www\.bing\.(.+?)\/search\?/);
var google = new RegExp(/^https?:\/\/(www|mail|maps|books|play|news|plus|photos|docs|drive|support)\.google\.(.+?)\/[a-z]*\?/);
var youtube = new RegExp(/^https?:\/\/www\.youtube\.com\/watch/);
var ebay = new RegExp(/^https?:\/\/www\.ebay\.(.+?)\/itm/);
var amazon = new RegExp(/^https?:\/\/www\.amazon\..*\/dp\//);
var newegg = new RegExp(/^http:\/\/www\.newegg\.(com|ca)\/Product\/Product\.aspx/);
var dealtime = new RegExp(/http:\/\/stat\.dealtime\.com\/DealFrame\/DealFrame\.cmp\?/);
var newPageUrl = cleanUrl(document.URL);

// Clean the current page URL
if (newPageUrl != document.URL) location.replace(newPageUrl);

// Cleans links on the page
document.addEventListener("DOMContentLoaded", function cleanPageLinks() {
    var links = document.links;

    // Google search results pages
    if (google.test(newPageUrl)) {
        for (var i = 0; i < links.length; i++)
            if (links[i].href != '' && !links[i].href.startsWith('javascript:')) { // Don't do anything with blank or javascript links
                links[i].href = cleanUrl(links[i].href);                           // General cleaning
                links[i].removeAttribute('onmousedown');                           // Remove Google search results redirection
            }

        // Google Instant: if the search terms change, remove the extra stuff.
        // Eg: 'google.com/search?q=old+search+query#q=new+search+query' -> 'google.com/search?q=new+search+query'
        window.onhashchange = function googleInstant() {
            if (!document.URL.includes('#imgrc=')) {                                                        // don't rewrite anything if an image is clicked in an image search
                var newSearchString = String(document.URL.match(/\#.*/)).replace(/^\#/,'');                 // The string after the hash, containing the new search terms
                var newSearchUrl = String(document.URL.replace(/search\?.*/, 'search?' + newSearchString)); // Remake the full URL with only the new search terms
                location.replace(newSearchUrl);
            }
        };
        
    }

    // All other websites
    else
        for (var i = 0; i < links.length; i++)
            if (links[i].href != '' && !links[i].href.startsWith('javascript:'))
                links[i].href = cleanUrl(links[i].href);
}, false);

function cleanUrl(oldurl) { // Main function for cleaning the url's
    var newurl = oldurl;
    switch(true) {
        case google.test(oldurl):
            newurl = oldurl.replace('?','?&') // temporarily put an "&" after the "?" so that the regex below will always match
                           .replace(/\&(tab|num|hl|safe|tbo|sclient|sourceid|spell|client|complete|as_qdr|um|sa|tab|authuser|rlz|cad|rct|ved|usg|site|source|oe|oq|sa|ei|ie|dpr|gs\_l|ved|tbas|sei|biw|bih|gpsrc|gfe_rd|gws_rd)\=[^&]*/g,'')
                           .replace('?&','?')
                           .replace(/^http\:/,'https:'); // always use https
            break;
        case bing.test(oldurl):
            newurl = oldurl.replace('?','?&')
                           .replace(/\&(go|qs|form|FORM|filt|pq|sc|sp|sk|qpvt)\=[^&]*/g,'')
                           .replace('?&','?')
                           .replace(/^http\:/,'https:');
            break;
        case youtube.test(oldurl):
            newurl = 'https://www.youtube.com/watch?' + oldurl.match(/v\=[^&]*/);
            break;
        case ebay.test(oldurl):
            newurl = 'http://' + oldurl.split('/')[2] + '/itm' + oldurl.match(/\/[0-9]{11,13}[^#?&\/]/); // the split gets the domain name. Should be more efficient than a regex.
            break;
        case amazon.test(oldurl):
            newurl = 'https://' + oldurl.split('/')[2] + oldurl.match(/\/dp\/[A-Z0-9]{10}/);
            break;
        case newegg.test(oldurl):
            newurl = 'http://' + oldurl.split('/')[2] + oldurl.match(/\/Product\/Product\.aspx\?Item\=[^&]*/);
            break;
        case dealtime.test(oldurl):
            newurl = decodeURIComponent(oldurl.replace(/.*\&url\=/,'').replace(/(\%26|)\&linkin_id\=.*$/,'')).replace(/\&(url|partner)\=[^&]*/g,'');
            break;
        default:
            break;
    }
    newurl = newurl.replace(/((\?|\&|)utm_(source|medium|campaign)\=[^&]*|\&amp\;)/g,'');
    return newurl;
}