General URL Cleaner

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

当前为 2016-05-19 提交的版本,查看 最新版本

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

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

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴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.(a[cdelmstz]|b[aefgijsty]|c[acdfghilmnvz]|cat|com|d[ejkmz]|e[es]|f[imr]|g[aeglmpry]|h[nrtu]|i[emoqrst])/.*$/
// @include        /^https?://[a-z]+.google.(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.0.0
// @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);
    },
    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');
        }
    },
    google:function(link) {
        link.removeAttribute('onmousedown');
        linkCleaners.all(link);
    }
};