User:Hat600/script/MassCoords.js

注意:保存之后,你必须清除浏览器缓存才能看到做出的更改。Google ChromeFirefoxMicrosoft EdgeSafari:按住⇧ Shift键并单击工具栏的“刷新”按钮。参阅Help:绕过浏览器缓存以获取更多帮助。

/* 
 *  this gadget uses [[User:Artoria2e5/Gadget-PRCoords.js]]
 *
 *  create a div with id 'hMassCoords' on a page, save or preview. and this js will work.
 *  ... of course you should install this js before doing so.
 * 
 *  is designed to deal with kml.
 *
 */

;(function($){
	// generate UI
	// prevent doing anything if #hMassCoords not found
	if( ! $('#hMassCoords').append('<textarea id="hMassCoords_textarea" style="height: 350px;"></textarea><button id="hMassCoords_button">Go</button>')[0] ){
		return false;
	}
	
	mw.loader.load('https://zh.wikipedia.org/w/index.php?title=User:Artoria2e5/PRCoords.js&action=raw&ctype=text/javascript');
	
	function towgs(element, index, array){
		// check if element is empty string; we'll deal with them later.
		if (!element){
			return "";
		}
		var gcjtext = element.split(',');
		var gcjcoord = {
			lat: parseFloat(gcjtext[1]),
			lon: parseFloat(gcjtext[0])
		};
		var wgscoord = PRCoords.gcj_wgs_bored(gcjcoord);
		return wgscoord.lon + ',' + wgscoord.lat;
	}

	$('#hMassCoords_button').bind('click', function(){
		// this will be the string to use replace()
		var inputxml = $('#hMassCoords_textarea').val();
		
		// this will be the substring to replace
		var gcjcoords = $(inputxml).find('coordinates').text().replace(/\n/g,'').replace(/\t/g,'').replace(/\,0/g,'').split(' ');
		
		// this will be the substring to replace with
		var wgscoords = gcjcoords.map(towgs);
		
		wgscoords.forEach(function(element, index, array){
			inputxml = inputxml.replace(gcjcoords[index], element);
		});
		
		$('#hMassCoords_textarea').val(inputxml);
		alert('complete');
	});
})(jQuery);