User:Liangent/Gadgets/Toolkit/linktranslator.js
外观
注意:保存之后,你必须清除浏览器缓存才能看到做出的更改。Google Chrome、Firefox、Microsoft Edge及Safari:按住⇧ Shift键并单击工具栏的“刷新”按钮。参阅Help:绕过浏览器缓存以获取更多帮助。
// '''Compressed version''' // [[User:Liangent/Gadgets/Toolkit/linktranslator.uncompressed.js|Uncompressed version]] //
require(['jquery','jqueryui'],function(){(function($){$(function(){var jobid=0;var ldsb='__LEFT_DOUBLE_SQUARE_BRACKETS__';if((wgAction=='edit'||wgAction=='submit')&&wgServer=='http://wikicn.playgoteam.workers.dev'){$('#wpDiff').after('\n<input id="wpLinktranslator" value="Translate links" '+'title="Translate links in text copied from another Wikipedia" type="button" />');$('#wpLinktranslator').click(function(event){event.preventDefault();$('#linktranslator').remove();$('<div id="linktranslator" title="Link translator">'+'<label for="linktranslator-source-lang">Source language code:</label> '+'<input id="linktranslator-source-lang" value="en" type="text" /><br />'+'<input id="linktranslator-translate" value="Translate" type="button" /></div>').dialog({modal:true,close:function(){jobid++;}});$('#linktranslator-translate').click(function(event){event.preventDefault();var thisjobid=jobid;var api='http://'+$('#linktranslator-source-lang').val()+'.wikipedia.org/w/api.php';if(document.location.protocol=='https:'){api='https://secure.wikimedia.org/wikipedia/'+$('#linktranslator-source-lang').val()+'/w/api.php';}
var wikitext=$('#wpTextbox1').val();var links=$('#wpTextbox1').val().match(/\[\[.+?\]\]/g);if(links==null){$('#linktranslator').text('There is not any link on page.');return;}else{$('#linktranslator').html('<div id="linktranslator-progressbar"></div>');$('#linktranslator-progressbar').progressbar();}
var respcount=0;try{netscape.security.PrivilegeManager.enablePrivilege("UniversalBrowserRead");}catch(e){}
for(var i=0;i<links.length;i++){(function(){var linkidx=i;var link=links[i].slice(2,-2);var linktarget=link;var linkdisplay=link;var idx=link.indexOf('|');if(idx!=-1){linktarget=link.substring(0,idx);linkdisplay=link.substring(idx+1);}
$('#linktranslator').append('<div id="linktranslator-item-'+i+'"></div>')
$('#linktranslator-item-'+i).text(links[i]+' -> ').append('<span class="linktranslator-item-newlink">...</span>');$.ajax({data:{action:'parse',format:'xml',page:linktarget,prop:'langlinks',redirects:1},type:'POST',url:api,success:function(data){if(thisjobid!=jobid){return;}
var newtarget=$('api>parse>langlinks>ll[lang=zh]',data).text();if(newtarget==''){$('#linktranslator-item-'+linkidx+' .linktranslator-item-newlink').text('FAILED');}else{var newlinks=ldsb+newtarget+'|'+linkdisplay+']]';$('#linktranslator-item-'+linkidx+' .linktranslator-item-newlink').text(newlinks.replace(new RegExp(ldsb,'g'),'[['));wikitext=wikitext.replace(links[linkidx],newlinks+'<!-- Original: '+ldsb+link+']] -->');}},error:function(){if(thisjobid!=jobid){return;}
$('#linktranslator-item-'+linkidx+' .linktranslator-item-newlink').text('ERROR');},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('<div id="linktranlator-done"><strong>Done</strong></div>');}}});})();}});});}});})(jQuery);});