General URL Cleaner

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

目前為 2016-06-08 提交的版本,檢視 最新版本

您需要先安裝使用者腳本管理器擴展,如 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?)?\.[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/*
// @exclude     https://apis.google.com/*
// @exclude     https://www.google.com/recaptcha/api2/*
// @version     2.2.6
// @license     GPL version 3 or any later version; http://www.gnu.org/copyleft/gpl.html
// ==/UserScript==
var doc = document;
var bing = /^https?:\/\/www\.bing\.com/;
var ebay = /^https?:\/\/[a-z]+\.ebay(\.com?)?\.[a-z]{2,3}/;
var amazon = /^https?:\/\/www\.amazon(\.com?)?\.[a-z]{2,3}\//;
var newegg = /^http:\/\/www\.newegg\.c(om|a)/;
var youtube = /^https?:\/\/www\.youtube\.com/;
var google = /^https?:\/\/[a-z]*\.google(\.com?)?\.[a-z]{2,3}\//;
var dealtime = /http:\/\/stat\.dealtime\.com\/DealFrame\/DealFrame\.cmp\?/;
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|sop)|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 (bing.test(doc.URL)) {
    var newUrl = cleanBingSearch(doc.URL);
    if (doc.URL.startsWith('http:')) location.href = newUrl;
    else cleanPageUrl(newUrl);
    cleanLinks('all');
}
else if (google.test(doc.URL)) {
    if (doc.URL.includes('/url?')) location.replace(cleanGoogleRedirect(doc.URL));
    else if (doc.URL.includes('/imgres?imgurl=')) location.replace(cleanGoogleImageRedirect(doc.URL));
    else if (/\.[a-z]{2,3}\/[a-z]*[?#]/.test(doc.URL)) {
        cleanPageUrl(cleanGoogleSearch(doc.URL));
        cleanLinks('google');
        googleInstant();
    }
}
else if (youtube.test(doc.URL)) {
    if (doc.URL.includes('/watch/')) cleanPageUrl(cleanYoutubeVideo(doc.URL));
    else if (doc.URL.includes('redirect?')) location.replace(cleanYoutubeRedirect(doc.URL));
    cleanLinks('youtube');
}
else if (ebay.test(doc.URL)) {
    if (doc.URL.includes('/itm/')) cleanPageUrl(cleanEbayItem(doc.URL));
    else if (doc.URL.includes('/sch/')||doc.URL.includes('/dsc/')) cleanPageUrl(cleanEbaySearch(doc.URL));
    cleanLinks('ebay');
}
else if (amazon.test(doc.URL)) {
    if (doc.URL.includes('/dp/')) cleanPageUrl(cleanAmazonItemdp(doc.URL));
    else if (doc.URL.includes('/gp/product')) cleanPageUrl(cleanAmazonItemgp(doc.URL));
    cleanLinks('amazon');
}
else if (newegg.test(doc.URL)) {
    if (doc.URL.includes('/Product/')) cleanPageUrl(cleanNeweggItem(doc.URL));
    cleanLinks('newegg');
}
else if (dealtime.test(doc.URL)) {
    location.replace(cleanDealtime(doc.URL));
}

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

function cleanLinks(site) { new MutationObserver(function(_,self) {
    for (var i in doc.links) { a = doc.links[i];
        if (a.href) if (a.href.startsWith('http')) {
            old = a.href;
            linkCleaners[site](a);
            if (a.innerText==old) a.innerText=a.href;
            if (a.title==old) a.title=a.href;
        }
    }
    if (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 (doc.URL.includes('#') && !doc.URL.includes('#imgrc=')) {
            newUrl = baseUrl(doc.URL) + cleanGoogleSearch((doc.URL.match(/\/[a-z]*\?/)||'/search?') + (doc.URL.match(/#(.*)/)[1]||doc.URL.match(/\?(.*)#/)[1]));
            history.replaceState(null,null,newUrl);
            cleanLinks('google');
        }}).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.replace(/.+\/url\?.*(url|q)=/,'').replace(/&(rct|psig|ei|bvm|sa)=.*$/g,''));
}
function cleanGoogleImageRedirect(url) {
    return decodeURIComponent(url.replace(/(.+?)\?imgurl=/,'').replace(/&imgrefurl=.*/,''));
}
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.replace(/(.+?)\?q=/,'').replace(/&redir_token=.*/,''));
}
function cleanEbayItem(url) {
    return baseUrl(url)+'/itm'+url.match(/\/[0-9]{11,13}/)+(url.match(/#[A-Za-z]+$/)||'');
}
function cleanEbaySearch(url) {
    return url.replace('?','?&').replace(ebayParams,'').replace('?&','?');
}
function cleanAmazonItemgp(url) {
    return baseUrl(url)+url.match(/\/gp\/product\/[A-Z0-9]{10}/);
}
function cleanAmazonItemdp(url) {
    return baseUrl(url)+url.match(/\/dp\/[A-Z0-9]{10}/);
}
function cleanNeweggItem(url) {
    return baseUrl(url)+'/Product/Product.aspx'+url.match(/\?Item=[^&]*/);
}
function cleanDealtime(url) {
    return decodeURIComponent(url.replace(/.*&url=/,'').replace(/(%26)?&linkin_id=.*$/,'')).replace(/&(url|partner)=[^&]*/g,'');
}
function baseUrl(url) {
    d = url.split('/'); return d[0]+'//'+d[2];
}

// -------- Link cleaning functions --------
var linkCleaners = {
    all:function(a) {
        if (google.test(a.href))
            if (a.href.includes('/url=')) a.href = cleanGoogleRedirect(a.href);
            else if (/\/[a-z]+\?/.test(a.href)) a.href = cleanGoogleSearch(a.href);
        else if (youtube.test(a.href))
            if (a.href.includes('/watch/')) a.href = cleanYoutubeVideo(a.href);
            else if (a.href.includes('/redirect?')) a.href = cleanGoogleRedirect(a.href);
        else if (amazon.test(a.href))
            if (a.href.includes('/dp/')) a.href = cleanAmazonItemdp(a.href);
            else if (a.href.includes('/gp/product')) a.href = cleanAmazonItemgp(a.href);
        else if (ebay.test(a.href))
            if (a.href.includes('/itm/')) a.href = cleanEbayItem(a.href);
            else if (a.href.includes('/sch/')) a.href = cleanEbaySearch(a.href);
        else if (newegg.test(a.href))
            if (a.href.includes('/Product/')) a.href = cleanNeweggItem(a.href);
        a.href = a.href.replace(utmParams,'');
    },
    amazon:function(a) { if (amazon.test(a.href))
        if (a.href.includes('/dp/')) a.href = cleanAmazonItemdp(a.href);
        else if (a.href.includes('/gp/product')) a.href = cleanAmazonItemgp(a.href);
    },
    ebay:function(a) { if (ebay.test(a.href))
        if (a.href.includes('/itm/')) a.href = cleanEbayItem(a.href);
        else if (a.href.includes('/sch/')) a.href = cleanEbaySearch(a.href);
    },
    newegg:function(a) { if (newegg.test(a.href))
        if (a.href.includes('/Product/')) a.href = cleanNeweggItem(a.href);
    },
    google:function(a) {
        a.removeAttribute('onmousedown');
        linkCleaners.all(a);
    },
    youtube:function(a) {
        linkCleaners.all(a);
        a.classList.remove('yt-uix-redirect-link');
        a.classList.remove('spf-link');
        a.removeAttribute('data-sessionlink');
    }
};