General URL Cleaner

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

Tính đến 26-08-2015. Xem phiên bản mới nhất.

You will need to install an extension such as Tampermonkey, Greasemonkey or Violentmonkey to install this script.

Bạn sẽ cần cài đặt một tiện ích mở rộng như Tampermonkey hoặc Violentmonkey để cài đặt kịch bản này.

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

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

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

You will need to install a user script manager extension to install this script.

(Tôi đã có Trình quản lý tập lệnh người dùng, hãy cài đặt nó!)

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.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(I already have a user style manager, let me install it!)

// ==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|ca|ac|ad|ae|al|am|us|as|at|az|ba|be|bf|bg|bi|bj|bs|bt|by|cc|cd|cf|cat|cg)\/.*$/
// @include        /^https?://[a-z]*.google.(ch|ci|cl|cm|cn|cv|cz|de|dj|dk|dm|dz|ee|es|fi|fm|fr|ga|ge|gg|gl|gm|gp|gr|gy|hn)\/.*$/
// @include        /^https?://[a-z]*.google.(iq|ie|im|io|is|it|je|jo|ki|kg|kz|hr|ht|hu|ir|la|li|lk|lt|lu|lv|md|me|mg|mk|ml)\/.*$/
// @include        /^https?://[a-z]*.google.(mn|ms|mu|mv|mw|ne|nl|no|nr|nu|pl|pn|ps|vg|pt|ro|rs|ru|rw|sc|se|sh|si|sk|sn|sm)\/.*$/
// @include        /^https?://[a-z]*.google.(so|st|td|tg|tk|tl|tm|to|tn|tt|vu|ws|com.(sg|sl|sv|tj|tn|tr|tw|ua|uy|vc|vn))\/.*$/
// @include        /^https?://[a-z]*.google.com.(au|af|ag|ai|ar|bh|bn|bo|br|bz|kh|co|cu|cy|do|ec|eg|et|fj|gh|gi|gt|hk|jm)\/.*$/
// @include        /^https?://[a-z]*.google.com.(kw|lb|lc|ly|mm|mt|mx|my|na|nf|ng|ni|np|om|pa|pe|ph|pk|pg|pr|py|qa|sa|sb)\/.*$/
// @include        /^https?://[a-z]*.google.co.(uk|nz|ao|ar|bw|ck|cr|id|il|in|jp|ke|kr|ls|ma|mz|th|tz|ug|uz|ve|vi|za|zm|zw)\/.*$/
// @include        /^https?://[a-z]*.amazon.(cn|in|co.jp|fr|de|it|nl|es|co.uk|ca|com.mx|com|com.au|com.br)\/.*$/
// @include        /^https?://[a-z]*.newegg.(com|ca|cn)\/.*$/
// @include        /^https?://[a-z]*.ebay.(com.au|at|be|ca|fr|de|com.hk|in|ie|co.il|it|com.my|nl|co.za|ph|pl|com.sg|co.za|es|ch|co.th|co.uk|com|vn)\/.*$/
// @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.0.4.4
// @license        GPL version 3 or any later version; http://www.gnu.org/copyleft/gpl.html
// ==/UserScript==

// -------- Variables -----------
// ------------------------------

var includeLinks =           new RegExp(/(^https?|^ftp)\:\/\//);
var bingSearch =             new RegExp(/^https?:\/\/www\.bing\.(.+?)\/search\?/);
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\.(.+?)\/itm/);
var ebaySearch =             new RegExp(/^https?:\/\/www\.ebay\.(.+?)\/sch\//);
var ebayGeneral =            new RegExp(/^https?:\/\/www\.ebay\.(.+?)/);
var amazonItemdp =           new RegExp(/^https?:\/\/www\.amazon\.(.+?)\/dp\//);
var amazonItemgp =           new RegExp(/^https?:\/\/www\.amazon\.(.+?)\/gp\/product\//);
var amazonGeneral =          new RegExp(/^https?:\/\/www\.amazon\.(.+?)\//);
var neweggItem =             new RegExp(/^http:\/\/www\.newegg\.(com|ca)\/Product\/Product\.aspx/);
var neweggGeneral =          new RegExp(/^http:\/\/www\.newegg\.(com|ca)/);
var dealtime =               new RegExp(/http:\/\/stat\.dealtime\.com\/DealFrame\/DealFrame\.cmp\?/);
var googleRedirect =         new RegExp(/^https?:\/\/www\.google\.(.+?)\/url\?.*\&url\=/);
var googleImageRedirect =    new RegExp(/^https?:\/\/www\.google\.(.+?)\/imgres\?imgurl\=/);
var googleSearch =           new RegExp(/^https?:\/\/[a-z]*\.google\.(.+?)\/[a-z]*\?.*$/);
var utmParameters =          new RegExp(/((\?|\&|)utm_(source|medium|campaign)\=[^&]*|\&amp\;)/g);
var googleSearchParameters = new RegExp(
    '\&(aqs|es_sm|channel|tab|num|hl|safe|tbo|sclient|sourceid|spell|site|sa|ei|client|complete|as_qdr|um|sa|tab|xhr|pq|pf|cp|bs|hs|' +
    'authuser|rlz|cad|rct|ved|usg|source|oe|oq|ie|dpr|ved|tbas|sei|biw|bih|gpsrc|gfe_rd|gws_rd|bvm|bav|pbx|gs_(l|rn|ri|mss|id))\=[^&]*', 'g');

// -------- Main section --------
// ------------------------------

if (bingSearch.test(document.URL)) {
    var newPageUrl = cleanBingSearch(document.URL);
    if (newPageUrl != document.URL) history.replaceState(null,null,newPageUrl);
    var cleanLinks = new MutationObserver(cleanLinksAll).observe(document,{childList:true,subtree:true});
}
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)) {
    var newPageUrl = cleanGoogleSearch(document.URL);
    if (newPageUrl != document.URL) history.replaceState(null,null,newPageUrl);
    var cleanLinks = new MutationObserver(cleanLinksGoogleSearch).observe(document,{childList:true,subtree:true});
    window.onhashchange = googleInstant;
}
else if (youtubeVideo.test(document.URL)) {
    var newPageUrl = cleanYoutubeVideo(document.URL);
    if (newPageUrl != document.URL) history.replaceState(null,null,newPageUrl);
    var cleanLinks = new MutationObserver(cleanLinksYoutubeVideo).observe(document,{childList:true,subtree:true});
}
else if (youtubeRedirect.test(document.URL)) {
    location.replace(cleanYoutubeRedirect(document.URL));
}
else if (ebayItem.test(document.URL)) {
    var newPageUrl = cleanEbayItem(document.URL);
    if (newPageUrl != document.URL) history.replaceState(null,null,newPageUrl);
    var cleanLinks = new MutationObserver(cleanLinksEbay).observe(document,{childList:true,subtree:true});
}
else if (ebaySearch.test(document.URL)) {
    var newPageUrl = cleanEbaySearch(document.URL);
    if (newPageUrl != document.URL) history.replaceState(null,null,newPageUrl);
    var cleanLinks = new MutationObserver(cleanLinksEbay).observe(document,{childList:true,subtree:true});
}
else if (ebayGeneral.test(document.URL)) {
    var cleanLinks = new MutationObserver(cleanLinksEbay).observe(document,{childList:true,subtree:true});
}
else if (amazonItemdp.test(document.URL)) {
    var newPageUrl = cleanAmazonItemdp(document.URL);
    if (newPageUrl != document.URL) history.replaceState(null,null,newPageUrl);
    var cleanLinks = new MutationObserver(cleanLinksAmazonItem).observe(document,{childList:true,subtree:true});
}
else if (amazonItemgp.test(document.URL)) {
    var newPageUrl = cleanAmazonItemgp(document.URL);
    if (newPageUrl != document.URL) history.replaceState(null,null,newPageUrl);
    var cleanLinks = new MutationObserver(cleanLinksAmazonItem).observe(document,{childList:true,subtree:true});
}
else if (amazonGeneral.test(document.URL)) {
    var cleanLinks = new MutationObserver(cleanLinksAmazonItem).observe(document,{childList:true,subtree:true});
}
else if (neweggItem.test(document.URL)) {
    var newPageUrl = cleanNeweggItem(document.URL);
    if (newPageUrl != document.URL) history.replaceState(null,null,newPageUrl);
    var cleanLinks = new MutationObserver(cleanLinksNeweggItem).observe(document,{childList:true,subtree:true});
}
else if (neweggGeneral.test(document.URL)) {
    var cleanLinks = new MutationObserver(cleanLinksNeweggItem).observe(document,{childList:true,subtree:true});
}
else if (dealtime.test(document.URL)) {
    location.replace(cleanDealtime(document.URL));
}

// -------- Site-specific cleaning functions --------
// --------------------------------------------------

function cleanGoogleSearch(oldurl) {
    return oldurl.replace('?','?&').replace(googleSearchParameters,'').replace('?&','?').replace(/^http\:/,'https:');
}
function cleanGoogleRedirect(oldurl) {
    return decodeURIComponent(oldurl.replace(googleRedirect,'').replace(/\&(psig|ei|bvm)\=.*$/g,''));
}
function cleanGoogleImageRedirect(oldurl) {
    return decodeURIComponent(oldurl.replace(googleImageRedirect,'').replace(/\&imgrefurl\=.*/,''));
}
function cleanBingSearch(oldurl) {
    return oldurl.replace('?','?&').replace(/\&(go|qs|form|FORM|filt|pq|sc|sp|sk|qpvt)\=[^&]*/g,'').replace('?&','?').replace(/^http\:/,'https:');
}
function cleanYoutubeVideo(oldurl) {
    return 'https://www.youtube.com/watch?' + oldurl.match(/v\=[^&]*/);
}
function cleanYoutubeRedirect(oldurl) {
    return decodeURIComponent(oldurl.replace(youtubeRedirect,'').replace(/\&redir_token\=.*/,''));
}
function cleanEbayItem(oldurl) {
    return 'http://' + oldurl.split('/')[2] + '/itm' + oldurl.match(/\/[0-9]{11,13}[^#?&\/]/);
}
function cleanEbaySearch(oldurl) {
    return oldurl.replace('?','?&').replace(/\&(\_osacat|\_odkw|\_from|rt|\_trksid|\_sacat)\=[^&]*/g,'').replace('?&','?');
}
function cleanAmazonItemgp(oldurl) {
    return 'http://' + oldurl.split('/')[2] + oldurl.match(/\/gp\/product\/[A-Z0-9]{10}\/?/);
}
function cleanAmazonItemdp(oldurl) {
    return 'http://' + oldurl.split('/')[2] + oldurl.match(/\/dp\/[A-Z0-9]{10}\/?/);
}
function cleanNeweggItem(oldurl) {
    return 'http://' + oldurl.split('/')[2] + oldurl.match(/\/Product\/Product\.aspx\?Item\=[^&]*/);
}
function cleanDealtime(oldurl) {
    return decodeURIComponent(oldurl.replace(/.*\&url\=/,'').replace(/(\%26|)\&linkin_id\=.*$/,'')).replace(/\&(url|partner)\=[^&]*/g,'');
}

// -------- Page link cleaning functions --------
// ----------------------------------------------

function cleanLinksGoogleSearch() {
    var links = document.links;
    for (var i = links.length; i--;) {   
        links[i].removeAttribute('onmousedown'); // Results link redirection
        if      (googleRedirect.test(links[i].href)) links[i].href = cleanGoogleRedirect(links[i].href);
        else if (googleSearch.test(links[i].href))   links[i].href = cleanGoogleSearch(links[i].href);
        else if (amazonItemgp.test(links[i].href))   links[i].href = cleanAmazonItemgp(links[i].href);
        else if (amazonItemdp.test(links[i].href))   links[i].href = cleanAmazonItemdp(links[i].href);
        else if (ebaySearch.test(links[i].href))     links[i].href = cleanEbaySearch(links[i].href);
        else if (ebayItem.test(links[i].href))       links[i].href = cleanEbayItem(links[i].href);
        else if (neweggItem.test(links[i].href))     links[i].href = cleanNeweggItem(links[i].href);
        else if (youtubeVideo.test(links[i].href))   links[i].href = cleanYoutubeVideo(links[i].href);
    }
}
function cleanLinksAll() {
    var links = document.links;
    for (var i = links.length; i--;) {
        if      (googleRedirect.test(links[i].href)) links[i].href = cleanGoogleRedirect(links[i].href);
        else if (googleSearch.test(links[i].href))   links[i].href = cleanGoogleSearch(links[i].href);
        else if (amazonItemgp.test(links[i].href))   links[i].href = cleanAmazonItemgp(links[i].href);
        else if (amazonItemdp.test(links[i].href))   links[i].href = cleanAmazonItemdp(links[i].href);
        else if (ebaySearch.test(links[i].href))     links[i].href = cleanEbaySearch(links[i].href);
        else if (ebayItem.test(links[i].href))       links[i].href = cleanEbayItem(links[i].href);
        else if (neweggItem.test(links[i].href))     links[i].href = cleanNeweggItem(links[i].href);
        else if (youtubeVideo.test(links[i].href))   links[i].href = cleanYoutubeVideo(links[i].href);
    }
    self.disconnect()
}
function cleanLinksAmazonItem() {
    var links = document.links;
    for (var i = links.length; i--;) {
        if      (amazonItemgp.test(links[i].href))   links[i].href = cleanAmazonItemgp(links[i].href);
        else if (amazonItemdp.test(links[i].href))   links[i].href = cleanAmazonItemdp(links[i].href);
    }
    self.disconnect()
}
function cleanLinksEbay() {
    var links = document.links;
    for (var i = links.length; i--;) {
        if      (ebaySearch.test(links[i].href))     links[i].href = cleanEbaySearch(links[i].href);
        else if (ebayItem.test(links[i].href))       links[i].href = cleanEbayItem(links[i].href);
    }
    self.disconnect()
}
function cleanLinksYoutubeVideo() {
    var links = document.links;
    for (var i = links.length; i--;) {
        if      (youtubeVideo.test(links[i].href))   links[i].href = cleanYoutubeVideo(links[i].href);
        if      (links[i].rel=='nofollow' || links[i].rel=='spf-prefetch') {
                                                     links[i].removeAttribute('rel');
                                                     links[i].removeAttribute('class');
                                                     links[i].removeAttribute('dir');
                                                     links[i].removeAttribute('data-sessionlink');
        }
    }
}
function cleanLinksNeweggItem() {
    var links = document.links;
    for (var i = links.length; i--;) {
        if      (neweggItem.test(links[i].href))     links[i].href = cleanNeweggItem(links[i].href);
    }
    self.disconnect()
}

// -------- Extra functions --------
// ---------------------------------

// Google Instant document URL cleaning
// This is called whenever the search terms change
function googleInstant() {
    if (!document.URL.includes('#imgrc=')) { // Don't rewrite anything if an image is clicked in image searches
        var newSearchTerms = String(document.URL.match(/\#.*/)).replace(/^\#/,'');
        var newSearchUrl = String(document.URL.replace(/search\?.*/, 'search?' + newSearchTerms));
        history.replaceState(null,null,newSearchUrl);
    }
}