User:Irukaza/common.js:修订间差异

跳到导航 跳到搜索
删除19,264字节 、​ 2020年3月11日 (星期三)
无编辑摘要
imported>=海豚=
(创建页面,内容为“→‎global mediaWiki:​ (function(mw) { if (!String.prototype.includes) String.prototype.includes = function includes(search, start) { 'use strict';…”
 
imported>=海豚=
无编辑摘要
(未显示同一用户的1个中间版本)
第327行: 第327行:
*/
*/
mw.loader.load('MediaWiki:Gadget-DotsSyntaxHighlighter.js');
mw.loader.load('MediaWiki:Gadget-DotsSyntaxHighlighter.js');
/* 给wikiplus添加快速填写摘要 */
mw.loader.load('https://common.wjghj.cn/js/InPageEdit-v2');
$(function () {
   var observar = new MutationObserver(function (record) {
     if ($('.Wikiplus-InterBox').find('#Wikiplus-Quickedit-Summary-Input')[0] &&
       (!$('.Wikiplus-InterBox').find('#summary_list')[0])) {
       var list = ['新条目', '修饰语句', '修正笔误', '内容扩充', '排版', '内部链接', '分类', '消歧义', '萌百化']
       var summary = document.createElement('span')
       summary.id = 'summary_list';
       summary.style.marginLeft = '0.5em';
       for (var i = 0; i < list.length; i++) {
         if (i != 0) summary.innerHTML += ' | ';
         summary.innerHTML += '<a href="#.">' + list[i] + '</a>';
       }
       $('.Wikiplus-InterBox').find('#Wikiplus-Quickedit-Summary-Input')[0].after(summary);
       $('#summary_list').children().each(function () {
         $(this)[0].onclick = function () {
           var str = $('#Wikiplus-Quickedit-Summary-Input')[0].value;
           if (/\/\/ Edit via Wikiplus\s*$/.test(str)) {
             $('#Wikiplus-Quickedit-Summary-Input')[0].value = str.replace(/^(.*?)( ?\/\/ Edit via Wikiplus\s*)$/, '$1 ' + $(this)[0].innerText + ' // Edit via Wikiplus');
           } else {
             $('#Wikiplus-Quickedit-Summary-Input')[0].value += ' ' + $(this)[0].innerText;
           }
         };
       });
     }
   });
   observar.observe(document.body, { childList: true });
});
/*
注意:该插件必须依附于Wikiplus才能生效。
载入后将在Wikiplus快速编辑的界面中添加一个“+”加号按钮,点击后可以新建快速替换的方案,完成后将生成一个按钮。
在名称栏中,输入#号后面的文字将作为摘要使用,在执行匹配成功后将自动把摘要的文字置于编辑摘要栏中。
在替换栏中可以使用 _@br_ 来指代换行,无论是否为正则模式。
左键点击按钮执行预先设置好的替换,右键点击按钮删除该替换方案。
注意:替换方案保存在当前浏览器中,而不是帐号内,若更换浏览器、更换电脑或清除缓存等,替换方案将不会同步。
*/
$(function(){
if(typeof MutationObserver == 'undefined'){
mw.notify('你使用的浏览器版本过低或不符合规范,无法使用Wikiplus-replace插件!', { type : 'warn' })
return false
}
var topBtn = document.getElementById('Wikiplus-Edit-TopBtn')
topBtn && topBtn.addEventListener('click', function(e){
if($('.Wikiplus-InterBox').length == 1){
$('.Wikiplus-InterBox').remove()
}
}, true)
   $.each(document.getElementsByClassName('mw-editsection'), function(index, value){
     value.addEventListener('click', function(e){
       if(e.target.classList.contains('Wikiplus-Edit-SectionBtn') && $('.Wikiplus-InterBox').length == 1){
     $('.Wikiplus-InterBox').remove()
     }
     }, true)
   })
   var observar = new MutationObserver(function(record){
     var wikiplus = null
     var wikiplusTextarea = $('#Wikiplus-Quickedit')
     $.each(record[0].addedNodes, function(item, element){
       if(element.classList && element.classList.contains('Wikiplus-InterBox')){
         wikiplus = element
       }
     })
     if(wikiplus){
     var br = '\n' //用于快捷指代替换中的换行
       $(wikiplus).find('.Wikiplus-InterBox-Content')
       .find('div:first').find('#Wikiplus-Quickedit-Jump')
       .after('<span id="Wikiplus-Quickedit-addReplace" class="Wikiplus-Btn">✚</span>')
 
       $(wikiplus).contextmenu(function(){ return false })
 
       if(localStorage.getItem('Quickedit-replace')){
         var replaceData = JSON.parse(localStorage.getItem('Quickedit-replace'))
         $.each(replaceData, function(item, data){
           var newButton = $('<span class="Wikiplus-Btn" data-id="' + data[3] + '">' + data[0] + '</span>')
           newButton.mousedown(function(e){
             if(e.which == 1){
               var code = wikiplusTextarea.val()
               var newCode = code.replace(new RegExp(data[1], 'gm'), data[2] ? data[2] : '')
               .replace(/_@br_/g ,br)
               if(code == newCode){
                 mw.notify('匹配结果为空!', { type : 'warn' })
               }else{
                 wikiplusTextarea.val(newCode)
                 data[4] && $('#Wikiplus-Quickedit-Summary-Input').val(data[4])
                 mw.notify('已完成替换!')
               }
             }
            
             if(e.which == 3){
               var replaceData = JSON.parse(localStorage.getItem('Quickedit-replace'))
               .filter(function(repData){
                 return repData[3] != $(e.target).data('id')
               })
               localStorage.setItem('Quickedit-replace', JSON.stringify(replaceData))
               $(this).remove()
             }
           })
           $(wikiplus).find('.Wikiplus-InterBox-Content')
           .find('div:first').find('.Wikiplus-Btn:last').after(newButton)
         })
       }
 
       $('#Wikiplus-Quickedit-addReplace').click(function(){
         var addReplaceHTML = '\
         <div id="Wikiplus-Quickedit-replace" style="width:400px; background:#EDF9F7; border:1px #666 solid; font-size:80%; box-shadow: 2px 2px 5px #aaa; overflow:hidden; padding:5px; position:fixed; top:50%; left:50%; transform:translate(-50%,-50%); -webkit-user-select:none; -moz-user-select:none; -o-user-select:none; user-select:none; z-index:300;">\
         <div style="padding:10px; padding-bottom:0;">添加快速替换<div class="Quickedit-replace-hide" style="float:right; font-size:26px; font-weight:bold; margin-top:-8px; -webkit-user-select:none; -moz-user-select:none; -o-user-select:none; user-select:none; cursor:pointer">×</div></div>\
         <hr style="height:1px; margin-bottom:-5px; background:#66CCFF; border:none;">\
         <div style="height:110px; border-radius:3px; padding:10px;">\
           <p>名称:<input id="Quickedit-replace-Name" style="width:300px; height:25px; text-indent:3px;"></p>\
           <p>查找:<input id="Quickedit-replace-Lookup" style="width:300px; height:25px; text-indent:3px;"></p>\
           <p>替换:<input id="Quickedit-replace-Replace" style="width:300px; height:25px; text-indent:3px;"></p>\
           <p>正则模式:<input type="checkbox" id="Quickedit-replace-pattern" value="reg" style="width:16px; height:16px; position:relative; top:3px;">\
           <input class="Quickedit-replace-hide" type="button" value="关闭" style="float:right; width:50px; height:25px; background:white; box-shadow: 0 1px 2px #aaa; -webkit-user-select:none; -moz-user-select:none; -o-user-select:none; user-select:none; margin:0 5px;">\
           <input id="Quickedit-replace-add" type="button" value="添加" style="float:right; width:50px; height:25px; background:white; box-shadow: 0 1px 2px #aaa; -webkit-user-select:none; -moz-user-select:none; -o-user-select:none; user-select:none; margin:0 5px;"></p>\
         </div>\
         </div>'
         $(document.body).append(addReplaceHTML)
 
         var addReplace = $('#Wikiplus-Quickedit-replace'),
         name = $('#Quickedit-replace-Name'),
         lookup = $('#Quickedit-replace-Lookup'),
         replace = $('#Quickedit-replace-Replace'),
         patternSwitch = $('#Quickedit-replace-pattern'),
         hideBtn = $('.Quickedit-replace-hide'),
         addBtn = $('#Quickedit-replace-add')
 
         hideBtn.click(function(){
           addReplace.remove()
         })
 
         addBtn.click(function(){
           var text = name.val()
           if(/#/.test(text)){
           var note = text.replace(/(.+)#(.+)/, '$2')
           text = text.replace(/(.+)#(.+)/, '$1')
           }
           var pattern = lookup.val()
           var replaceText = replace.val()
           try{
             if(! patternSwitch.prop('checked')){
               pattern = pattern.replace(/([\\\(\)\[\]\{\}\+\.\*\^\$\!\?\|])/g,'\\$1')
             }
             new RegExp(pattern, 'gm')
           }catch(e){
             mw.notify('正则表达式有误,请核对后重新输入!', { type : 'error' })
             return false
           }
           var id = Math.random()
           if(localStorage.getItem('Quickedit-replace')){
             var replaceData = JSON.parse(localStorage.getItem('Quickedit-replace'))
             replaceData.push([text, pattern, replaceText, id, note || null])
             replaceData = JSON.stringify(replaceData)
             localStorage.setItem('Quickedit-replace', replaceData)
           }else{
             var replaceData = JSON.stringify([[text, pattern, replaceText, id, note || null]])
             localStorage.setItem('Quickedit-replace', replaceData)
           }
           var newButton = $('<span class="Wikiplus-Btn" data-id="' + id + '">' + text + '</span>')
           newButton.mousedown(function(e){
             if(e.which == 1){
               var code = wikiplusTextarea.val()
               var newCode = code.replace(new RegExp(pattern, 'gm'), replaceText ? replaceText : '')
               .replace(/_@br_/g ,br)
               if(code == newCode){
                 mw.notify('匹配结果为空!', { type : 'warn' })
               }else{
                 wikiplusTextarea.val(newCode)
                 note && $('#Wikiplus-Quickedit-Summary-Input').val(note)
                 mw.notify('已完成替换!')
               }
             }
 
             if(e.which == 3){
               var replaceData = JSON.parse(localStorage.getItem('Quickedit-replace'))
               .filter(function(repData){
                 return repData[3] != $(e.target).data('id')
               })
               localStorage.setItem('Quickedit-replace', JSON.stringify(replaceData))
               $(this).remove()
             }
           })
           $(wikiplus).find('.Wikiplus-InterBox-Content')
           .find('div:first').find('.Wikiplus-Btn:last').after(newButton)
           mw.notify('已添加新的替换方案!')
         })
 
       })
       $('#Wikiplus-Quickedit-Back, .Wikiplus-InterBox-Close').click(function(){
         $('#Wikiplus-Quickedit-replace').remove()
       })
     }
   })
   var options = {
     childList : true
   }
   observar.observe(document.body, options)
})
/*
载入该插件后,将对wikitext语法所使用的符号和html标签进行成对的补全。
该插件并不会计算整个源代码中的html标签配对情况,标签的补全仅发生在输入大于号(>)时,补全距离最近的html标签。
在输入无其他信息的标签后(如:<div></div>),若按下退格(backspace)键,可以直接删除这一组标签。
注意:因为每次输入时都会对整个源代码进行查找匹配,为了防止卡顿,默认限制为500个字符,但这会导致当标签过长(例如在节点上添加了大量css代码)时,无法自动补全。
过长的源代码会使输入时性能大幅下降,造成卡顿。当总字符数大于20000时,将自动关闭补全,请合理使用段落编辑的功能。
*/
 
$(function(){
   if(/action=(edit|submit)/.test(location.href)){
   var wpText = $('#wpTextbox1')
  
   if(wpText.val().length > 20000){
   mw.notify('因源代码过长,补全功能被关闭!', { type : 'warn' })
   return
   }
  
var retrospect = 500
var pattern = [
['-{', '}-'],
['[', ']'],
['{', '}'],
["'", "'"],
['"', '"'],
['(', ')'],
['<!--', '-->']
]
   //光标后插入信息
   function insertText(obj, str){
var content = obj.value,
position = obj.selectionEnd,
left = content.substring(0, position),
right = content.substring(position)
obj.value = left + str + right
   }
 
var original = null
wpText.one('click', function(){
original = {
code : wpText.val(),
start : wpText[0].selectionStart,
end : wpText[0].selectionEnd
}
})
   // 缓存输入内容 & 判断
var end = 0,
thisLength = 0,
lastLength = 0,
code = '',
thisCode = '',
deletedKeyword = '',
// 需要暂停补全的状态
typewritingMode = false,
cutPaste = false,
selectInput = false,
selectDelete = false,
inputFinish = false
var codeSave = [{
code : wpText.val(),
start : wpText[0].selectionStart,
end : wpText[0].selectionEnd
}],
inputMark = false
   function selectionReset(){
     end = wpText[0].selectionEnd
     lastLength = wpText.val().length
}
   wpText.click(function(){
     selectionReset()
   }).keyup(function(e){
     if(e.keyCode > 36 && e.keyCode < 41){
       selectionReset()
       code = ''
     }
   }).on('keydown keyup', function(e){
if(e.keyCode == 8){
var subStart = (this.selectionEnd - 1) < 0 ? 0 : (this.selectionEnd - 1)
var left = thisCode.substring(subStart, this.selectionEnd)
deletedKeyword = left
selectionReset()
}
}).on('compositionstart', function(){
typewritingMode = true
}).on('compositionend', function(){
typewritingMode = false
       inputFinish = true
       $(this).trigger('input')
       selectionReset()
}).on('cut paste', function(){
cutPaste = true
codeSave.push({
code : wpText.val(),
start : wpText[0].selectionStart,
end : wpText[0].selectionEnd
})
inputMark = false
setTimeout(selectionReset, 0)
}).on('keydown', function(e){
selectInput = !! document.getSelection().toString()
if(selectInput){ end = wpText[0].selectionStart}
if(selectInput && (e.keyCode == 8 || e.keyCode == 46)){ selectDelete = true }
}).on('input', function(){
;(function(_this){
         if(typewritingMode){ return }
         if(inputFinish){ inputFinish = false; return }
if(cutPaste){ cutPaste = false; return }
if(selectDelete){ selectDelete = false; return }
     thisCode = $(_this).val()
     var right = thisCode.substring(end)
     thisLength = $(_this).val().length
     if(thisLength - lastLength == 1 || selectInput){
code += right.substring(0, 1)
     }else{
       code = ''
      
       if(lastLength - thisLength == 1){
       var subStart = (_this.selectionEnd - 20) < 0 ? 0 : (_this.selectionEnd - 20),
       subEnd = (_this.selectionEnd + 20) > thisLength ? thisLength : (_this.selectionEnd + 20)
      
       var subLeft = thisCode.substring(0, subStart),
       subRight = thisCode.substring(subEnd)
      
       var left = thisCode.substring(subStart, _this.selectionEnd),
       right = thisCode.substring(_this.selectionEnd, subEnd),
       leftRE = /([\s\S]*)<(.+)$/,
rightRE = /^\<\/(.+?)>([\s\S]*)/
       var tagLeft = left.replace(leftRE, '$2')
var tagRight = right.replace(rightRE, '$1')
       if(tagLeft == tagRight){
       var leftCode = left.replace(leftRE, '$1')
       var rightCode = right.replace(rightRE, '$2')
       $(_this).val(subLeft + leftCode + rightCode + subRight)
       var cursorPos = new String(subLeft + leftCode).length
       _this.selectionStart = _this.selectionEnd = cursorPos
       }
$.each(pattern, function(){
var $this = wpText[0]
if(lastLength - thisLength == 1){
var subStart = $this.selectionEnd < 0 ? 0 : $this.selectionEnd
var subEnd = ($this.selectionEnd + 1) > thisLength ? thisLength : ($this.selectionEnd + 1)
$this.selectionEnd == subEnd && subEnd++
var subLeft = thisCode.substring(0, $this.selectionEnd),
subRight = thisCode.substring(subEnd)
var left = thisCode.substring(subStart, $this.selectionEnd),
right = thisCode.substring($this.selectionEnd, subEnd)
if(deletedKeyword == this[0] && right == this[1]){
$($this).val(subLeft + subRight)
$this.selectionStart = $this.selectionEnd = subLeft.length
selectionReset()
}
}
})
       }
      
     }
     function pair(keyword, char, num){
       if(code.indexOf(keyword) != -1){
var num = num || char.length
var position = wpText[0].selectionEnd
         insertText(wpText[0], char)
         wpText[0].selectionStart = wpText[0].selectionEnd = position
         selectionReset()
         code = ''
       }
     }
 
     function addPair(arr){
     $.each(arr, function(){
pair(this[0], this[1], this[2])
})
     }
       selectionReset()
     addPair(pattern)
      
// 配对标签
       if(code.indexOf('>') != -1){
       var start = (end - retrospect) < 0 ? 0 : (end - retrospect)
         var left = wpText.val().substring(start, end)
         var ptn = /[\s\S]*<((?!br|hr|img|references|templatestyles|\!--|\/|\>)[^\s<>]+)[^>]*?>$/
         if(ptn.test(left)){
var tagEnd = '</' + left.replace(ptn, '$1') + '>'
var position = wpText[0].selectionEnd
           insertText(wpText[0], tagEnd)
         wpText[0].selectionStart = wpText[0].selectionEnd = position
           selectionReset()
           code = ''            
         }         
       }
})(this)
      
codeSave.push({
code : wpText.val(),
start : wpText[0].selectionStart,
end : wpText[0].selectionEnd
})
inputMark = true      
input = false
})
wpText.on('keydown', function(e){
if(e.ctrlKey && e.keyCode == 90){
e.preventDefault()
if(inputMark){
codeSave.pop()
inputMark = false
}
var history = codeSave.pop() || original
if(history){
wpText.val(history.code).focus()
wpText[0].selectionStart = history.start
wpText[0].selectionEnd = history.end
}
}
})
   }
})
/*
/*
这是一个保存编辑内容的小插件,载入后将在“显示更改”按钮后生成一个“还原备份”按钮。
这是一个保存编辑内容的小插件,载入后将在“显示更改”按钮后生成一个“还原备份”按钮。
匿名用户

导航菜单