Thành viên:Trần Nguyễn Minh Huy/Regex menu framework.js

Bách khoa toàn thư mở Wikipedia

Chú ý: Sau khi lưu thay đổi trang, bạn phải xóa bộ nhớ đệm của trình duyệt để nhìn thấy các thay đổi. Google Chrome, Firefox, Internet ExplorerSafari: Giữ phím ⇧ Shift và nhấn nút Reload/Tải lại trên thanh công cụ của trình duyệt. Để biết chi tiết và hướng dẫn cho các trình duyệt khác, xem Trợ giúp:Xóa bộ nhớ đệm.

/*************
*** Regex menu framework 1.2
*** by [[m:user:Pathoschild]]
*** documentation: http://meta.wikimedia.org/wiki/User:Pathoschild/Script:Regex_menu_framework
*************/
var rmf_globals = {};
 
if(wgAction=='edit' || wgAction=='submit') {
	/****************
	*** config tools
	****************/
	// function
	function regexTool(name,functionname) {
		var newline = document.createElement('li');
 
		var newlink = document.createElement('a');
		newlink.setAttribute('href','javascript:'+functionname);
		newlink.setAttribute('title',name);
		newlink.setAttribute('class','sidebar-link');
		newlink.appendChild(document.createTextNode(name));
 
		newline.appendChild(newlink);
		toollist.appendChild(newline);
	}
 
	/****************
	*** framework tools
	****************/
	/* search only */
	function regsearch(search) {
		return editbox.value.match(search);
	}
 
	/* search & replace */
	function regex(search,replace,repeat) {
		/* define how many times to perform */
		if(!repeat || repeat<0) {
			var repeat = 1;
		}
			/* perform */
		for(var i=0; i<repeat; i++) {
			editbox.value = editbox.value.replace(search,replace);
		}
	}
 
	/* change reason */
	function setreason(reason,mode) {
		// append once
		if( mode=='appendonce' && !rmf_globals['summary_'+reason] ) {
			rmf_globals['summary_'+reason] = 1;
			if(editreason.value.match(/[^\s]/))
				editreason.value += ', ' + reason;
			else
				editreason.value = reason;
		}
		// append mode
		if(mode=='append' && editreason.value.match(/[^\s]/)) {
			editreason.value += ', ' + reason;
		}
		// default mode
		else {
			editreason.value = reason;
		}
	}
 
	/* set edit options */
	function setoptions(minor,watch) {	
		// minor edit?
		if(minor=='true') {
			document.getElementById('wpMinoredit').checked = true;
		}
		else if(minor=='false') {
			document.getElementById('wpMinoredit').checked = false;
		}
 
		// watch page?
		if(watch=='true') {
			document.getElementById('wpWatchthis').checked = true;
		}
		else if(watch=='false') {
			document.getElementById('wpWatchthis').checked = false;
		}
	}
 
	/* automatically perform an action */
	function doaction(action) {
		if(action=='preview') {
			document.getElementById('wpPreview').click();
		}
		else if(action=='diff') {
			document.getElementById('wpDiff').click();
		}
		else if(action=='save') {
			document.getElementById('wpSave').click();
		}
	}
 
	/****************
	*** Custom regex form tool
	****************/
	/* create form */
	function custom() {
		/* if already open */
		if(document.getElementById('regexform')) {
			/* display reset warning, unless it's already displayed */
			if(!document.getElementById('resetwarning')) {
				/* ask the user whether he wants to reset the form or cancel */
				// create elements
				resetwarning = document.createElement('div');
				resetwarning.setAttribute('id','resetwarning');
 
				var resetlink = document.createElement('a');
				resetlink.setAttribute('href','javascript:customremove(); custom();');
				resetlink.setAttribute('title','Làm sạch');
				resetlink.appendChild(document.createTextNode('làm sạch'));
 
				var cancellink = document.createElement('a');
				cancellink.setAttribute('href','javascript:function customreset() { regexform.removeChild(resetwarning); }; customreset();');
				cancellink.setAttribute('title','Đóng thông điệp này');
				cancellink.appendChild(document.createTextNode('đóng thông điệp này'));
 
				// put message together
				resetwarning.appendChild(document.createTextNode('Bạn vừa nhấn nút mở hộp thoại "Tìm và thay thế", nhưng nó đã được mở sẵn bên dưới. Xin hãy '));
				resetwarning.appendChild(resetlink);
				resetwarning.appendChild(document.createTextNode(' hoặc '));
				resetwarning.appendChild(cancellink);
 
				// output
				regexform.insertBefore(resetwarning,regexform.firstChild);
			}
		}
		else {	
			/* container */
			regexform = document.createElement('div');
			regexform.setAttribute('id','regexform');
			editbox.parentNode.insertBefore(regexform,editbox.parentNode.firstChild);
 
			/* header */
			var formheader = document.createElement('h3');
			formheader.appendChild(document.createTextNode('Tìm và thay thế'));
			regexform.appendChild(formheader);
 
			/* instructions */
			var newlink = document.createElement('a');
			newlink.setAttribute('href','javascript:function opennew(url) { window.open(url); }; opennew("http://www.regular-expressions.info/javascript.html");');
			newlink.setAttribute('title','JavaScript regex tutorial');
			newlink.setAttribute('class','external text');
			newlink.appendChild(document.createTextNode('tutorial'));
 
			var newtext = document.createElement('p');
			newtext.appendChild(document.createTextNode('Mã script này giúp bạn tìm kiếm những từ trùng lắp trong một bài viết và thay thế tất cả như ý muốn. Cơ cấu hoạt động của nó giống như hộp thoại "Find and Replace" của MicrosoftWord (xem '));
			newtext.appendChild(newlink);
			newtext.appendChild(document.createTextNode('), có thể tìm và thay thế nhiều từ một lúc trong hộp thoại (các nhóm từ "$1" đến "$9"). Mã script này được Thành viên Pathoschild (một Tiếp viên tại Meta sáng chế) và được Thành viên Trần Nguyễn Minh Huy việt hoá cũng như đưa vào sử dụng tại Wikipedia tiếng Việt. '));
			regexform.appendChild(newtext);
 
			/* mini tutorial */
			var newtext = document.createElement('p');
			newtext.appendChild(document.createTextNode('Ví dụ:'));
			regexform.appendChild(newtext);
 
			var newtext = document.createElement('ul');
			var newitem = document.createElement('li');
			newitem.appendChild(document.createTextNode('thay thế "a" sang "b": nhập "a" vào hộp "Tìm #1", nhập "b" vào hộp "Thay #1", sau đó nhấn "Thay đổi"'));
			regexform.appendChild(newtext);
			newtext.appendChild(newitem);
 
			var newitem = document.createElement('li');
			newitem.appendChild(document.createTextNode('thay thế "Wikipedia" sang "Wikisource": nhập "Wikipedia" vào hộp "Tìm #1", nhập "Wikisource" vào hộp "Thay #1", sau đó nhấn "Thay đổi"'));
			newtext.appendChild(newitem);
 
			var newitem = document.createElement('li');
			newitem.appendChild(document.createTextNode('thay thế "Wikipedia" sang "Wikisource" - "Wikiquote" sang "Wikimedia": nhập "Wikipedia" vào hộp "Tìm #1", nhập "Wikisource" vào hộp "Thay #1". Nhấn "Thêm khung tìm kiếm" sẽ hiện ra hai hộp thoại khác. Tiếp tục nhập "Wikiquote" vào hộp "Tìm #2", nhập "Wikimedia" vào hộp "Thay #2", sau đó nhấn "Thay đổi"'));
			newtext.appendChild(newitem);
 
			/* form tag */
			var formform = document.createElement('form');
			formform.setAttribute('id','regexformform');
			regexform.appendChild(formform);
 
			// place marker for input boxes
			inputlist = document.createElement('ol');
			formform.appendChild(inputlist);
 
			/* options */
			// list format
			var formoptions = document.createElement('ul');
			formform.appendChild(formoptions);
 
			// go! link
			var container = document.createElement('li');
			var newlink = document.createElement('a');
			newlink.setAttribute('href','javascript:customgo();');
			newlink.setAttribute('title','Thay đổi');
			newlink.appendChild(document.createTextNode('Thay đổi'));
			newlink.setAttribute('style','font-weight:bold;');
			container.appendChild(newlink);
			formoptions.appendChild(container);
 
			// add new boxes link
			var container = document.createElement('li');
			var newlink = document.createElement('a');
			newlink.setAttribute('href','javascript:customaddnew();');
			newlink.setAttribute('title','Thêm khung tìm kiếm');
			newlink.appendChild(document.createTextNode('Thêm khung tìm kiếm'));
			container.appendChild(newlink);
			formoptions.appendChild(container);
 
			// remove form
			var container = document.createElement('li');
			var newlink = document.createElement('a');
			newlink.setAttribute('href','javascript:customremove();');
			newlink.setAttribute('title','Đóng hộp thoại');
			newlink.appendChild(document.createTextNode('Đóng hộp thoại'));
			newlink.setAttribute('style','color:red;');
			container.appendChild(newlink);
			formoptions.appendChild(container);
 
			// add first pair of input boxes
			customaddnew();
		}
	}
 
	/* add a new pair of input boxes */
	function customaddnew() {
		/* count for later processing */
		if("patterncount" in window) {
			patterncount++;
		}
		else {
			patterncount = 0;
		}
 
		/* add new list item */
		var container = document.createElement('li');
		inputlist.appendChild(container);
 
		/* create search */
		var newinput = document.createElement('textarea');
		newinput.setAttribute('class','formsearch');
		newinput.setAttribute('tabindex',patterncount+100);
		newlabel = document.createElement('label');
		newlabel.setAttribute('for','formsearch');
		newlabel.appendChild(document.createTextNode('Tìm #'+(patterncount+1)));
 
		container.appendChild(newlabel);
		container.appendChild(newinput);
 
		/* force newline */
		container.appendChild(document.createElement('br'));
 
		/* create replace */
		var newinput = document.createElement('textarea');
		newinput.setAttribute('class','formreplace');
		newinput.setAttribute('tabindex',patterncount+101);
		newlabel = document.createElement('label');
		newlabel.setAttribute('for','formreplace');
		newlabel.appendChild(document.createTextNode('Thay #'+(patterncount+1)));
 
		container.appendChild(newlabel);
		container.appendChild(newinput);
	}
 
	/* run patterns */
	function customgo() {
		/* get search and replace arrays */
		var arrpatterns = regexform.getElementsByTagName('textarea');
		arrsearch = new Array();
		arrreplace = new Array();
		var searchcount = -1;
		var replacecount = -1;
 
		for(i=0; i<arrpatterns.length; i++) {
			/* search boxes */
			if(arrpatterns[i].getAttribute('class') == 'formsearch') {
				searchcount++;
				arrsearch[searchcount] = arrpatterns[i].value;
			}
			/* replace boxes */
			else if (arrpatterns[i].getAttribute('class') == 'formreplace') {
				replacecount++;
				arrreplace[replacecount] = arrpatterns[i].value;
			}
		}
 
		/* perform patterns */
		for(var i=0; i<arrsearch.length; i++) {
			/* convert input to regex */
			// without delimiters
			if(!arrsearch[i].match(/^\s*\/[\s\S]*\/[a-z]*\s*$/i)) {
				arrsearch[i] = new RegExp(arrsearch[i]);
			}
			// with delimiters
			else {
				// break into parts
				var regpattern = arrsearch[i].replace(/^\s*\/([\s\S]*)\/[a-z]*\s*$/i,'$1');
				var regmodifiers = arrsearch[i].replace(/^\s*\/[\s\S]*\/([a-z]*)\s*$/,'$1');
 
				// filter invalid flags
				regmodifiers = regmodifiers.replace(/[^gim]/ig,'');
 
				// convert into array
				arrsearch[i] = new RegExp(regpattern, regmodifiers);
			}
 
			/* perform */
			editbox.value = editbox.value.replace(arrsearch[i],arrreplace[i]);
		}
	}
 
	/* remove form */
	function customremove() {
		regexform.parentNode.removeChild(regexform);
		patterncount = -1;
	}
 
	/****************
	*** create menu
	****************/
	function rfmenu(rfmconfig) {
		/* get elements */
		// note: these must be global.
		editbox = document.getElementById('wpTextbox1');
		editreason = document.getElementById('wpSummary');
		try {
			sidebar = document.getElementById('p-navigation').parentNode;
		}
		catch(err) {
			console.log( 'Chức năng thay đổi bị huỷ bỏ vì một lỗi không rõ. Điều này hiếm khi xảy ra, hãy thử lại một lần nữa, nếu không được vui lòng tự sửa bằng tay.' );
			return;
		}
 
		/* add CSS */
		document.write('<style type="text/css">'
			+ '#p-regex li { list-style:none !important; }'
			+ '#p-regex a { color:gray !important; }'
			+ '#regexform { margin:1em; padding:0.5em; border:2px solid blue; }'
			+ '#regexform ol { list-style:none; border:1px solid #CCC; padding:0.5em; }'
			+ '#resetwarning { border:1px solid red; padding:3px; }'
			+ '</style>');
 
		/* create menu */
		var container = document.createElement('div');
		container.setAttribute('class','portlet');
		container.setAttribute('id','p-regex');
		sidebar.appendChild(container);
 
		var header = document.createElement('h5');
		header.appendChild(document.createTextNode('Scripts'));
		container.appendChild(header);
 
		var toolbox = document.createElement('div');
		toolbox.setAttribute('class','p-body');
		container.appendChild(toolbox);
 
		toollist = document.createElement('ul');
		toolbox.appendChild(toollist);
 
 
		/* load user-defined sidebar links */
		if(self['gadget_rmflinks']) { gadget_rmflinks(); }
		if(self['rmflinks']) { rmflinks(); }
	}
 
	/* load script */
	addOnloadHook(rfmenu);
}