2023年政策修订增补工作正在进行中,欢迎参与!
  • Moegirl.ICU:萌娘百科流亡社群 581077156(QQ),欢迎对萌娘百科运营感到失望的编辑者加入
  • Moegirl.ICU:账号认领正在试运行,有意者请参照账号认领流程

User:東東君/js/replace.js

萌娘百科,万物皆可萌的百科全书!转载请标注来源页面的网页链接,并声明引自萌娘百科。内容不可商用。
跳转到导航 跳转到搜索

注意:在保存之后,您可能需要清除浏览器缓存才能看到所作出的变更的影响。

  • Firefox/Safari:按住“Shift”的同时单击“刷新”,或按“Ctrl-F5”或“Ctrl-R”(Mac为“⌘-R”)
  • Google Chrome:按“Ctrl-Shift-R”(Mac为“⌘-Shift-R”)
  • Internet Explorer:按住“Ctrl”的同时单击“刷新”,或按“Ctrl-F5”
  • Opera:在“工具→首选项”中清除缓存
如果您已登录但该页面出现未登录状态,请尝试在地址栏的地址最后添加代码?_=1来访问最新页面。
添加代码后的本页地址如下:-{R|https://moegirl.icu/User:%E6%9D%B1%E6%9D%B1%E5%90%9B/js/replace.js?_=1}-
/*
	这个js文件将载入一个查找与替换的工具。
	萌百本身有查找与替换工具的,不知为何莫名其妙地消失了,于是就自己造了一个。
	注意:开启段落模式后,下次匹配的范围将被限制在最近一次选中的代码范围中。
	      在替换栏中可以使用“_@br_”来指代换行,无论是否为正则模式,如果你要替换的内容里就有这个字符串...你到底写是什么内容啊,会写这个
	      若呼出界面的按钮没有加载,也可以使用ctrl + shift + 空格呼出或关闭 ,如果还不行请保存编辑内容并刷新
*/

$(function(){
    if(/action=(edit|submit)/.test(location.href)){
    	setTimeout(function(){
	        var widgetHTML = '\
	            <div id="widget-RegExp" style="width:400px; background:#FAFCFE; border:1px #666 solid; border-radius:5px; font-size:80%; overflow:hidden; padding:5px; position:fixed; top:50%; left:50%; -webkit-user-select:none; -moz-user-select:none; -o-user-select:none; user-select:none; display:none; z-index:10000;">\
	            <div id="widgetTop" style="background:linear-gradient(white,#E8F2F8); padding:10px; cursor:move;">查找与替换<div id="widget-RegExpHide" 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>\
	            <div style="height:150px; border-radius:3px; padding:10px;">\
	                <p>查找:<input id="widgetLookup" style="width:300px; height:25px; text-indent:3px; border-radius:3px;"></p>\
	                <p>替换:<input id="widgetReplace" style="width:300px; height:25px; text-indent:3px; border-radius:3px;"></p>\
	                <p id="widget-RegExpMessage">&emsp;</p>\
	                <p>正则模式:<input type="checkbox" id="widget-RegExpFlag" value="reg" style="width:16px; height:16px; position:relative; top:3px; margin-right:10px;">范围模式:<input type="checkbox" id="widget-RegExpSec" value="set" style="width:16px; height:16px; position:relative; top:3px;"></p>\
	                <p><input id="widget-RegExpRevoke" type="button" value="撤销" style="float:right; width:50px; height:25px; background:linear-gradient(white,#E8F2F8); border-radius:3px; border-radius:3px; -webkit-user-select:none; -moz-user-select:none; -o-user-select:none; user-select:none; margin:0 5px;"></p>\
	            <p><input id="widget-RegExpAction" type="button" value="替换" style="float:right; width:50px; height:25px; background:linear-gradient(white,#E8F2F8); border-radius:3px; border-radius:3px; -webkit-user-select:none; -moz-user-select:none; -o-user-select:none; user-select:none; margin:0 5px;"></p>\
	            </div>\
	            </div>'
	        var shade = '<div id="widget-RegExp-shade" style="width:100%; height:100%; position:fixed; top:0; left:0; z-index:9999;"></div>'
	        $('body').append($(widgetHTML))
	        $('#wikiEditor-ui-toolbar .tabs').append($('<span id="widget-RegExpShow" class="tab"><a href="#" role="button" aria-pressed="false">查找与替换</a></span>'))
	
			function windowReset(){
				$('#widget-RegExp').css({
		            top : $(window).outerHeight() / 2 - $('#widget-RegExp').outerHeight() / 2 + 'px',
		            left : $(window).outerWidth() / 2 - $('#widget-RegExp').outerWidth() / 2 + 'px'
		        })
			}
			windowReset()
	
	        var move = false
	        var distanceX = 0
	        var distanceY = 0
	        $('#widgetTop').mousedown(function(e){
	            move = true
	            this.setCapture && this.setCapture()
	            var location = $('#widget-RegExp').offset()
	            distanceX = e.clientX - location.left;
	            distanceY = e.clientY - location.top;
	            $('body').append(shade)
	        })
	        $(window).mousemove(function(e){
	            if(move){
		            $('#widget-RegExp').offset({
		                left : e.clientX - distanceX,
		                top : e.clientY - distanceY
		            })
	            }
	        }).mouseup(function(){
	            move = false
	            this.releaseCapture && this.releaseCapture()
	            $('#widget-RegExp-shade').remove()
	        })
	
			function windowSwitch(){
	            if($('#widget-RegExp:hidden').length){
		            $('#widget-RegExp').show()
		            windowReset()
	            }else{
	            	$('#widget-RegExp').hide()
	            }
			}
			
	        $('#widget-RegExpShow').click(function(){
				windowSwitch()
	            return false
	        })
	        $('#widget-RegExpHide').click(function(){
	        	windowSwitch()
	        })
	        $(window).keydown(function(e){
	            if(e.ctrlKey && e.keyCode == 32){
	            	e.preventDefault()
	        		windowSwitch()
	            }
	        })
	        
	        $('#widget-RegExpAction').click(function(){
	            var pattren = $('#widgetLookup').val()
	            var replace = $('#widgetReplace').val()
	            var regFlag = $('#widget-RegExpFlag:checked').length
	            var secFlag = $('#widget-RegExpSec:checked').length
	            var wikiCode = $('#wpTextbox1').val()
	
	            var editBox = $('#wpTextbox1')[0]
	            var start = editBox.selectionStart
	            var end = editBox.selectionEnd
	            var paraWikiCode = editBox.value.substring(start, end)               
	            if(secFlag && paraWikiCode && (paraWikiCode != wikiCode)){
	                var paraReplace = true
	            }
	
	            if(pattren){      
	                try{
	                    if(! regFlag){
	                        var pattren = pattren.replace(/([\\\(\)\[\]\{\}\+\.\*\^\$\!\?\|])/g,'\\$1')
	                    }
	                    var pattren = new RegExp(pattren,'gm')
	                }catch(e){
	                    $('#widget-RegExpMessage').text('表达式有误!')
	                    return false
	                }
	                if(paraReplace){			//选中某处后再执行替换,只操作选中的文字
	                    var workedWikiCode = paraWikiCode.replace(pattren,replace)
	                    var left = wikiCode.substring(0, start)
	                    var right = wikiCode.substring(end, wikiCode.length)
	                    workedWikiCode = left + workedWikiCode + right
	                    $('#wpTextbox1').val(workedWikiCode)
	                }else{
	                    $('#wpTextbox1').val(wikiCode.replace(pattren,replace))
	                }
	                var br = '\n'
	                $('#wpTextbox1').val($('#wpTextbox1').val().replace(/_@br_/g,br))     //在替换栏中使用“_@br_”指代换行
	
	                var prompt = paraReplace ? '<strong>范围匹配:</strong>' : ''
	                if($('#wpTextbox1').val() == wikiCode){
	                    $('#widget-RegExpMessage').html(prompt + '匹配结果为空!')
	                }else{
	                    $('#widget-RegExpRevoke').one('click',function(){
	                        $('#wpTextbox1').val(wikiCode)
	                        if(paraReplace){
	                            editBox.selectionStart = start
	                            editBox.selectionEnd = end
	                        }
	                    })
	                    $('#widget-RegExpMessage').html(prompt + '已完成替换!')
	                }  
	            }else{
	                $('#widget-RegExpMessage').text('表达式不能为空!')
	            }
	        })
    	}, 1000)
    }
})