Jump to content

MediaWiki:GeoHack.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.
/**
 * The file is used by GeoHack on Tool Labs (linked from [[Template:GeoTemplate]]).
 *
 * This file is not loaded within a wiki context. Standard libraries (jQuery, mediawiki.js etc) are not available.
 */
 
/**
 * Copy of standard library function
 */
function appendCSS(text) {
	var s = document.createElement('style');
	s.type = 'text/css';
	s.rel = 'stylesheet';
	if (s.styleSheet) s.styleSheet.cssText = text //IE
	else s.appendChild(document.createTextNode(text + '')) //Safari sometimes borks on null
	document.getElementsByTagName('head')[0].appendChild(s);
}


window.onload = function() {
	/* Add row hover effect */
	appendCSS(".directory tr:hover {background:#fdc !important;}");
}

var head  = document.getElementsByTagName('head')[0];
var style  = document.createElement('link');
style.rel  = 'stylesheet';
style.type = 'text/css';
style.href = '//maps.wikimedia.org/leaflet/leaflet.css';
head.appendChild(style);

var script  = document.createElement('script');
script.type = 'text/javascript';
script.src = '//maps.wikimedia.org/leaflet/leaflet.js';
head.appendChild(script);
	    
var OSM_class_R = /\bOSM:([\d.+-]+)_([\d.+-]+)_([\d.+-]+)_(\w+)/;
function embedOpenStreetMap() {
    clearInterval(osmtimer);
    var osmTarget = document.getElementById('osmEmbed');
    if (osmTarget && OSM_class_R.exec(osmTarget.className)) {
        var lat = 1.0 * RegExp.$1,
        	lon = 1.0 * RegExp.$2,
        	zoom = RegExp.$3;
        	
	    var map = L.map('osmEmbed').setView([lat, lon], zoom);
	    map.attributionControl.setPrefix( '' );
		L.tileLayer('//maps.wikimedia.org/osm-intl/{z}/{x}/{y}{r}.png', {
		    attribution: 'Wikimedia maps beta | Map data &copy; <a href="http://osm.org/copyright">OpenStreetMap contributors</a>'
		}).addTo(map);
		
		L.marker([lat, lon]).addTo(map);
    }
}

osmtimer = setInterval("if (document.getElementById('osmEmbed') && L) { embedOpenStreetMap(); }", 200);
setTimeout('clearInterval(osmtimer)', 5000);