User:Kempelen/common.js

From OpenStreetMap Wiki
Jump to: navigation, search

Note: After saving, you may have to bypass your browser's cache to see the changes.

  • Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
  • Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
  • Internet Explorer: Hold Ctrl while clicking Refresh, or press Ctrl-F5
  • Opera: Clear the cache in Tools → Preferences
/* 
 * Translators helper script v0.2
 * 
 * When viewing a HU: translated page, marks links 
 *  Border: not HU: link, but a HU: page exists - needs link fixed
 *  Green: not HU: - may need translation
 */

$("#p-logo a").attr('href', '/wiki/HU:Main_Page');

// Work on Hungarian pages only.
if(window.location.href.match(/(HU:|WikiProject_Hungary)/)) {

  var pageName = $("#firstHeading").html();

  var json = { 'action':'query', 
            'format':'json', 
            'indexpageids':'1', 
            'prop':'links', 
            'pllimit':'500', 
            'titles': pageName 
           };

  $.ajax({
    type: "GET",
    url: "/w/api.php",
    data: json,
    dataType: 'json',
    success: function( jsondata ){

       var id = jsondata.query.pageids[0];
       var title = jsondata.query.pages[id].title.replace(/^HU:/, '');
       var links = jsondata.query.pages[id].links;
       var l = "";

       // Ignore links to other translations and already Hungarian pages
       var rx = new RegExp("^([a-zA-Z-]+:)?" + title + "$");
       var rx2 = new RegExp("^(HU:|WikiProject Hungary)");

       for(var i = 0; i < links.length; i++) {

         if(!links[i].title.match(rx) && !links[i].title.match(rx2)) {

           // Make all "English" links green
           var href = encodeURIComponent(links[i].title.replace(/ /g, "_"));
           $("a[href*=/wiki/" + href + "]").css('color','green');

           // Collect all non-HU pages with HU: prefix for checking for translation
           l += "HU:" + links[i].title + "|";
         }
       }
       l = l.substring(0, l.length -1); // cut "|"

       checklinks(l);

    }
  });
}

// Checks if the HU: pages exist, while above we found no-HU link to it!
// titles are separated with pipe | and are all prefixed with HU:
function checklinks(titles) {

  var json = { 'action':'query', 
    'format':'json', 
    'indexpageids':'1', 
    'pllimit':'500', 
    'titles': titles 
  };
//alert(titles);

  $.ajax({
    type: "GET",
    url: "/w/api.php",
    data: json,
    dataType: 'json',
    success: function( jsondata ) {
      for(var i = 0; i < jsondata.query.pageids.length; i++) {
        var id = jsondata.query.pageids[i];

        // Negative means no such HU: page exists, so no problem
        if(id > 0) {

          // HU exists, but the nonHU was linked, red border!
          var title = jsondata.query.pages[id].title.replace(/^HU:/, '').replace(/ /g, "_");
          $("a[href*=/wiki/" + title + "]").css('border','red solid');
        }
      }
    }
  });

}