User:Reza1615/test.js

// /*jslint regexp: true, indent: 4 */ /*global $: false, wgNamespaceNumber: false, autoStart: false, wgAction: false, homeWiki: true, translatorFormat: true, translatorKeepLinksLables: true */ if (typeof homeWiki === "undefined") { var homeWiki = "fa"; } if (typeof translatorFormat === "undefined") { var translatorFormat = "($1: $2)"; } if (typeof translatorKeepLinksLables === "undefined") { var translatorKeepLinksLables = false; } function Translator { "use strict"; var translationTextArea; function queryTranslationFromData(data) { var XMLDocument = $(data), XMLQuery = 'll[lang="' + homeWiki + '"]'; return XMLDocument.find(XMLQuery).text; }   function commitOnNode(node, translatedTitle) { var injectionString = translatorFormat.replace("$1", homeWiki).replace("$2", translatedTitle); node.append(injectionString); }   function commitOnTranslationTextarea(title, translatedTitle) { translationTextArea.val(translationTextArea           .val            .replace( new RegExp("(\\{\\{)" + title + "(\\}\\})"), "$1" + translatedTitle + (translatorKeepLinksLables ? "$2" : "") + "$3" )           );    }    function commit(data, node, title) { var translatedTitle = queryTranslationFromData(data); if (translatedTitle !== "") { if (node === null) { commitOnTranslationTextarea(title, translatedTitle); } else { commitOnNode(node, translatedTitle); }       }                }    function translateFromLanguageLink(title, node) { $.get(wgScriptPath + "/api.php?action=query&prop=langlinks&titles=" + title + "&redirects=&format=xml&lllimit=500",              function (data) { commit(data, node, title); }); }   this.run = function  { if (wgAction === "view" || wgAction === "purge" || wgAction === "historysubmit") { $("#bodyContent a").each(function {                var iter = $(this),                    title = iter.attr("title");                if (title !== undefined) {                    translateFromLanguageLink(title, iter);                }            }); } else if (wgAction === "edit" || wgAction === "submit") { translationTextArea = $("#wpTextbox1") .clone .attr({"id": "wpTextbox2"}) .css({"background-color": "#CCCEFF"}); $("#wpTextbox1").before(translationTextArea); var matched = translationTextArea.val.match(/\{\{.*?\}\}/g), i,               title; for (i = 0; i < matched.length; i = i + 1) { // i don't like use for in, because old IE can not support it               title = matched[i].replace(/\{\{?.*?\}\}/g, "$1"); translateFromLanguageLink(title, null); }       }    }; }

var translator = new Translator; $(function {    "use strict";    if (typeof autoStart !== "undefined") {        if (autoStart === true) {            translator.run;        }    } else {        $("h1").append(' (translate links to ' + homeWiki + '!)');    } });