General URL Cleaner

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

Устаревшая версия за 29.08.2016. Перейдите к последней версии.

Чтобы установить этот скрипт, вы сначала должны установить расширение браузера, например Tampermonkey, Greasemonkey или Violentmonkey.

Чтобы установить этот скрипт, вы сначала должны установить расширение браузера, например Tampermonkey или Violentmonkey.

Чтобы установить этот скрипт, вы сначала должны установить расширение браузера, например Tampermonkey или Violentmonkey.

Чтобы установить этот скрипт, вы сначала должны установить расширение браузера, например Tampermonkey или Userscripts.

Чтобы установить этот скрипт, сначала вы должны установить расширение браузера, например Tampermonkey.

Чтобы установить этот скрипт, вы должны установить расширение — менеджер скриптов.

(у меня уже есть менеджер скриптов, дайте мне установить скрипт!)

Чтобы установить этот стиль, сначала вы должны установить расширение браузера, например Stylus.

Чтобы установить этот стиль, сначала вы должны установить расширение браузера, например Stylus.

Чтобы установить этот стиль, сначала вы должны установить расширение браузера, например Stylus.

Чтобы установить этот стиль, сначала вы должны установить расширение — менеджер стилей.

Чтобы установить этот стиль, сначала вы должны установить расширение — менеджер стилей.

Чтобы установить этот стиль, сначала вы должны установить расширение — менеджер стилей.

(у меня уже есть менеджер стилей, дайте мне установить скрипт!)

// ==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?)?\.[a-z]{2,3}/.*$/
// @include     /^https?://www\.amazon(\.com?)?\.[a-z]{2,3}/.*$/
// @include     /^https?://www\.newegg\.c(om|a)/.*$/
// @include     /^https?://[a-z]+\.ebay(\.com?)?\.[a-z]{2,3}/.*$/
// @include     /^https?://www\.bing\.com/.*$/
// @include     https://www.youtube.com/*
// @include     http://stat.dealtime.com/*
// @include     http://www.imdb.com/*
// @exclude     https://apis.google.com/*
// @exclude     https://www.google.com/recaptcha/api2/*
// @version     2.6.2
// @license     GPL version 3 or any later version; http://www.gnu.org/copyleft/gpl.html
// ==/UserScript==
var doc = document; var loc=location; var numLinks = 0;
var host = loc.host; var path = loc.pathname;
var ebay = /^[a-z]+\.ebay(\.com?)?\.[a-z]{2,3}$/;
var amazon = /^www\.amazon(\.com?)?\.[a-z]{2,3}$/;
var google = /^[a-z]+\.google(\.com?)?\.[a-z]{2,3}$/;
var amazonParams = /&(url|ie|pf_rd_[a-z]|bbn|rw_html_to_wsrp)=[^&#]*/;
var utmParams = /([?&#]utm_[a-z]+=[^&#]*|&)/g;
var bingParams = /&(go|qs|form|FORM|filt|pq|s[cpk]|qpvt|cvid)=[^&]*/g;
var youtubeParams = /&(feature|src_vid|annotation_id|[gh]l)=[^&]*/g;
var ebayParams = /&(_(o?sacat|odkw|from|trksid)|rt)=[^&]*/g;
var googleParams = /&(aqs|as_qdr|authuser|bav|bi[wh]|bs|bvm|cad|channel|complete|cp|s?client|dpr|es_sm|g(fe|ws)_rd|gpsrc|h[ls]|ie|n?um|btnG|o[eq]|pbx|p[fq]|rct|rlz|sa(fe)?|s?ei|site|source(id)?|spell|tab|tbas|tbo|usg|ved|xhr|gs_(l|r[ni]|mss|id))=[^&]*/g;

// -------- Main --------
if (host=='www.bing.com') {
    var newUrl = cleanBingSearch(doc.URL);
    if (loc.protocol=='http:') loc.replace(newUrl);
    else cleanPageUrl(newUrl);
    cleanLinks('all');
}
else if (host=='www.youtube.com') {
    if (path=='/watch') cleanPageUrl(cleanYoutubeVideo(doc.URL));
    else if (path=='/redirect') loc.replace(cleanYoutubeRedirect(loc.search));
    cleanLinks('youtube');
}
else if (host=='www.newegg.com'||host=='www.newegg.ca') {
    if (path=='/Product/Product.aspx') cleanPageUrl(cleanNeweggItem(loc));
    cleanLinks('newegg',1);
}
else if (host=='www.imdb.com') {
    if (loc.search) cleanPageUrl(cleanImdb(doc.URL));
    cleanLinks('imdb');
}
else if (google.test(host)) {
    if ((path=loc.pathname)=='/url'||path=='/imgres') loc.replace(cleanGoogleRedirect(loc.search));
    else if (loc.search||loc.hash.includes('q=')) {
        cleanPageUrl(cleanGoogleSearch(doc.URL));
        cleanLinks('google',1);
        googleInstant();
    }
}
else if (ebay.test(host)) {
    if (path.includes('/itm/')) cleanPageUrl(cleanEbayItem(loc));
    else if (loc.search) cleanPageUrl(cleanEbaySearch(doc.URL));
    cleanLinks('ebay');
}
else if (amazon.test(host)) {
    if (path.includes('/dp/')) cleanPageUrl(cleanAmazonItemdp(loc));
    else if (path.includes('/gp/product')) cleanPageUrl(cleanAmazonItemgp(loc));
    else if (loc.search) cleanPageUrl(cleanAmazonParams(doc.URL));
    cleanLinks('amazon');
}
else if (host=='stat.dealtime.com' && path=='/DealFrame/DealFrame.cmp') {
    location.replace(cleanDealtime(doc.URL));
}

// -------- Front functions --------
function cleanPageUrl(newUrl) {
    if (newUrl != doc.URL) history.replaceState(null,null,newUrl);
}

function cleanLinks(site, remain=0) { new MutationObserver(function(_,self) {
    links = doc.getElementsByTagName("a");
    if (links.length>numLinks) { numLinks=links.length;
        for (var i=numLinks; 0<i--;) { a = links[i];
            try { if (a.host && a.protocol.startsWith('http')) {
                linkCleaners[site](a);
            }} catch(TypeError) {}
        }
    }
    if (!remain && doc.readyState=='complete') setTimeout(function(){self.disconnect();}, 20000);
}).observe(doc,{childList:true,subtree:true});}

function googleInstant() { new MutationObserver(function(_,self) {
    var search = doc.getElementById('lst-ib');
    if (search) { new MutationObserver(function() {
        if ((hash=loc.hash)=='#imgrc=_') history.replaceState(null,null,doc.URL.replace(hash,''));
        else if (hash && !hash.startsWith('#imgrc=')) {
            newUrl = loc.origin + (loc.pathname||'/') + cleanGoogleSearch('?'+(hash||loc.search).slice(1));
            history.replaceState(null,null,newUrl);
        }
        }).observe(search,{childList:true,attributes:true});
        self.disconnect();
    }
}).observe(doc,{childList:true,subtree:true});}

// -------- URL cleaning functions --------
function cleanGoogleSearch(url) {
    return url.replace('?','?&').replace(googleParams,'').replace('?&','?').replace(/^http\:/,'https:');
}
function cleanGoogleRedirect(url) {
    return decodeURIComponent(url.match(/[&?](img)?url=([^&]+)/)[2]);
}
function cleanBingSearch(url) {
    return url.replace('?','?&').replace(bingParams,'').replace('?&','?').replace(/^http\:/,'https:');
}
function cleanYoutubeVideo(url) {
    return url.replace("?","?&").replace(youtubeParams,'').replace("?&","?");
}
function cleanYoutubeRedirect(url) {
    return decodeURIComponent(url.match(/[?&]q=([^&]+)/)[1]);
}
function cleanEbaySearch(url) {
    return url.replace('?','?&').replace(ebayParams,'').replace('?&','?');
}
function cleanEbayItem(a) {
    return a.origin+'/itm'+a.pathname.match(/\/[0-9]{11,13}/)+a.hash;
}
function cleanNeweggItem(a) {
    return a.origin+'/Product/Product.aspx'+a.search.match(/\?Item=[^&]+/);
}
function cleanAmazonItemgp(a) {
    return a.origin+a.pathname.match(/\/gp\/product\/[A-Z0-9]{10}/)+a.hash;
}
function cleanAmazonItemdp(a) {
    return a.origin+a.pathname.match(/\/dp\/[A-Z0-9]{10}/)+a.hash;
}
function cleanAmazonParams(url) {
    return url.replace('?','?&').replace(amazonParams,'').replace('?&','?').replace(/\/ref=[^?&]+/,'');
}
function cleanDealtime(url) {
    return decodeURIComponent(url.match(/[&?]url=([^?&#]+)/)[1]).replace(/&(url|partner)=[^&#?]+/g,'');
}
function cleanImdb(url) {
    return url.replace('?','?&').replace(/&(pf_rd_[a-z]|ref_)=[^&#]*/,'').replace('?&','?').replace(/\?$/,'');
}

// -------- Link cleaning functions --------
var linkCleaners = {
    all:function(a) { host=a.host; path=a.pathname;
        if (google.test(host))
            if (path=='/imgres'||path=='/url') a.href = cleanGoogleRedirect(a.search);
            else if (a.search) a.search = cleanGoogleSearch(a.search);
        else if (host=='www.youtube.com')
            if (path=='/watch') a.search = cleanYoutubeVideo(a.search);
            else if (path=='/redirect') a.href = cleanYoutubeRedirect(a.search);
        else if (path=='/Product/Product.aspx' && (host=='www.newegg.com'||host=='www.newegg.ca'))
            a.href = cleanNeweggItem(a);
        else if (host=='www.imdb.com' && a.search)
            a.search = cleanImdb(a.search);
        else if (amazon.test(host))
            if (path.includes('/dp/')) a.href = cleanAmazonItemdp(a);
            else if (path.includes('/gp/product')) a.href = cleanAmazonItemgp(a);
            else if (a.search) a.href = cleanAmazonParams(a.href);
        else if (ebay.test(host))
            if (path.includes('/itm/')) a.href = cleanEbayItem(a);
            else if (a.search) a.search = cleanEbaySearch(a.search);
        else a.search = a.search.replace(utmParams,'');
    },
    amazon:function(a) {
        if (amazon.test(a.host))
            if (a.pathname.includes('/dp/')) a.href = cleanAmazonItemdp(a);
            else if (a.pathname.includes('/gp/product')) a.href = cleanAmazonItemgp(a);
            else if (a.search) a.href = cleanAmazonParams(a.href);
    },
    ebay:function(a) {
        if (ebay.test(a.host))
            if (a.pathname.includes('/itm/')) a.href = cleanEbayItem(a);
            else if (a.search) a.search = cleanEbaySearch(a.search);
    },
    newegg:function(a) {
        if (a.pathname=='/Product/Product.aspx' && (a.host=='www.newegg.com'||a.host=='www.newegg.ca'))
            a.href = cleanNeweggItem(a);
    },
    google:function(a) {
        a.removeAttribute('onmousedown');
        linkCleaners.all(a);
    },
    youtube:function(a) {
        a.classList.remove('yt-uix-redirect-link');
        a.classList.remove('spf-link');
        a.removeAttribute('data-sessionlink');
        old = a.href;
        linkCleaners.all(a);
        if (a.title==old) a.title=a.href;
    },
    imdb:function(a){
        if (a.host=='www.imdb.com' && a.search)
            a.search = cleanImdb(a.search);
    }
};