Jump to content

User:Anakin101/alwayssecurewikipedia.js

From Wikipedia, the free encyclopedia
Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
/* Script to keep rewrite external links to Wikipedia (and other Wikimedia projects) to
 * use the secure server when already browsing from https://secure.wikimedia.org, so
 * that links to diffs, etc., don't take you off the site. */


$(function() {
	if (wgServer !== 'https://secure.wikimedia.org') return;
	
	var re1 = /^http:\/\/([^\/]+?)\.(wik(?:i[mp]edia|tionary|isource|iquote|ibooks|inews|iversity))\.org\/?(.*)$/i;
	var re2 = /^http:\/\/(?:[^\/]+?\.)?(mediawiki|wikimediafoundation)\.org\/?(.*)$/i;
	
	var links = document.getElementsByTagName('a');
	for (var i = links.length; i--;) {
		var link = links[i], href = link.href;
		var m, main, sub, page;
		
		if (m = href.match(re1)) {
			main = m[2].toLowerCase();
			sub = m[1];
			page = m[3];
			
			if (sub === undefined) continue;
			sub = sub.toLowerCase();
			
			if (sub === 'www' || sub === 'mail') continue;
			if (main === 'wikipedia') {
				if (sub.match(/^(download|mobile|.*?\.mobile|.*?\.m)$/)) {
					continue;
				}
			} else if (main === 'wikimedia') {
				if (sub.match(/^(lists|upload|download|bugzilla|techblog|wikitech|svn|stats|volunteer|ticket|survey|.*?planet|.*?donate)$/)) {
					continue;
				}
				
				if (!sub.match(/^(?:.{1,3}|.{1,3}?\..{1,3}|(?:.+?)\.labs)$/)) { // labs, regional wikimedias
					main = 'wikipedia'; // commons, meta, species, wikimania200x, many others
				}
			}
			
			sub = sub.replace(/\./g, '-');
			
		} else if (m = href.match(re2)) {
			main = 'wikipedia';
			sub = m[1].toLowerCase();
			page = m[2];
			if (sub === 'wikimediafoundation') sub = 'foundation';
			
		} else continue;
		
		link.href = wgServer + '/' + main + '/' + sub + '/' + (page ? page : 'wiki/');
	}
});