General URL Cleaner 2.8

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

目前為 2016-09-21 提交的版本,檢視 最新版本

您需要先安裝使用者腳本管理器擴展,如 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 2.8
// @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?://[a-z]+\.ebay(\.com?)?\.[a-z]{2,3}/.*$/
// @include     /^https?://www\.amazon(\.com?)?\.[a-z]{2,3}/.*$/
// @include     /^https?://www\.newegg\.c(om|a)/.*$/
// @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.8.1
// @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 lhost = loc.host; var lpath = 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|ref_)=[^&#]*/;
var utmParams = /&utm_[a-z]+=[^&]*/g;
var neweggParams = /&(cm_sp|icid)=[^&#]*/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 = /&(sa(fe)?|ved|source(id)?|s?ei|tab|tbo|h[ls]|authuser|n?um|ie|aqs|as_qdr|bav|bi[wh]|bs|bvm|cad|channel|complete|cp|s?client|dpr|e(ch|msg|s_sm)|g(fe|ws)_rd|gpsrc|noj|btnG|o[eq]|p(si|bx|f|q)|rct|rlz|site|spell|tbas|usg|xhr|gs_[a-z]+)=[^&#]*/g;

// -------- Main --------
if (lhost=='www.bing.com') {
    var newUrl = cleanBing(doc.URL);
    if (loc.protocol=='http:') loc.replace(newUrl);
    else cleanPageUrl(newUrl);
    cleanLinks('all');
}
else if (lhost=='www.youtube.com') {
    if (lpath=='/watch') cleanPageUrl(cleanYoutube(doc.URL));
    else if (lpath=='/redirect') loc.replace(cleanYoutubeRedir(loc.search));
    cleanLinks('youtube');
}
else if (lhost.endsWith('.newegg.com')||lhost.endsWith('.newegg.ca')) {
    if (loc.search) cleanPageUrl(cleanNewegg(doc.URL));
    cleanLinks('newegg',1);
}
else if (lhost=='www.imdb.com') {
    if (loc.search) cleanPageUrl(cleanImdb(doc.URL));
    cleanLinks('imdb');
    deleteHash();
}
else if (google.test(lhost)) {
    if (lpath=='/url'||lpath=='/imgres') loc.replace(cleanGoogleRedir(loc.search));
    else if (loc.search||loc.hash.match(/[&#]q=/)) {
        cleanPageUrl(cleanGoogle(doc.URL));
        cleanLinks('google',1);
        googleInstant();
    }
}
else if (ebay.test(lhost)) {
    if (lpath.includes('/itm/')) cleanPageUrl(cleanEbayItem(loc));
    else if (loc.search) cleanPageUrl(cleanEbayParams(doc.URL));
    cleanLinks('ebay');
    deleteHash();
}
else if (amazon.test(lhost)) {
    if (lpath.includes('/dp/')) cleanPageUrl(cleanAmazonItemdp(loc));
    else if (lpath.includes('/gp/product')) cleanPageUrl(cleanAmazonItemgp(loc));
    else if (loc.search) cleanPageUrl(cleanAmazonParams(doc.URL));
    cleanLinks('amazon');
    deleteHash();
}

// -------- 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;
            console.time('clean');
            for (var i=numLinks;0<i--;) { a = links[i]; try {
                if (a.host && a.protocol.startsWith('http')) linkCleaners[site](a);
            }catch(TypeError){}}
            console.timeEnd('clean');
        } if (!remain && doc.readyState=='complete') setTimeout(function(){self.disconnect();}, 20000);
    }).observe(doc,{childList:true,subtree:true});
}
function deleteHash() {
    onhashchange = function() {
        history.replaceState(null,null,loc.href.replace(loc.hash,''));
    };
}
function googleInstant() {
    changeState(function(url) {
        if (url.endsWith('#imgrc=_')) return url.slice(0,-8);
        else if (url.match(/#(.+&)?q=/)) return lpath+cleanGoogle(url.replace(/.+#/,'?'));
        return url;
    });
}
function changeState(mod) {
    h=history; h._pushState=h.pushState; h._replaceState=h.replaceState;
    h.pushState    = function() { arguments[2]=mod(arguments[2]); return h._pushState.apply(this,arguments); };
    h.replaceState = function() { arguments[2]=mod(arguments[2]); return h._replaceState.apply(this,arguments); };
}

// -------- URL cleaning functions --------
function cleanGoogle(url) {
    return url.replace('?','?&').replace(googleParams,'').replace('?&','?');
}
function cleanGoogleRedir(url) {
    return decodeURIComponent(url.match(/[&?](img)?url=([^&]+)/)[2]);
}
function cleanBing(url) {
    return url.replace('?','?&').replace(bingParams,'').replace('?&','?').replace(/^http\:/,'https:');
}
function cleanYoutube(url) {
    return url.replace("?","?&").replace(youtubeParams,'').replace("?&","?");
}
function cleanYoutubeRedir(url) {
    return decodeURIComponent(url.match(/[?&]q=([^&]+)/)[1]);
}
function cleanEbayParams(url) {
    return url.replace('?','?&').replace(ebayParams,'').replace('?&','?');
}
function cleanEbayItem(a) {
    return a.origin+'/itm'+a.pathname.match(/\/[0-9]{12}/)+(a.search.replace('&','?').match(/\?orig_cvip=[^?]+/)||'')+a.hash;
}
function cleanNewegg(url) {
    return url.replace('?','?&').replace(neweggParams,'').replace('?&','?');
}
function cleanAmazonParams(url) {
    return url.replace('?','?&').replace(amazonParams,'').replace('?&','?').replace(/\?$/,'');
}
function cleanAmazonItemgp(a) {
    return a.origin+'/gp/product'+a.pathname.match(/\/[A-Z0-9]{10}/)+a.hash;
}
function cleanAmazonItemdp(a) {
    return a.origin+'/dp'+a.pathname.match(/\/[A-Z0-9]{10}/)+a.hash;
}
function cleanImdb(url) {
    return url.replace('?','?&').replace(/&(pf_rd_[a-z]|ref_)=[^&#]*/,'').replace('?&','?').replace(/\?$/,'');
}
function cleanUtm(url) {
    return url[0]+('&'+url.slice(1)).replace(utmParams,'').slice(1);
}

// -------- 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 = cleanGoogleRedir(a.search);
            else if (a.search) a.search = cleanGoogle(a.search);
        else if (host=='www.youtube.com')
            if (path=='/watch') a.search = cleanYoutube(a.search);
            else if (path=='/redirect') a.href = cleanYoutubeRedir(a.search);
        else if (host.endsWith('.newegg.com')||host.endsWith('.newegg.ca'))
            if(a.search) a.search = cleanNewegg(a.search);
        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 = cleanEbayParams(a.search);
        else {
            if (a.search) a.search = cleanUtm(a.search);
            if (a.hash) a.hash = cleanUtm(a.hash);
        }
    },
    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 = cleanEbayParams(a.search);
    },
    newegg:function(a) {
        if (a.host.endsWith('.newegg.com')||a.host.endsWith('.newegg.ca'))
            if (a.search) a.search = cleanNewegg(a.search);
    },
    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);
    }
};