User:SunAfterRain/js/translatevariants.js
< User:SunAfterRain | js
注意:保存之后,你必须清除浏览器缓存才能看到做出的更改。Google Chrome、Firefox、Microsoft Edge及Safari:按住⇧ Shift键并单击工具栏的“刷新”按钮。参阅Help:绕过浏览器缓存以获取更多帮助。
/**
* 使用 [[User:Xiplus/js/TranslateVariants.js]] https://zh.wikipedia.org/w/index.php?title=User:Xiplus/js/TranslateVariants.js&oldid=64883574 修改而成
* 用來轉換非MediaWiki頁面
**/
(function() {
if (mw.config.get('wgPageName').match(/(^(Draft:|)Media[Ww]iki:[^/]+(\/zh)?$|^(Template|Wikipedia):(.*)\/zh$|[Ee]ditnoince(\/zh|)$)/)) {
mw.loader.using(['mediawiki.api', 'mediawiki.ForeignApi', 'mediawiki.diff.styles']).then(function() {
let link = mw.util.addPortletLink(
'p-cactions',
'#',
'轉換變體'
);
$(link).on('click', function() {
this.remove();
main();
});
});
} else {
return;
}
mw.messages.set({
TranslateVariantsSummary: '自動轉換變體自[[$1]]的[[Special:PermanentLink/$2|版本$2]] via [[User:SunAfterRain/translatevariants.js]]'
});
function main() {
const langs = ['zh','zh-hans', 'zh-cn', 'zh-my', 'zh-sg', 'zh-hant', 'zh-tw', 'zh-hk', 'zh-mo'];
const langname = {
'zh': '原始',
'zh-hans': '简体',
'zh-cn': '大陆简体',
'zh-my': '大马简体',
'zh-sg': '新加坡简体',
'zh-hant': '繁體',
'zh-tw': '臺灣正體',
'zh-hk': '香港繁體',
'zh-mo': '澳門繁體'
};
let result = {};
var api = new mw.Api();
var zhwpapi;
if (mw.config.get('wgDBname') === 'zhwiki') {
zhwpapi = new mw.Api();
} else {
//zhwpapi = new mw.ForeignApi('//zh.wikipedia.org/w/api.php');
zhwpapi = new mw.Api({
ajax: {
url:'//zh.wikipedia.org/w/api.php'
}
});
}
var basepagetext = '';
let table = $('<div id="TranslateVariants">').prependTo('#bodyContent');
$('<div style="color:red">提醒:TranslateVariants工具使用<a href="/wiki/Module:CGroup/MediaWiki_special">MediaWiki special</a>兩個轉換組進行自動轉換,請確認轉換結果是否正確!</div>').appendTo(table);
var defaultlangs = 'zh,zh-hans,zh-cn,zh-my,zh-sg,zh-hant,zh-tw,zh-hk,zh-mo';
var runlangs = prompt('轉換以下語言(以逗號隔開):', defaultlangs);
if (runlangs === null) {
runlangs = defaultlangs;
}
var langqueue = runlangs.split(',').map(function(lang) {
return lang.trim();
}).filter(function(lang) {
return langs.indexOf(lang) !== -1;
});
api.get({
action: 'query',
prop: 'revisions',
rvprop: ['content', 'timestamp'],
titles: [mw.config.get('wgPageName')],
formatversion: '2',
curtimestamp: true
}).then(function(data) {
var page, revision;
if (!data.query || !data.query.pages) {
return $.Deferred().reject('unknown');
}
page = data.query.pages[0];
if (!page || page.invalid) {
return $.Deferred().reject('invalidtitle');
}
if (page.missing) {
return $.Deferred().reject('nocreate-missing');
}
revision = page.revisions[0];
return {
revid: page.revisions[0].revid,
content: revision.content
};
}).then(function(data) {
let text = data.content;
result.zh = text;
text = text.replace(/[[\]{}<>|:*'_#&\s]/gim, function(s) {
return "&#" + s.charCodeAt(0) + ";";
});
text = text.replace(/([[)((?:(?!|)(?!]).)+?)(|(?:(?!]).)+?]])/g, '$1-{$2}-$3');
text = text.replace(/-{(.+?)}-/g, function(s) {
return s
.replace('-{', '-{')
.replace('}-', '}-')
.replace(/|/g, '|')
.replace(/ /g, ' ')
.replace(/=/g, '=')
.replace(/>/g, '>')
.replace(/:/g, ':');
});
text = text.replace(/-{}-/g, '');
basepagetext = text;
process(data.revid);
});
function process(revid) {
if (langqueue.length === 0) {
return;
}
var lang = langqueue.shift();
var diffTable = $('<div id="TranslateVariants-diff-' + lang + '">').appendTo(table);
$('<hr>').appendTo(table);
const basename = mw.config.get('wgPageName').replace(/\/zh$/, '');
var targetTitle;
if (lang == 'zh') {
targetTitle = basename + '';
} else {
targetTitle = basename + '/' + lang;
}
var newtext;
zhwpapi.parse(
'{{NoteTA|G1=MediaWiki|G2=MediaWiki special}}<div id="TVcontent">' + basepagetext + '</div>',
{
'uselang': lang,
'prop': 'text'
}
).then(function(data) {
newtext = $('<div/>').html(data).find('#TVcontent').text();
return api.post({
action: 'query',
prop: 'revisions',
titles: [targetTitle],
rvdifftotext: newtext,
formatversion: '2',
});
}, function(err) {
mw.notify('解析' + lang + '時發生錯誤:' + err);
}).then(function(data) {
let tool = $('<div><a href="' + mw.util.getUrl(targetTitle) + '">' + lang + '(' + langname[lang] + ')</a>(<a href="' + mw.util.getUrl(targetTitle, { action: 'edit' }) + '">編</a>)</div>').appendTo(diffTable);
let page = data.query.pages[0];
let summary = mw.msg('TranslateVariantsSummary', mw.config.get('wgPageName'), revid);
if (page.missing) {
let submit = $('<button style="float: right;">發佈頁面</button>').appendTo(tool);
submit.on('click', function() {
this.remove();
api.create(
targetTitle,
{ summary: summary },
newtext
).then(function() {
mw.notify(
$('<div>').append(
$('<span>').text('已編輯 ')
).append(
$('<a href="' + mw.util.getUrl(targetTitle) + '">').append(
targetTitle
)
)
);
}, function(e) {
mw.notify(
$('<div>').append(
$('<span>').text('編輯 ')
).append(
$('<a href="' + mw.util.getUrl(targetTitle) + '">').append(
targetTitle
)
).append(
$('<span>').text(' 時發生錯誤:' + e)
)
);
});
});
$('<pre>').html(newtext.replace(/[<>&]/gim, function(s) {
return "&#" + s.charCodeAt(0) + ";";
})).appendTo(diffTable);
return;
}
let diff = page.revisions[0].diff.body;
if (diff === '') {
$('<span style="float: right;">無變更</span>').appendTo(tool);
} else {
let submit = $('<button style="float: right;">發佈變更</button>').appendTo(tool);
submit.on('click', function() {
this.remove();
api.edit(
targetTitle,
function() {
return {
text: newtext,
summary: summary,
nocreate: false
};
}
).then(function() {
mw.notify('已編輯 ' + targetTitle);
}, function(e) {
mw.notify('編輯 ' + targetTitle + ' 發生錯誤:' + e);
});
});
$('<table class="diff">').html(diff).prepend('<colgroup><col class="diff-marker"><col class="diff-content"><col class="diff-marker"><col class="diff-content"></colgroup>').appendTo(diffTable);
}
}, function(err) {
mw.notify('取得' + lang + '差異時發生錯誤:' + err);
}).always(function() {
process();
});
}
}
})();