User:小躍/RedirectionTemplate.js
外观
< User:小躍
注意:保存之后,你必须清除浏览器缓存才能看到做出的更改。Google Chrome、Firefox、Microsoft Edge及Safari:按住⇧ Shift键并单击工具栏的“刷新”按钮。参阅Help:绕过浏览器缓存以获取更多帮助。
//[[en:User:Kaldari/wikilove.js]]變化版<nowiki>
if (( wgAction == 'view' && skin == 'vector' )) {
// Script depends on jQuery dialog widget
mw.loader.using( 'jquery.ui.dialog', function() {
// Construct object (to prevent namespace conflicts)
redirection = {
displayProgress: function( form, message ) {
$('#'+form+' div').hide(); // remove everything else from the dialog box
$('#'+form).append ( $('<div style="text-align:center;margin:3em 0;"></div>').html( message+'<br/><img src="/media/wikipedia/commons/4/42/Loading.gif" />' ) );
},
displayError: function( form, error ) {
$('#'+form+' div').hide(); // remove everything else from the dialog box
$('#'+form).append ( $('<div style="color:#990000;margin-top:0.4em;"></div>').html( '錯誤: '+error ) );
},
getEditToken: function() {
$.ajax({
url: wgScriptPath + '/api.php?',
data: 'action=query&prop=info&intoken=edit&titles=Main%20Page&indexpageids&format=json',
dataType: 'json',
success: function( data ) {
if ( data.query.pages && data.query.pageids ) {
var pageid = data.query.pageids[0];
redirection.editToken = data.query.pages[pageid].edittoken;
}
}
});
},
getEmailable: function() {
// Test to see if the 'E-mail this user' link exists
redirection.emailable = $('#t-emailuser').length ? true : false;
},
editPage: function( form, summary, template ) {
$.ajax({
url: wgScriptPath + '/api.php?',
data: 'action=edit&title='+encodeURIComponent(wgPageName)+'§ion=0&summary='+encodeURIComponent(summary)+'添加重定向模板'+'&appendtext='+encodeURIComponent(template)+'&format=json&token='+encodeURIComponent(redirection.editToken),
dataType: 'json',
type: 'POST',
success: function( data ) {
if ( data.edit.result == "Success" ) {
window.location.reload();
} else {
redirection.displayError( form, 'API結果不明。請嘗試登出維基百科,再次登入維基百科並返回本頁再次加入重定向模板。' );
}
},
error: function( xhr ) {
redirection.displayError( form, '編輯錯誤。請嘗試重新整理本頁,並再次加入重定向模板。' );
//console.debug( xhr.responseText );
}
});
},
sendEmail: function( form, subject, text ) {
$.ajax({
url: wgScriptPath + '/api.php?',
data: 'action=emailuser&target='+encodeURIComponent(wgTitle)+'&subject='+encodeURIComponent(subject)+'&text='+encodeURIComponent(text)+'&format=json&token='+encodeURIComponent(redirection.editToken),
dataType: 'json',
type: 'POST'
});
},
launchDialog: function( interface, emailOption ) {
// If user is emailable, add email checkbox to forms that request it
if ( emailOption && typeof redirection.emailable !== 'undefined' && redirection.emailable ) {
if ( typeof interface.data('emailCheckboxAppended') == 'undefined' ) {
interface.append( $('<div style="margin-top:0.4em;"></div>').html('<input type="checkbox" name="notify"/> 用電郵通知這個用戶(注意:一般新用戶在編輯用戶討論頁時系統會有自動電郵)') );
interface.data('emailCheckboxAppended', true);
}
}
// Open the dialog box
interface.dialog('open');
// Retrieve the edit token so it will be available when the form is submitted
redirection.getEditToken();
},
initialize: function() {
// Function to make a list of images selectable
$.fn.selectItem = function() {
return this.each(function() {
var containerId = this.id;
var customId = 0;
$('#'+containerId+' img').css('border', '8px solid transparent');
$('#'+containerId+' img').each(function() {
if (this.id == '') {
this.id = 'select_img_' + customId;
customId++;
}
$('#'+this.id).click(function() {
$('#'+containerId+'Selected').val(this.title);
$('#'+containerId+' img').css('border', '8px solid transparent');
$('#'+this.id).css('border', '8px solid #AED0EA');
})
});
});
};
// List of all available edittipss
var $edittipsList = [
'別名重定向',
'縮寫重定向',
'英文重定向',
'譯名重定向',
'章節重定向',
'拼寫重定向',
'可能性重定向',
'舊名重定向',
'歷史名稱重定向',
'錯字重定向'
];
// Define edittips interface
$wlEdittipss = $('<div id="edittipsForm" style="position:relative;"></div>')
.append( $('<div style="margin-top:0.4em;"></div>').html( '選擇重定向種類: ' ).append( $('<select id="edittipsSelect" style="padding:1px;vertical-align:baseline;"></select>') ) )
.dialog({
width: 500,
autoOpen: false,
title: '重定向模板',
modal: true,
buttons: { "新增重定向模板": function() {
$(this).dialog({buttons:{}});
if ( typeof redirection.editToken !== 'undefined' ) {
if ( $('#edittipsForm input[name="notify"]').attr('checked') ) {
// Send email
redirection.sendEmail( 'edittipsForm', '維基百科重定向模板設置', '你好 '+wgTitle+'!\n\n有用戶給了你一個關於「'+$('#edittipsSelect').val()+'」的維基百科重定向模板設置,目前在你的中文維基百科用戶討論頁上待你查閱,希望你能積極參與維基百科。' );
}
// Perform edit to page
redirection.displayProgress( 'edittipsForm', '正在向頁面加入重定向模板,請稍等' );
var template = '{{'+$('#edittipsSelect').val()+'}}' ;
redirection.editPage( 'edittipsForm', '', template );
} else {
redirection.displayError( 'edittipsForm', '無法完成編輯' );
}
}}
});
// Populate edittips select list
$.each( $edittipsList, function(index, value) { $('#edittipsSelect').append( $('<option></option>').val(value).html(value) ); });
// Insert new tab into page
var menuEdittips = '<li id="wl-edittips"><span><a onclick="redirection.launchDialog( $wlEdittipss, true ); return false;" href="#">選擇重定向的模板</a></span></li>';
var redirectionMenu = '<div class="vectorMenu" id="p-redirection"><h5><span>重定向模板</span><a href="#" style="padding:0 0.4em;width:auto;"><span style="display:inline-block;height:2.5em;width:16px;margin-right:14px;background-position:0 60%;background-image:url(/media/wikipedia/commons/thumb/5/52/Crystal_Clear_app_ktip.png/20px-Crystal_Clear_app_ktip.png);background-repeat:no-repeat;"> </span></a></h5><div class="menu"><ul>'+menuEdittips+'</ul></div></div>';
$('#p-cactions').after(redirectionMenu);
} // close initialize function
} // close redirection object
redirection.initialize();
redirection.getEmailable();
}) // close mw.loader
} // close if
//</nowiki>