Медијавики:Gadget-weatherConvert.js

Извор: Викиновости

Напомена: Пошто објавите измене, можда ћете морати да обришете кеш прегледача како бисте их видели.

  • Firefox / Safari: Држите Shift и кликните на Reload или притисните Ctrl-F5 или Ctrl-R (⌘-R на Mac-у).
  • Google Chrome: Притисните Ctrl-Shift-R (⌘-Shift-R на Mac-у).
  • Internet Explorer / Edge: Држите Ctrl и кликните на Refresh или притисните Ctrl-F5.
  • Opera: Притисните Ctrl-F5.
//Add convert C<->f button to weather pages
/*extern getElementsByClassName, new_element, importScript addOnloadHook*/

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]) {
    importScript("User:Bawolff/sandbox/stuff.js");
    //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();
    
}
});