User:Bawolff/sandbox/cat2tag.js

From Wikinews, the free news source you can write!
Jump to navigation Jump to search

Note: After saving, you may have to bypass your browser's cache to see the changes. Mozilla / Firefox / Safari: hold down Shift while clicking Reload, or press Ctrl-Shift-R (Cmd-Shift-R on Apple Mac); IE: hold Ctrl while clicking Refresh, or press Ctrl-F5; Konqueror: simply click the Reload button, or press F5; Opera users may need to completely clear their cache in Tools→Preferences. — More skins

//A hide-away category to tagging trick. removes tags on load. Finds Categories, appends them as tags at bottom on save (or any submit)
//problems with this: this is really something that should be done server side. (and in the end, this might be done server side, or we might not even do this)
//note: this script, like all my edits is in the public domain. 

//Todo: make this work with WikiEd, test, make robust. At this point, this script is just to show such a thing could work.
/*extern wgAction  addOnloadHook wgNamespaceNumber*/
if ((wgAction === "edit") && (wgNamespaceNumber === 0)) {
    var tag = {};
    tag.cat = /\[\[[cC]ategory:([^\]]+)\]\]/g;

    tag.makeTags = function () {
        var editbox = document.getElementById("wpTextbox1").value;
        var categories = editbox.match(tag.cat);
        editbox += "\n<!--Start technocrati Tags." + " Do not modify on or below this line!-->";
        editbox += '<div id="technocratiTags" style="display:none;">';
        for (var i = 0; i < categories.length;i++) {
            editbox += "<tag>";
            editbox += categories[i].replace(tag.cat, "$1");
            editbox += "</tag>";
        }
        editbox += "</div>";
        document.getElementById("wpTextbox1").value = editbox;
        return true; //make sure it submits
    }


    tag.hideTags = function () {
//if you assign doc.getElementById(testarea) to a var, it doesn't seem to work
document.getElementById("wpTextbox1").value = document.getElementById("wpTextbox1").value.replace(/\n\<\!--Start\stechnocrati\sTags\.\sDo\snot\smodify\son\sor\sbelow\sthis\sline\!--\>\s?\<div\sid\=\"technocratiTags\"\sstyle\=\"display\:none\;\"\>[\d\D]+\<\/div\>\s?$/, "");
    }

    //Attach the unhide to save button
    tag.attach = function () {
        var editform = document.getElementById("editform");
        if (!editform) throw new Error("No edit box! Problem with script, please tell someone.");

        if (editform.addEventListener) {
            editform.addEventListener("submit", tag.makeTags, true);
        }
        else if (editform.attachEvent) {
            editform.attachEvent("onsubmit", tag.makeTags);
        }
        else {
            editform.onsubmit = tag.makeTags;
        }
    }

    addOnloadHook(tag.hideTags);
    addOnloadHook(tag.attach);

}