General URL Cleaner

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

目前為 2015-08-12 提交的版本,檢視 最新版本

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

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

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 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|ca|ac|ad|ae|al|am|us|as|at|az|ba|be|bf|bg|bi|bj|bs|btby|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|frga|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|nonr|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        1.9.2.2
// @license        GPL version 3 or any later version; http://www.gnu.org/copyleft/gpl.html
// ==/UserScript==

// compile these regexes beforehand to improve efficiency
// These regex's are for checking which site the URL belongs to
var bing = new RegExp(/^https?:\/\/www\.bing\.(.+?)\/search\?/);
var google = new RegExp(/^https?:\/\/[a-z]*\.google\.(.+?)\/[a-z]*\?/);
var googleImageRedirect = new RegExp(/^https?:\/\/www\.google\.(.+?)\/url\?.*\&url\=/);
var youtube = new RegExp(/^https?:\/\/www\.youtube\.com\/watch/);
var youtubeRedirectLink = new RegExp(/^https?:\/\/www.youtube.com\/redirect\?q\=/);
var ebay = new RegExp(/^https?:\/\/www\.ebay\.(.+?)\/itm/);
var ebaySearch = new RegExp(/^https?:\/\/www\.ebay\.(.+?)\/sch\//);
var amazondp = new RegExp(/^https?:\/\/www\.amazon\..*\/dp\//);
var amazongp = new RegExp(/^https?:\/\/www\.amazon\..*\/gp\/product\//);
var newegg = new RegExp(/^http:\/\/www\.newegg\.(com|ca)\/Product\/Product\.aspx/);
var dealtime = new RegExp(/http:\/\/stat\.dealtime\.com\/DealFrame\/DealFrame\.cmp\?/);
//  These regex's are for replacing parts of the URL
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|' +
    'authuser|rlz|cad|rct|ved|usg|source|oe|oq|ie|dpr|gs_l|ved|tbas|sei|biw|bih|gpsrc|gfe_rd|gws_rd)\=[^&]*', 'gi');

// Clean the current page URL
var newPageUrl = cleanUrl(document.URL);
if (newPageUrl != document.URL) location.replace(newPageUrl);

// Cleans links on the page
var links = document.links;
// don't do anything with links that are blank, javascript, email addresses, data
var excludeLinks = new RegExp(/(^$|^javascript\:|^mailto\:|^data\:)/);

if (google.test(newPageUrl)) {
    document.addEventListener("DOMContentLoaded", cleanGooglePageLinks, false);
    window.onhashchange = googleInstant;
}
else {
    document.addEventListener("DOMContentLoaded", cleanPageLinks, false);
}

// Standard link cleaning function
function cleanPageLinks() {
    for (var i = links.length; i--;) {
        if (excludeLinks.test(links[i].href)) continue; // Links to skip
        links[i].href = cleanUrl(links[i].href);        // Standard link cleaning
    }
    // We don't need to keep the event listener running
    this.removeEventListener('DOMContentLoaded', cleanPageLinks, false);
}

// Google search results link cleaning function
function cleanGooglePageLinks() {
    for (var i = links.length; i--;) {
        if (excludeLinks.test(links[i].href)) continue; // Links to skip
        links[i].removeAttribute('onmousedown');        // Remove search results redirection
        links[i].href = cleanUrl(links[i].href);        // Standard link cleaning
    }
    // We don't need to keep event listener running
    this.removeEventListener('DOMContentLoaded', cleanGooglePageLinks, false);
}

// Google Instant document URL cleaning - if the search terms change, remove the extra stuff.
function googleInstant() {
    // Don't rewrite anything if an image is clicked in image searches
    if (!document.URL.includes('#imgrc=')) {
        // The string after the hash, containing the new search terms
        var newSearchString = String(document.URL.match(/\#.*/)).replace(/^\#/,'');
        
        // Remake the full URL with only the new search terms
        var newSearchUrl = String(document.URL.replace(/search\?.*/, 'search?' + newSearchString));
        location.replace(newSearchUrl);
    }
}

// Main function for cleaning the url's
function cleanUrl(oldurl) {
    var newurl = oldurl;
    switch(true) {
        case googleImageRedirect.test(oldurl):
            newurl = decodeURIComponent(oldurl.replace(googleImageRedirect,'').replace(/\&(psig|ei|bvm)\=.*$/g,''));
            break;
        case google.test(oldurl):
                     // temporarily put an "&" after the "?" so that the search parameters regex will always work
            newurl = oldurl.replace('?','?&')
                           .replace(googleSearchParameters,'')
                           .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 youtubeRedirectLink.test(oldurl):
            newurl = decodeURIComponent(oldurl.replace(youtubeRedirectLink,'').replace(/\&redir_token\=.*/,''));
            break;
        case ebay.test(oldurl):
            // the split gets the domain name. Should be more efficient than a regex.
            newurl = 'http://' + oldurl.split('/')[2] + '/itm' + oldurl.match(/\/[0-9]{11,13}[^#?&\/]/);
            break;
        case ebaySearch.test(oldurl):
            newurl = oldurl.replace('?','?&')
                           .replace(/\&(\_osacat|\_odkw|\_from|rt|\_trksid|\_sacat)\=[^&]*/g,'')
                           .replace('?&','?');
            break;
        case amazondp.test(oldurl):
            newurl = 'http://' + oldurl.split('/')[2] + oldurl.match(/\/dp\/[A-Z0-9]{10}\/?/);
            break;
        case amazongp.test(oldurl):
            newurl = 'http://' + oldurl.split('/')[2] + oldurl.match(/\/gp\/product\/[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(utmParameters,'');
    return newurl;
}