User:Prolineserver/extiw2.js

From Wikimedia Commons, the free media repository
Jump to navigation Jump to search
Note: After saving, you have to bypass your browser's cache to see the changes. Internet Explorer: press Ctrl-F5, Mozilla: hold down Shift while clicking Reload (or press Ctrl-Shift-R), Opera/Konqueror: press F5, Safari: hold down Shift + Alt while clicking Reload, Chrome: hold down Shift while clicking Reload.
$(FormatExtiwInit);
 
function FormatExtiwInit() {
  // get all links in document
  var a = document.getElementsByTagName('a');
  var j = 0;
  for(i=0; i<a.length; i++)  {
    if(a[i].className == 'extiw') { // look for external interwiki links
      // generate a new class name
      var newClass = encodeURIComponent(a[i].title).replace(/%/g,"xyz").replace(/\./g,"dot");
      // generate the api-url and send it to toolserver
      var url = a[i].href.replace(/\/wiki\//g,"/w/api.php?action=query&titles=");
      url='+encodeURIComponent(url)+'&title='+newClass;
      // replace class names by new name
      a[i].className = newClass;
    }
  }
  // load corresponding css files
  url = '';
  for(i=0; i<j;i++) {
    if(strlen(url+title[i])>1950) // maximal url lengthin IE is 2083
      url = 'http://toolserver.org/~prolineserver/formatlinks.php?'+ url+title[i];
      document.write("<link rel=\"stylesheet\" type=\"text/css\" href=\""+url+"\" />");
      url = '';
    }
  }
}