User:魔琴/gadgets/addWSFR/index.js
外观
注意:保存之后,你必须清除浏览器缓存才能看到做出的更改。Google Chrome、Firefox、Microsoft Edge及Safari:按住⇧ Shift键并单击工具栏的“刷新”按钮。参阅Help:绕过浏览器缓存以获取更多帮助。
//<nowiki>
mw.loader.using( ['mediawiki.util', 'mediawiki.api', 'ext.gadget.site-lib'/*,'ext.gadget.HanAssist'*/ ], function () {
mw.loader.load('/w/index.php?title=User:魔琴/gadgets/Re/Re.css&action=raw&ctype=text/css', 'text/css'); // 直接使用Re的CSS
function han (s,t) {
return wgULS( s, t );
//return HanAssist.localize( { hans: s, hant: t } );
}
var aWSFR = {};
var dl = null;
var html = '';
var summaryAd = '[[User:魔琴/gadgets/addWSFR|added {{Wikisource further reading}}]]';
var wsfrtext = '== 延伸阅读 ==\n\n{{Wikisource further reading}}\n\n'
if (mw.config.get('wgAction')==='edit') {
if (window.location.href.search(/awsfr=1/) >= 0) {
$('#wpTextbox1').val(wsfrtext+$('#wpTextbox1').val());
}
return;
}
if (!mw.config.get('wgIsArticle') || mw.config.get('wgNamespaceNumber')!== 0 ) return;
var pagename = mw.config.get('wgPageName');
API = new mw.Api();
if ($('.mw-editsection').length > 0) {
var awsfrBtn = $('<span>').append($('<span>').addClass('mw-editsection-divider').text(' | ')).append($('<a>').addClass('awsfr-prepend').attr('href', 'javascript:void(0)').text('aWSFR插入')).append($('<span>').addClass('mw-editsection-divider').text(' | ')).append($('<a>').addClass('awsfr-replace').attr('href', 'javascript:void(0)').text('aWSFR替换'));
$( "h2 .mw-editsection" ).each(function(){
var _awsfrBtn = awsfrBtn.clone();
$(this).find('.mw-editsection-bracket').last().before(_awsfrBtn);
});
}
$('.awsfr-prepend').click(function(){
section = $(this).closest("h2").find(".mw-editsection").find("a").attr("href").replace(/^.*section=(\d*).*$/g,'$1');
var promise = API.postWithToken( 'csrf', {
action: 'edit',
title: pagename,
section: section,
prependtext: wsfrtext,
summary: summaryAd
} ).then( function ( result ) {
mw.notify( han('已经插入,请等待刷新后检查', '已經插入,請等待重新整理後檢查') );
setTimeout( function () {
window.location.href = mw.util.getUrl( pagename );
}, 4000 );
}, function ( err ) {
mw.notify( han('编辑失败,请重试或手动编辑', '編輯失敗,請重試或手動編輯') );
console.log(err);
});
});
$('.awsfr-replace').click(function(){
aWSFR = {};
aWSFR.section = $(this).closest("h2").find(".mw-editsection").find("a").attr("href").replace(/^.*section=(\d*).*$/g,'$1');
aWSFR.replace_para = {
action: 'edit',
title: pagename,
text: wsfrtext,
summary: summaryAd
};
preview();
});
function preview() {
var previewPromise = API.post({
action: 'parse',
contentmodel: 'wikitext',
text: wsfrtext,
pst: 'true', // PST = pre-save transform; this makes substitution work properly
title: pagename,
prop: 'text',
formatversion: '2'
}).done(function(data){
var dl = null;
var html =
'<div id="replace-preview" class="mw-body-content">' + data.parse.text + '</div>';
if (dl) {
dl.html(html).dialog("open");
return null;
}
dl = $(html).dialog({
title: han('预览','預覽'),
minWidth: 600,
minHeight: 100,
buttons: [
{
text: han('确定','確定'),
click: function() {
action();
$(this).dialog('close');
}
},
{
text: han('手动编辑','手動編輯'),
click: function() {
window.location.href = '/w/index.php?title=' + pagename + '&awsfr=1&action=edit§ion=' + aWSFR.section;
}
},
{
text: '取消',
click: function() { $(this).dialog('close'); }
}
]
});
}).fail(function(error) {
mw.notify( han('预览失败','預覽失敗') + error );
});
}
function action() {
var promise = API.postWithToken( 'csrf', {
action: 'edit',
title: pagename,
section: aWSFR.section,
text: '== 延伸阅读 ==\n\n{{Wikisource further reading}}\n\n',
summary: summaryAd
} ).then( function ( result ) {
mw.notify( han('已经替换,请等待刷新后检查', '已經替換,請等待重新整理後檢查') );
setTimeout( function () {
window.location.href = mw.util.getUrl( pagename );
}, 4000 );
}, function ( err ) {
mw.notify( han('编辑失败,请重试或手动编辑', '編輯失敗,請重試或手動編輯') );
console.log(err);
});
}
});
//</nowiki>