User:Liangent/Gadgets/Toolkit/linktranslator.uncompressed.js
外观
注意:保存之后,你必须清除浏览器缓存才能看到做出的更改。Google Chrome、Firefox、Microsoft Edge及Safari:按住⇧ Shift键并单击工具栏的“刷新”按钮。参阅Help:绕过浏览器缓存以获取更多帮助。
require(['jquery', 'jqueryui'], function() {
(function ($) {
$(function() {
var jobid = 0;
var ldsb = '__LEFT_DOUBLE_SQUARE_BRACKETS__';
// secure server?
if ((wgAction == 'edit' || wgAction == 'submit') && wgServer == 'http://baike.710302.xyz') {
$('#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; // or in #wpLinktranslator's click event?
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();
// how to make a set to avoid duplicated links?
var links = $('#wpTextbox1').val().match(/\[\[.+?\]\]/g);
if (links == null) {
$('#linktranslator').text('There is not any link on page.');
return;
} else { // assert links.length != 0 here.
$('#linktranslator').html('<div id="linktranslator-progressbar"></div>');
$('#linktranslator-progressbar').progressbar();
}
var respcount = 0;
try {
netscape.security.PrivilegeManager.enablePrivilege("UniversalBrowserRead");
} catch (e) {
}
// TODO: should be rewritten using jQuery.each
for (var i = 0; i < links.length; i++) {
(function() {
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('<div id="linktranslator-item-' + i + '"></div>')
$('#linktranslator-item-' + i).text(links[i] + ' -> ')
.append('<span class="linktranslator-item-newlink">...</span>');
// TODO: if newtarget == linkdisplay?
// TODO: cannot identify if missing or [[zh:]] (for main page, also blank text)
$.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'), '[['));
// only replacing the first is ok, we will run this many times
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);
});