User:Alexchris/link-ts.js

// //Link Translator //由User:Liangent最初設計 //由User:Kovl修改 v2015-2-6-7-52 //由user:Panintelize 再修改.2016-02-06 //由User:和平奮鬥救地球 翻譯為繁體/正體版本.2016-06-08 //源代码取自User:Liangent/Gadgets/Toolkit/linktranslator.uncompressed.js //v2015-2-6-7-52 //此腳本不依賴其他腳本

$(function { mw.loader.using(['jquery.ui']); var LTUI = {   Translate:"翻譯",    TranslateLinks:"翻譯連結",    LinkTranslator:"連結翻譯器",    TLTitle:"自動翻譯從其他語言維基百科複製的文本",    SourceLanguageCode:"目標維基的語言代碼：",    OriginalLink:"原連結：",    NOLINKINPAGE:"此頁沒有任何連結",    Done:"完成",    NoWikiEd:"linktranslator.js與WikiEd不相容，请於頁面右上角暫時禁用WikiEd. ",    EditMessage:"由link translatortranslated；",    //OPTION    KeepOriginalText:"以原文顯示綠連:",    KeepOriginalText2:"以原文顯示藍連:",    CommentOriginalLink:"注釋原連結：",    UseLangLink:"跨語言連結：",    //STATUS    PARSEFAILED:"解析失敗",    ERROR:"錯誤",    NOLINK:"没有連結",    MULTIPLELINK:"多個連結",    PAGESAME:"頁面相同",    PAGEDIFF:"頁面不同",    DONTEXIST:"頁面不存在" };

var LTConf = { SourceLanguageCode:"en", KeepOriginalText:"checked", //"checked" OR "" KeepOriginalText2:"checked", CommentOriginalLink:"", UseLangLink:"checked" };

//clear previous button $('#wpLinktranslator').remove;

// secure server? if ((wgAction == 'edit' || wgAction == 'submit') && wgServer == '//en.wikipedia.org') { $('#wpDiff').after('\n'); $('#wpLinktranslator').click(LTClick); }

//variables var jobid = 0; var ldsb = '__LEFT_DOUBLE_SQUARE_BRACKETS__'; var EXEConf;

//on click "Translate links" #wpLinktranslator function LTClick(event) { event.preventDefault; $('#linktranslator').remove; if ($("#wikEdFrameWrapper").css("visibility") == "visible"){ alert(LTUI.NoWikiEd); return; }   $('' +        '' + LTUI.SourceLanguageCode + ' ' +        ' ' +        '' + LTUI.KeepOriginalText + ' ' +        ' ' +        '' + LTUI.KeepOriginalText2 + ' ' +        ' ' +        '' + LTUI.CommentOriginalLink + ' ' +        ' ' +        '' + LTUI.UseLangLink + ' ' + ' ' +       ' ' ).dialog({ modal: false, close: function {jobid++;}, width: 500 });   $('#linktranslator-translate').click(TClick); }

//on click "Translate" #linktranslator-translate function TClick(event) { event.preventDefault; var thisjobid = jobid; // or in #wpLinktranslator's click event? EXEConf = { KeepOriginalText:$('#linktranslator-keep-original').prop('checked'), KeepOriginalText2:$('#linktranslator-keep-original2').prop('checked'), CommentOriginalLink:$('#linktranslator-comment-link').prop('checked'), UseLangLink:$('#linktranslator-lang-link').prop('checked') };   LTConf.SourceLanguageCode = $('#linktranslator-source-lang').val; var api = 'http://' + LTConf.SourceLanguageCode + '.wikipedia.org/w/api.php'; if (document.location.protocol == 'https:') { api = 'https://' + LTConf.SourceLanguageCode + '.wikipedia.org/w/api.php'; }   var wikitext = $('#wpTextbox1').val; // how to make a set to avoid duplicated links? var links = $('#wpTextbox1').val.match(/(\)/g); if (links === null) { $('#linktranslator').text(LTUI.NOLINKINPAGE); return; } else { // assert links.length != 0 here. $('#linktranslator').dialog( "option", "position", { my: "top", at: "top"} ); $('#linktranslator').html(' '); $('#linktranslator-progressbar').progressbar; }   var respcount = 0; // TODO: should be rewritten using jQuery.each function eachlink(i) { var linkidx = i;       var link = links[i].slice(2, -2); var linktarget = link; // TODO: pipe tricks like /subpage/? var linkdisplay = link; var idx = link.indexOf('|'); if (idx != -1) { linktarget = link.substring(0, idx); linkdisplay = link.substring(idx + 1); }       $('#linktranslator').append(' '); $('#linktranslator-item-' + i).text(links[i] + ' -> ') .append(' ... '); // TODO: if newtarget == linkdisplay? // TODO: cannot identify if missing or (for main page, also blank text) $.ajax({           data: {                action: 'parse',                format: 'json',                page: linktarget,                prop: 'langlinks',                redirects: 1            },            dataType: "jsonp",            type: 'POST',            url: api,            success: function(data) {                console.log(data);                if (thisjobid != jobid) {                    return;                }                var llink;                var newtarget;                var newlinks;                if(data.parse){                	//request successful                    llink = $.grep(data.parse.langlinks, function(e){ return e.lang === 'en'; });                } else if(data.error.info){                	//request completed with error                    if(data.error.info == "The page you specified doesn't exist")                        {$('#linktranslator-item-' + linkidx + ' .linktranslator-item-newlink').text(LTUI.DONTEXIST);} else {$('#linktranslator-item-' + linkidx + ' .linktranslator-item-newlink').text(LTUI.ERROR + '(' + data.error.info + ')');} return; } else{ //error without info field $('#linktranslator-item-' + linkidx + ' .linktranslator-item-newlink').text(LTUI.PARSEFAILED); return; }               if (llink.length === 1) { newtarget = llink[0]["*"]; if(linktarget === newtarget){ $('#linktranslator-item-' + linkidx + ' .linktranslator-item-newlink').text(LTUI.PAGESAME); return; }                   //EXEConf.KeepOriginalText if(EXEConf.KeepOriginalText2) { newlinks = ldsb + newtarget + '|' + linkdisplay + ']]'; } else { newlinks = ldsb + newtarget + ']]'; }               } else if (llink.length === 0) { if(EXEConf.UseLangLink) { newlinks = '{{link-' + LTConf.SourceLanguageCode + '||' + ''; if(EXEConf.KeepOriginalText) { newlinks = '{{tsl|' + LTConf.SourceLanguageCode + '|' + linktarget + '||' + linkdisplay +'}}'; } else { newlinks = '{{tsl|' + LTConf.SourceLanguageCode + '|' + linktarget + '}}'; }                   } else { $('#linktranslator-item-' + linkidx + ' .linktranslator-item-newlink').text(LTUI.NOLINK); return; }               } else { $('#linktranslator-item-' + linkidx + ' .linktranslator-item-newlink').text(LTUI.MULTIPLELINK); return; }               //EXEConf.CommentOriginalLink var newcomment; if(EXEConf.CommentOriginalLink) { newcomment = ''; } else { newcomment = ''; }               //mark on dialogue $('#linktranslator-item-' + linkidx + ' .linktranslator-item-newlink').text(newlinks.replace(new RegExp(ldsb, 'g'), '[['));               // only replacing the first is ok, we will run this many times                wikitext = wikitext.replace(links[linkidx], newlinks + newcomment);            },            error: function(jqXHR, textStatus, errorThrown) {                if (thisjobid != jobid) {                    return;                }                $('#linktranslator-item-' + linkidx + ' .linktranslator-item-newlink').text(LTUI.ERROR + '(' + textStatus + ')');            },            complete: function {                if (thisjobid != jobid) {                    return;                }                respcount++;                $('#linktranslator-progressbar').progressbar('value', respcount * 100 / links.length);                if (respcount >= links.length) {                    $('#wpTextbox1').val(wikitext.replace(new RegExp(ldsb, 'g'), '[['));                    $('#linktranslator').prepend(' ' + LTUI.Done + ' ');                }            }        });//ajax complete    }//trino complete    for (var i = 0; i < links.length; i++) {eachlink(i);}    $('#wpSummary').val(LTUI.EditMessage + $('#wpSummary').val); }

//end }); //