User:Hamaryns/monobook.js

From Wiktionary, the free dictionary
Jump to navigation Jump to search

Note: You may have to bypass your browser’s cache to see the changes. In addition, after saving a sitewide CSS file such as MediaWiki:Common.css, it will take 5-10 minutes before the changes take effect, even if you clear your cache.

  • Mozilla / Firefox / Safari: hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (Command-R on a Macintosh);
  • Konqueror and Chrome: click Reload or press F5;
  • Opera: clear the cache in Tools → Preferences;
  • Internet Explorer: hold Ctrl while clicking Refresh, or press Ctrl-F5.

This JavaScript is executed for Hamaryns on every page load, when User:Hamaryns is using the Monobook skin.


/* <pre><noinclude> */

// Parse lines into array.  Search for "{{checktrans}}"...process from that line
// until the next heading (any line starting with "=".)
//  For each line, if in the *Langname: [[translation]]...
//  then change it to *{{ttbc|Langname}}: [[translation]]...
//  and strip out {{top}}/{{mid}}/{{bot}} from that section.
function checkTrans() {

// debug with Firebug
    console.debug();

    var l;
    oldtext = txt;
    var newtxt = "";

    if (txt.search(/\{\{checktrans\}\}/gi) == -1) return;
    if (txt.search(/\{\{ttbc\|/g) != -1) return; // remove?
    l = txt.split("\n");

    var ok = true;

    for (line in l) {
      var line2 = line;

      if (!ok && line.search(/^=/) != -1) {
          ok = true;
      }
  
      if (ok && line.search(/\{\{checktrans\}\}/gi) != -1) {
          ok = false;
      }
  
      if (!ok) { // get rid of this
        line2 = line2.replace(/\{\{top\}\}/g, "");
        line2 = line2.replace(/\{\{mid\}\}/g, "");
        line2 = line2.replace(/\{\{bottom\}\}/g, "");
      }
  
      if (line.search(/^\* /) != -1) {
        line = "*" + line.substring(2);
      }
  
      if (!ok) {
        if (line.search(/\{\{ttbc/) == -1 && line.search(/^\*(.*?): /) != -1) {
          var chrat = line.search(/: /);
          if (line.substring(1, chrat).search(/\:/g) == -1) {
            line2 = "*{{ttbc|" + 
              line.substring(1,chrat).replace(/\[|\]|\*/g, "") + "}}:" + 
              line.substring(chrat+1);
          }
        }
      }
  
      newtxt = newtxt + line2 + "\n";
    }

    txt = newtxt;
    comment("ttbc");
}

// Parse lines into array.  Search for "*Dutch:"... 
// replace any definition in there with {{t|nl|...}}
function replaceDutch() {

// debug with Firebug
    console.debug();

    var l;
    oldtext = txt;
    var newtxt = "";

    if (txt.search(/\*Dutch\:\s*\[\[/gi) == -1) return;
    l = txt.split("\n");

    for (line in l) {
      var line2 = line;

      if (line.search(/\{\{t\|nl\|/) == -1 && line.search(/^\*Dutch:\s*\[\[/) != -1) {
        line2 = line.replace(/[[/, "{{t|nl|").replace(/]]/, "}}");
      }

      newtxt = newtxt + line2 + "\n";
    }

    txt = newtxt;
    comment("{{temp|t}}");
  }

function myFunctions() {
    replaceDutch;
    checkTrans;
}

addLoadEvent(myFunctions);