MediaWiki:Gadget-weatherConvert.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

try { // containerize gadget, to protect other gadgets if this one goes wrong

//Add convert C<->f button to weather pages
/*extern getElementsByClassName, new_element */

var cvnt = {};
$(function () {

(cvnt.getTemps = function () {
    cvnt.tempC = getElementsByClassName(document, "div", "tempUnitC");
    cvnt.tempC = cvnt.tempC.concat(getElementsByClassName(document, "div", "tempUnitc"));

    cvnt.tempF = getElementsByClassName(document, "div", "tempUnitF");
    cvnt.tempF = cvnt.tempF.concat(getElementsByClassName(document, "div", "tempUnitf"));
})();

cvnt.done = false; //are above stale

if (cvnt.tempF[0] || cvnt.tempC[0]) {
    mw.loader.load( '/w/index.php?title=' + "User:Bawolff/sandbox/stuff.js" + '&action=raw&ctype=text/javascript' );
    //new_element (tagname, attributes/*obj*/, append2/*opt*/)

    cvnt.init = function () {
        var formFrag = document.createDocumentFragment();
        var form = new_element("form", {name: "convertTemps", id: "convertTemps", action: "javascript:cvnt.convert()"}, formFrag);
        new_element("input", {type: "submit", value: "Convert °C ↔ °F", name: "convert"}, form);
        var appendTo = getElementsByClassName(document, "div", "image-label");
        if (appendTo[0]) {
            appendTo = appendTo[0].parentNode;
            appendTo.appendChild(formFrag);
        }
        else {
            appendTo = getElementsByClassName(document, "div", "printFooter");
            appendTo.insertBefore(formFrag);
        }
    }
    cvnt.convert = function () {
        if (cvnt.done) cvnt.getTemps();
        var temp;
        for (var i = 0;i < cvnt.tempC.length;i++) {
            temp = cvnt.toF(cvnt.tempC[i].firstChild.nextSibling.firstChild.nextSibling.nextSibling.firstChild.firstChild.nodeValue);
            cvnt.tempC[i].firstChild.nextSibling.firstChild.nextSibling.nextSibling.firstChild.firstChild.nodeValue = temp;
            temp = null;
            cvnt.tempC[i].className = cvnt.tempC[i].className.replace(/(\\s|^)tempUnit[Cc](\\s|$)/g, "$1tempUnitf$2");
        }
        for (var i = 0;i < cvnt.tempF.length;i++) {
            temp = cvnt.toC(cvnt.tempF[i].firstChild.nextSibling.firstChild.nextSibling.nextSibling.firstChild.firstChild.nodeValue);
            cvnt.tempF[i].firstChild.nextSibling.firstChild.nextSibling.nextSibling.firstChild.firstChild.nodeValue = temp;
            temp = null;
            cvnt.tempF[i].className = cvnt.tempF[i].className.replace(/(\\s|^)tempUnit[Ff](\\s|$)/g, "$1tempUnitc$2");
        }
        cvnt.done = true;
        return void 0;
    }

    cvnt.toF = function (C) {
        return Math.round((C * 1.8) + 32);
    }   
    cvnt.toC = function (F) {
        return Math.round((F - 32) / 1.8);
    } 


    cvnt.init();
    
}
});

} catch (e) { // containerize gadget, to protect other gadgets if this one goes wrong
  // ignore
}