User:Dschwen/addkmllinks.js

From Wikipedia, the free encyclopedia

Note: After saving, you have to bypass your browser's cache to see the changes. In Internet Explorer and Firefox, hold down the Ctrl key and click the Refresh or Reload button. Opera users have to clear their caches through Tools→Preferences, see the instructions for Opera. Konqueror and Safari users can just click the Reload button.

var addkmllinks =
{
 config:
 {
  coordurls : new Array ( 
                'http://tools.wikimedia.de/~magnus/geo/geohack.php?',
                'http://www8.brinkster.com/erikbaas/wiki/maps.asp?',
                'http://www.nsesoftware.nl/wiki/maps.asp?' 
              )
 },
 
 // Check against coordinate urls
 isMaplink : function( url )
 {
  if( typeof(url) != 'string' ) return false;
 
  with(addkmllinks)
  {
   // for( var key = 0; key < config.coordurls.length; key++ ) {
   for( var key in config.coordurls ) {
    if( url.substr(0,config.coordurls[key].length) == config.coordurls[key] )
     return true;
   }
  }
 
  return false;
 },
 
 install : function()
 {
  with(addkmllinks)
  {
   var coordnum = 0;
   var len; // cache array length for iterations
   var links = document.getElementsByTagName('a');
   len = links.length;
   for( var key = 0; key < len; key++ )
   {
    link = links[key];
    if( isMaplink(link.href) ) coordnum++;
   } //for
 
   if( coordnum > 1 )
   {
    var content = document.getElementById('content');
 
    var el;
    el = document.createElement('a');
    el.href = 'http://suda.co.uk/projects/microformats/geo/get-geo.php?type=kml&uri=' + encodeURIComponent( document.location );
    el.appendChild( document.createTextNode('Export points of interest') );
    content.appendChild(el);
   }
 
  } //with
 }
 
}
 
//
// Hook up installation function
//
addOnloadHook(addkmllinks.install);
 
//</pre>