无编辑摘要
imported>=海豚= 无编辑摘要 |
imported>=海豚= 无编辑摘要 |
||
第1行: | 第1行: | ||
/ | $(function(){ | ||
(function( | |||
// tab键改为输出2个空格 | |||
$('#wpTextbox1').keydown(function(e){ | |||
if(e.keyCode == 9){ | |||
e.preventDefault() | |||
var position = this.selectionStart, | |||
left = this.value.substring(0, position), | |||
right = this.value.substring(position), | |||
code = left + ' ' + right | |||
this.value = code | |||
this.selectionStart = this.selectionEnd = position + 2 | |||
} | |||
}) | |||
// AJAX编辑预览代替方案(快捷键为:ctrl + shift + s) | |||
if(/((?!\.css|\.js|模块|module).+)action=(edit|submit)/.test(location.href)){ | |||
setTimeout(function(){ | |||
$('#wikiEditor-ui-toolbar .tabs').append($('<span id="edit-ajaxPreviewBtn" class="tab"><a href="#" role="button" aria-pressed="false">预览</a></span>')) | |||
$('#edit-ajaxPreviewBtn').click(foo) | |||
}, 3000) | |||
var removeBtn = $('<div id="edit-ajaxPreviewRm" style="width:43px; height:30px; text-align:center; line-height:30px; color:black; font-size:26px; background:white; box-shadow:0 0 5px black; opacity:0.8; position:fixed; top:20px; right:0; cursor:pointer; display:none; z-index:1001;">×</div>') | |||
.click(function(){ | |||
viewHide() | |||
}) | |||
$('body').append(removeBtn) | |||
var saveBtn = $('<div id="edit-ajaxPreviewSave" style="width:43px; height:30px; text-align:center; line-height:30px; color:black; font-size:26px; background:white; box-shadow:0 0 5px black; opacity:0.8; position:fixed; top:73px; right:0; cursor:pointer; display:none; z-index:1001;">√</div>') | |||
.click(function(){ | |||
$('#wpSave').click() | |||
}) | |||
$('body').append(saveBtn) | |||
var requestTimeoutKey = 0, | |||
deferred = null | |||
function viewHide(){ | |||
deferred && deferred.abort() | |||
clearTimeout(requestTimeoutKey) | |||
$('#edit-ajaxPreview').remove() | |||
$('#edit-ajaxPreviewRm').hide() | |||
$('#edit-ajaxPreviewSave').hide() | |||
$('body').css('overflow', 'visible') | |||
} | |||
$(window).keydown(function(e){ | |||
if(e.ctrlKey && e.keyCode == 83){ | |||
if($('#edit-ajaxPreview').length){ | |||
viewHide() | |||
}else{ | |||
foo(e) | |||
} | |||
} | |||
}) | |||
var api = new mw.Api() | |||
function foo(e){ | |||
var previewElement = $('<div id="edit-ajaxPreview" style="width:90%; min-width:800px; height:90%; min-height:400px; padding:15px; background:white; box-shadow:0 0 15px #666; overflow:auto; position:fixed; margin:auto; top:0; left:0; right:0; bottom:0; z-index:1000;">').append('<span id="edit-ajaxPreview-LoadingMsg" style="font-size:30px; position:fixed; top:50%; left:50%; transform:translate(-50%, -50%);"> 获取预览中...</span>') | |||
$('body').append(previewElement) | |||
var wikiText = $('#wpTextbox1').val(), | |||
titleName = decodeURIComponent(location.href.replace(/^.+[?&]title=([^&]+)&.+$/, '$1')) | |||
$('#edit-ajaxPreviewRm').show() | |||
$('#edit-ajaxPreviewSave').show() | |||
$('body').css('overflow', 'hidden') | |||
deferred = api.post({ | |||
"action": "parse", | |||
"format": "json", | |||
"text": wikiText, | |||
"title": titleName, | |||
"contentmodel": "wikitext" | |||
}).done(function(data){ | |||
$('#edit-ajaxPreview').html($(data.parse.text['*']).addClass('mw-body-content')) | |||
}) | |||
requestTimeoutKey = setTimeout(function(){ | |||
if(deferred.state() != 'resolved'){ | |||
viewHide() | |||
foo(e) | |||
} | |||
}, 8000) | |||
return false | |||
} | |||
} | |||
// ctrl + alt快速添加变量命名空间 | |||
if (mw.config.values.wgAction == "edit" || mw.config.values.wgAction == "submit"){ | |||
var editor = $('#wpTextbox1') | |||
var title = mw.config.values.wgTitle.replace(/./, function(s){ | |||
return s.toLowerCase() | |||
}) | |||
editor.keydown(function(e){ | |||
if(e.ctrlKey && e.altKey){ | |||
editor.val(editor.val().replace(/(\{\{#var(define|):)([^\|\}]+)/g, function(s, s1, s2, s3){ | |||
if(!new RegExp('^' + title + '\\.').test(s3)){ | |||
if(['num', 'key', 'value', 'time', 'times'].includes(s3)){ return s } | |||
return s1 + title + '.' + s3 | |||
} | |||
return s | |||
})) | |||
mw.notify('已添加变量命名空间。') | |||
} | |||
}) | |||
} | |||
}) |