// ==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?://.*.google(|.(com|co))(|.(ac|ad|ae|af|ag|ai|al|am|ao|ar|as|at|au|az|ba|bd|be|bf|bg|bh|bi|bj|bn|bo|br|bs|bt|bw|by|bz|ca|kh|cc|cd|cf|cat|cg|ch|ci|ck|cl|cm|cn|cn|co|cr|cu|cv|cy|cz|de|dj|dk|dm|do|dz|ec|ee|eg|es|et|fi|fj|fm|fr|ga|ge|gf|gg|gh|gi|gl|gm|gp|gr|gt|gy|hk|hn|hr|ht|hu|id|ir|iq|ie|il|im|in|io|is|it|je|jm|jo|jp|ke|ki|kg|kr|kw|kz|la|lb|lc|li|lk|ls|lt|lu|lv|ly|ma|md|me|mg|mk|ml|mm|mn|ms|mt|mu|mv|mw|mx|my|mz|na|ne|nf|ng|ni|nl|no|np|nr|nu|nz|om|pa|pe|ph|pk|pl|pg|pn|pr|ps|pt|py|qa|ro|rs|ru|rw|sa|sb|sc|se|sg|sh|si|sk|sl|sn|sm|so|st|sv|td|tg|th|tj|tk|tl|tm|to|tn|tn|tr|tt|tw|tz|ua|ug|uk|us|uy|uz|vc|ve|vg|vi|vn|vu|ws|za|zm|zw)).*$/ // @include /^https?://.*.amazon.(cn|in|co.jp|fr|de|it|nl|es|co.uk|ca|com.mx|com|com.au|com.br).*$/ // @include /^https?://.*.newegg.(com|ca|cn).*$/ // @include /^https?://.*.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?://.*.bing.com\/.*$/ // @include /^https?://.*.youtube.com\/.*$/ // @include /^https?://.*.dealtime.com\/.*$/ // @exclude https://apis.google.com/* // @exclude https://www.google.com/recaptcha/api2/* // @version 1.9.0.6 // @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 var bing = new RegExp(/^https?:\/\/www\.bing\.(.+?)\/search\?/); var google = new RegExp(/^https?:\/\/(www|mail|maps|books|play|news|plus|photos|docs|drive|support)\.google\.(.+?)\/[a-z]*\?/); var googleImageRedirect = new RegExp(/^https?:\/\/www\.google\.(.+?)\/url\?/); var youtube = new RegExp(/^https?:\/\/www\.youtube\.com\/watch/); var ebay = new RegExp(/^https?:\/\/www\.ebay\.(.+?)\/itm/); var ebaySearch = new RegExp(/^https?:\/\/www\.ebay\.(.+?)\/sch\//); var amazon = new RegExp(/^https?:\/\/www\.amazon\..*\/dp\//); var newegg = new RegExp(/^http:\/\/www\.newegg\.(com|ca)\/Product\/Product\.aspx/); var dealtime = new RegExp(/http:\/\/stat\.dealtime\.com\/DealFrame\/DealFrame\.cmp\?/); // 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; var excludeLinks = new RegExp(/(^$|^data\:|^javascript\:)/); // don't do anything with links that are blank, javascript, or containing 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 } this.removeEventListener('DOMContentLoaded', cleanPageLinks, false); // We don't need to keep the event listener running } // 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 } this.removeEventListener('DOMContentLoaded', cleanGooglePageLinks, false); // We don't need to keep event listener running } // Google Instant document URL cleaning - if the search terms change, remove the extra stuff. function googleInstant() { if (!document.URL.includes('#imgrc=')) { // Don't rewrite anything if an image is clicked in image searches var newSearchString = String(document.URL.match(/\#.*/)).replace(/^\#/,''); // The string after the hash, containing the new search terms var newSearchUrl = String(document.URL.replace(/search\?.*/, 'search?' + newSearchString)); // Remake the full URL with only the new search terms 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\=.*$/,'')); break; case google.test(oldurl): newurl = oldurl.replace('?','?&') // temporarily put an "&" after the "?" so that the regex below will always match .replace(/\&(aqs|es_sm|channel|tab|num|hl|safe|tbo|sclient|sourceid|spell|client|complete|as_qdr|um|sa|tab|authuser|rlz|cad|rct|ved|usg|site|source|oe|oq|sa|ei|ie|dpr|gs\_l|ved|tbas|sei|biw|bih|gpsrc|gfe_rd|gws_rd)\=[^&]*/g,'') .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 ebay.test(oldurl): newurl = 'http://' + oldurl.split('/')[2] + '/itm' + oldurl.match(/\/[0-9]{11,13}[^#?&\/]/); // the split gets the domain name. Should be more efficient than a regex. break; case ebaySearch.test(oldurl): newurl = oldurl.replace('?','?&') // temporarily put an "&" after the "?" so that the regex below will always match .replace(/\&(\_osacat|\_odkw|\_from|rt|\_trksid|\_sacat)\=[^&]*/g,'') .replace('?&','?') break; case amazon.test(oldurl): newurl = 'https://' + oldurl.split('/')[2] + oldurl.match(/\/dp\/[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(/((\?|\&|)utm_(source|medium|campaign)\=[^&]*|\&\;)/g,''); return newurl; }