General URL Cleaner Test

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

Fra 19.05.2016. Se den seneste versjonen.

// ==UserScript==
// @run-at document-start
// @name           General URL Cleaner Test
// @namespace      
// @description    Cleans URL's from various popular sites. Also, makes sure the sites are using HTTPS.
// @include        /^https?://[a-z]+.google.(a[cdelmstz]|b[aefgijsty]|c[acdfghilmnvz]|cat|com|d[ejkmz]|e[es]|f[imr]|g[aeglmpry]|h[nrtu]|i[emoqrst]|j[eo]|k[giz]|l[aiktuv]|m[degklnsuvw]|n[eloru]|p[lnst]|r[osuw]|s[cehikmnot]|t[dgklmnot]|us|v[gu]|ws)/.*$/
// @include        /^https?://[a-z]+.google.com.(a[fgiru]|b[hnorz]|c[ouy]|do|e[cgt]|fj|g[hit]|hk|jm|k[hw]|l[bcy]|m[mtxy]|n[afgip]|om|p[aeghkry]|qa|s[abglv]|t[jnrw]|u[ay]|v[cn])/.*$/
// @include        /^https?://[a-z]+.google.co.(a[or]|bw|c[kr]|i[dln]|jp|k[er]|ls|m[az]|nz|t[hz]|u[gkz]|v[ei]|z[amw])/.*$/
// @include        /^https?://[a-z]+.amazon.(co.(jp|uk)|com.(au|br|mx)|c[an]|com|de|es|fr|i[nt]|nl)/.*$/
// @include        /^https?://[a-z]+.newegg.(com|ca)/.*$/
// @include        /^https?://[a-z]+.ebay.(at|be|c[ah]|com|de|es|fr|i[ent]|nl|p[hl]|vn|co.(il|th|uk|za)|com.(au|hk|my|sg))/.*$/
// @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.1
// @license        GPL version 3 or any later version; http://www.gnu.org/copyleft/gpl.html
// ==/UserScript==

// -------- Pre-compile regexes -----------
var bingSearch =             new RegExp(/^https?:\/\/www\.bing\.com\/(|profile\/)[a-z]*\?/);
var bingGeneral =            new RegExp(/^https?:\/\/www\.bing\.com\//);
var youtubeVideo =           new RegExp(/^https?:\/\/www\.youtube\.com\/watch/);
var youtubeRedirect =        new RegExp(/^https?:\/\/www\.youtube\.com\/redirect\?q\=(?=.*\&redir_token\=)/);
var ebayItem =               new RegExp(/^https?:\/\/www\.ebay\.([a-z.]+?)\/itm/);
var ebaySearch =             new RegExp(/^https?:\/\/www\.ebay\.([a-z.]+?)\/sch\//);
var ebayGeneral =            new RegExp(/^https?:\/\/[a-z]+\.ebay\.([a-z.]+?)/);
var amazonItemdp =           new RegExp(/^https?:\/\/www\.amazon\.(.+?)\/dp\//);
var amazonItemgp =           new RegExp(/^https?:\/\/www\.amazon\.(.+?)\/gp\/product\//);
var amazonGeneral =          new RegExp(/^https?:\/\/www\.amazon\.([a-z.]+?)\//);
var neweggItem =             new RegExp(/^http:\/\/www\.newegg\.(com|ca)\/Product\/Product\.aspx/);
var neweggGeneral =          new RegExp(/^http:\/\/www\.newegg\.(com|ca)/);
var googleRedirect =         new RegExp(/^https?:\/\/www\.google\.([a-z.]+?)\/url\?((|(.+?)\&)url\=|q\=)/);
var googleImageRedirect =    new RegExp(/^https?:\/\/www\.google\.([a-z.]+?)\/imgres\?imgurl\=/);
var googleSearch =           new RegExp(/^https?:\/\/[a-z]*\.google\.([a-z.]+?)\/[a-z]*\?.*$/);
var utmParameters =          new RegExp(/((\?|\&|)utm_(source|medium|campaign)\=[^&]*|\&amp\;)/g);
var dealtime =               new RegExp(/http:\/\/stat\.dealtime\.com\/DealFrame\/DealFrame\.cmp\?/);
var googleSearchParameters = new RegExp('\&(aqs|as_qdr|authuser|bav|bi[wh]]|bs|bvm|cad|channel|(|s)client|complete|cp|dpr|ei|es_sm|g(fe|ws)_rd|gpsrc|h[ls]|ie|num' +
                                        '|o[eq]|pbx|pf|pq|rct|rlz|sa|safe|sei|site|source(|id)|spell|tab|tbas|tbo|um|usg|ved|xhr|gs_(l|rn|ri|mss|id))\=[^&]*', 'g');

// -------- Main --------
if (bingSearch.test(document.URL)) {
    var newPageUrl = cleanBingSearch(document.URL);
    if (/^http:/.test(newPageUrl)) location.href = newPageUrl.replace(/^http:/,"https:");
    else cleanPageUrl(newPageUrl);
    cleanLinks('all');
}
else if (googleRedirect.test(document.URL)) {
    location.replace(cleanGoogleRedirect(document.URL));
}
else if (googleImageRedirect.test(document.URL)) {
    location.replace(cleanGoogleImageRedirect(document.URL));
}
else if (googleSearch.test(document.URL)) {
    cleanPageUrl(cleanGoogleSearch(document.URL));
    cleanLinks('google');
    window.onhashchange = googleInstant;
}
else if (youtubeVideo.test(document.URL)) {
    cleanPageUrl(cleanYoutubeVideo(document.URL));
    cleanLinks('youtube');
}
else if (youtubeRedirect.test(document.URL)) {
    location.replace(cleanYoutubeRedirect(document.URL));
}
else if (ebayItem.test(document.URL)) {
    cleanPageUrl(cleanEbayItem(document.URL));
    cleanLinks('ebay');
}
else if (ebaySearch.test(document.URL)) {
    cleanPageUrl(cleanEbaySearch(document.URL));
    cleanLinks('ebay');
}
else if (ebayGeneral.test(document.URL)) {
    cleanLinks('ebay');
}
else if (amazonItemdp.test(document.URL)) {
    cleanPageUrl(cleanAmazonItemdp(document.URL));
    cleanLinks('amazon');
}
else if (amazonItemgp.test(document.URL)) {
    cleanPageUrl(cleanAmazonItemgp(document.URL));
    cleanLinks('amazon');
}
else if (amazonGeneral.test(document.URL)) {
    cleanLinks('amazon');
}
else if (neweggItem.test(document.URL)) {
    cleanPageUrl(cleanNeweggItem(document.URL));
    cleanLinks('newegg');
}
else if (neweggGeneral.test(document.URL)) {
    cleanLinks('newegg');
}
else if (dealtime.test(document.URL)) {
    location.replace(cleanDealtime(document.URL));
}

// -------- Front functions --------
function cleanPageUrl(newUrl) {
    if (newUrl != document.URL) history.replaceState(null,null,newUrl);
}
function cleanLinks(site) {
    var observer = new MutationObserver(function (mutations) {
        mutations.forEach(function(m) {
            links = m.target.getElementsByTagName('a');
            for (var i=links.length; i--;) {
                linkCleaners[site](links[i]);
            }
        });
    }).observe(document,{childList:true,attributes:true,attributeFilter:['href'],subtree:true});
}
function googleInstant() {
    if (!document.URL.includes('#imgrc=')) {
        var searchTerms = String(document.URL.match(/\#.*/)).replace(/^\#/,'');
        history.replaceState(null,null,String(document.URL.replace(/search\?.*/, 'search?'+searchTerms)));
    }
}

// -------- URL cleaning functions --------
function cleanGoogleSearch(url) {
    return url.replace('?','?&').replace(googleSearchParameters,'').replace('?&','?').replace(/^http\:/,'https:');
}
function cleanGoogleRedirect(url) {
    return decodeURIComponent(url.replace(googleRedirect,'').replace(/\&(rct|psig|ei|bvm|sa)\=.*$/g,''));
}
function cleanGoogleImageRedirect(url) {
    return decodeURIComponent(url.replace(googleImageRedirect,'').replace(/\&imgrefurl\=.*/,''));
}
function cleanBingSearch(url) {
    return url.replace('?','?&').replace(/\&(go|qs|form|FORM|filt|pq|s[cpk]|qpvt)\=[^&]*/g,'').replace('?&','?');
}
function cleanYoutubeVideo(url) {
    return url.replace("?","?&").replace(/\&(feature|src_vid|annotation_id|gl|hl)=[^&]*/g,'').replace("?&","?").replace(/^http\:/,'https:');
}
function cleanYoutubeRedirect(url) {
    return decodeURIComponent(url.replace(youtubeRedirect,'').replace(/\&redir_token\=.*/,''));
}
function cleanEbayItem(url) {
    return 'http://' + url.split('/')[2] + '/itm' + url.match(/\/[0-9]{11,13}[^#?&\/]/) + (url.match(/\#[A-Za-z]+$/)||'');
}
function cleanEbaySearch(url) {
    return url.replace('?','?&').replace(/\&(\_osacat|\_odkw|\_from|rt|\_trksid|\_sacat)\=[^&]*/g,'').replace('?&','?');
}
function cleanAmazonItemgp(url) {
    return 'http://' + url.split('/')[2] + url.match(/\/gp\/product\/[A-Z0-9]{10}\/?/);
}
function cleanAmazonItemdp(url) {
    return 'http://' + url.split('/')[2] + url.match(/\/dp\/[A-Z0-9]{10}\/?/);
}
function cleanNeweggItem(url) {
    return 'http://' + url.split('/')[2] + url.match(/\/Product\/Product\.aspx\?Item\=[^&]*/);
}
function cleanDealtime(url) {
    return decodeURIComponent(url.replace(/.*\&url\=/,'').replace(/(\%26|)\&linkin_id\=.*$/,'')).replace(/\&(url|partner)\=[^&]*/g,'');
}

// -------- Link cleaning functions --------
var linkCleaners = {
    all:function(link) {
        if      (googleRedirect.test(link.href)) link.href = cleanGoogleRedirect(link.href);
        else if (googleSearch.test(link.href))   link.href = cleanGoogleSearch(link.href);
        else if (amazonItemgp.test(link.href))   link.href = cleanAmazonItemgp(link.href);
        else if (amazonItemdp.test(link.href))   link.href = cleanAmazonItemdp(link.href);
        else if (ebaySearch.test(link.href))     link.href = cleanEbaySearch(link.href);
        else if (ebayItem.test(link.href))       link.href = cleanEbayItem(link.href);
        else if (neweggItem.test(link.href))     link.href = cleanNeweggItem(link.href);
        else if (youtubeVideo.test(link.href))   link.href = cleanYoutubeVideo(link.href);
    },
    amazon:function(link) {
        if      (amazonItemgp.test(link.href))   link.href = cleanAmazonItemgp(link.href);
        else if (amazonItemdp.test(link.href))   link.href = cleanAmazonItemdp(link.href);
    },
    ebay:function(link) {
        if      (ebaySearch.test(link.href))     link.href = cleanEbaySearch(link.href);
        else if (ebayItem.test(link.href))       link.href = cleanEbayItem(link.href);
    },
    newegg:function(link) {
        if      (neweggItem.test(link.href))     link.href = cleanNeweggItem(link.href);
    },
    google:function(link) {
        link.removeAttribute('onmousedown');
        linkCleaners.all(link);
    },
    youtube:function(link) {
        if      (youtubeVideo.test(link.href))   link.href = cleanYoutubeVideo(link.href);
        if (/yt-uix-redirect-link/.test(link.className)) {
            link.className = 'yt-uix-sessionLink';
            linkCleaners.all(link);
            link.title = link.href;
        }
        else if (/spf-link/.test(link.className)) {
            link.className = 'content-link';
            link.removeAttribute('rel');
        }
    }
};