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