General URL Cleaner

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

Från och med 2015-05-30. Se den senaste versionen.

// ==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        *
// @exclude        https://apis.google.com/*
// @exclude        https://www.google.com/recaptcha/api2/*
// @version        1.8.5
// @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 ebaySearch = new RegExp(/^https?:\/\/www\.ebay\.(.+?)\/sch\//);
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", cleanPageLinks, false); // General cleaning
if (google.test(newPageUrl)) window.onhashchange = googleInstant;     // Remove Google search results redirection

// Generic link cleaning function
function cleanPageLinks() {
    var links = document.links;
    for (var i = links.length; i--;)
        if (links[i].href != '' && !links[i].href.startsWith('javascript:') &&  !links[i].href.startsWith('data:'))
            links[i].href = cleanUrl(links[i].href);
    this.removeEventListener('DOMContentLoaded', cleanPageLinks, false);
}

// Google Instant document URL cleaning - 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'
function googleInstant() {
    if (!document.URL.includes('#imgrc=')) {                                                  // Don't rewrite anything if an image is clicked in image searches
        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);
    }
}

// Main function for cleaning the url's
function cleanUrl(oldurl) {
    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 ebaySearch.test(oldurl):
            newurl = oldurl.replace('?','?&') // temporarily put an "&" after the "?" so that the regex below will always match
                           .replace(/\&(\_osacat|\_odkw|\_from|rt|\_trksid|\_sacat)\=[^&]*/g,'')
                           .replace('?&','?')
            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;
}