User:Proteins/genericgeotagscript.js

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.
//<pre>
// Generic script for doing something fun with an article's geotag
// Planned example: generate a star chart for that location, with two sliders for day and time
//
// To use this script, add "importScript('User:Proteins/genericgeotagscript.js');" to your monobook.js subpage 
// under your user page, as you can see at User:Proteins/monobook.js

function reportGeotagCoordinates() {
	var latitude;
	var longitude;

	var top_node;
	var parent_node;
	var parent_class_name;

	var spans;
	var temp_span;
	var num_spans = 0;
	var span_index = 0;
	var span_class_name = 0;

	var latitude = 0.0;
	var longitude = 0.0;


// Get the bodyContent node
	top_node = document.getElementById('bodyContent');
	if (!top_node) { 
		window.alert("There is no bodyContent node in this article.");
		return;
	}
 
// Get a list of the spans 
	spans = top_node.getElementsByTagName("SPAN");
	if (!spans) { 
		window.alert("There are no SPAN tags in this article.");
		return;
	}
	num_spans = spans.length;
	if (num_spans < 1) { 
		window.alert("There are no spans in this article.");
		return;
	}
 
// Loop over the spans
	latitude = 0.0;
	longitude = 0.0;

	latitude_is_defined = false;
	longitude_is_defined = false;

	for (span_index=0; span_index<num_spans; span_index++) { 
		temp_span = spans[span_index]; 
		if (!temp_span) { continue; } 

		span_class_name = temp_span.className;
		if (!span_class_name) { continue; }

// Get the latitude
		if (span_class_name == "latitude") { 
			parent_node = temp_span.parentNode;
			parent_class_name = parent_node.className;
			if (parent_class_name != "geo-dec geo") { continue; } 
			latitude = temp_span.innerHTML;
			latitude_is_defined = true;
		}

// Get the longitude
		if (span_class_name == "longitude") { 
			parent_node = temp_span.parentNode;
			parent_class_name = parent_node.className;
			if (parent_class_name != "geo-dec geo") { continue; } 
			longitude = temp_span.innerHTML;
			longitude_is_defined = true;
		}
  
	} // closes loop over spans

	if ((longitude_is_defined) && (latitude_is_defined)) { 
		window.alert("Latitude = " + latitude + " \t Longitude = " + longitude + "\n");
	} else { 
		window.alert("The geotag latitude and longitude coordinates were not found on this page.\n");
	}
} // closes function reportGeotagCoordinates()

$(function () {
mw.util.addPortletLink('p-cactions', 'javascript:reportGeotagCoordinates()', 'geo', 'ca-geotag', 'Report the article geotag coordinates', 'g', '');
});
 
//</pre>