User:Mike Dillon/Scripts/convert-kanjitab.js

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.

$(function () {
    var textBox = document.getElementById('wpTextbox1');
    if (!textBox) return;
    if (!textBox.value.match(/\{\{ja-kanjitab-top/)) return;
    if (textBox.value.match(/addOnloadHook/)) return;

    var summary = document.getElementById('wpSummary');
    var minor = document.getElementById('wpMinoredit');
    var diff = document.getElementById('wpDiff');
    if (!summary || !minor || !diff) return;

    var li = mw.util.addPortletLink("p-tb", "#", "Replace kanjitab", "ca-kanjitab", "Replace ja-kanjitab-top and ja-kanjitab-bottom");
    var a = li.getElementsByTagName("a")[0];
    a.onclick = function () {
        var re = /\{\{ja-kanjitab-top\}\}\s*!\s*(\[\[[^\]]+\]\](\s*\|\|\s*\[\[[^\]]+\]\])*)\s*\{\{ja-kanjitab-bottom\}\}/;
        var m = textBox.value.match(re);
        if (!m) return false;

        var output = textBox.value.substring(0, m.index);

        var kanji = [];
        var matches = m[1].split(/\s*\|\|\s*/);
        for (var i = 0; i < matches.length; i++) {
            kanji.push(matches[i].replace(/\[\[([^\]]+)\]\]/, '$1'));
        }
        output += '{{ja-kanjitab|' + kanji.join('|') + '}}';

        output += textBox.value.substring(m.index + m[0].length);

        textBox.value = output;

        return false;
    };
});