General URL Cleaner

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

Stan na 29-05-2015. Zobacz najnowsza wersja.

Aby zainstalować ten skrypt, wymagana jest instalacje jednego z następujących rozszerzeń: Tampermonkey, Greasemonkey lub Violentmonkey.

Aby zainstalować ten skrypt, wymagana jest instalacje jednego z następujących rozszerzeń: Tampermonkey, Violentmonkey.

Aby zainstalować ten skrypt, wymagana jest instalacje jednego z następujących rozszerzeń: Tampermonkey, Violentmonkey.

Aby zainstalować ten skrypt, wymagana będzie instalacja rozszerzenia Tampermonkey lub Userscripts.

You will need to install an extension such as Tampermonkey to install this script.

Aby zainstalować ten skrypt, musisz zainstalować rozszerzenie menedżera skryptów użytkownika.

(Mam już menedżera skryptów użytkownika, pozwól mi to zainstalować!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

Będziesz musiał zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

Będziesz musiał zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

Musisz zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

(Mam już menedżera stylów użytkownika, pozwól mi to zainstalować!)

// ==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;
}