User:A2569875/Tools/RemoveHiddenCharsTool.js

维基百科,自由的百科全书

注意:保存之后,你必须清除浏览器缓存才能看到做出的更改。Google ChromeFirefoxMicrosoft EdgeSafari:按住⇧ Shift键并单击工具栏的“刷新”按钮。参阅Help:绕过浏览器缓存以获取更多帮助。

// 移除不可見字元
// WARNING:如果條目內有從右往左寫的文字(例如阿拉伯文等),該腳本會構成破壞!
// 基於 User:Vanished user 1929210/removehiddenchars.js 改成左側工具欄按鈕
// See also: Module:沙盒/Artoria2e5/unicode-other

(function($, mw) {
	if(typeof(wgULS)==="undefined")wgULS=function(text){return text;};
	function removehiddenchars() {
	    // Cc:00-1f(除去09、0a和0d)、7f-9f
	    // Cf(部分):200b-200f、202c-202e、2060-2064、206a-206f、feff
	
	    var CH = /[\u0000-\u0008\u000b\u000c\u000e-\u001f\u007f-\u009f\u200b-\u200f\u202c-\u202e\u2060-\u2064\u206a-\u206f\ufeff]/ug;
	    var SUMMARY = wgULS('去除不可见字符', '去除不可見字元');
	
	    var remove = function (textbox, summarybox, checkbox) {
	        var text = $(textbox).val();
	        console.log(CH.test(text));
	        if (CH.test(text)) {
	            $(textbox).val(text.replace(CH, ''));
	            if ($(summarybox).val() === '') {
	                $(summarybox).val(SUMMARY);
	                $(checkbox).attr('checked', 'checked');
	            }
	        }
	        return text;
	    };
	
	    if (mw.config.get('wgAction') === 'edit') {
	    	var wpTextbox = "#wpTextbox1";
	    	var clena_flag = !!CH.test($(wpTextbox).val());
	        var text = remove(wpTextbox, '#wpSummary', '#wpMinoredit');

	        window.restoreInvisibleChars = function () {
	            $(wpTextbox).val(text);
	        };
	        if(clena_flag){
	        	OO.ui.alert(wgULS('已清除不可见字符。', '已清除不可見字元。'));
	        }else{
	        	OO.ui.alert(wgULS('找不到不可见字符。', '找不到不可見字元。'));
	        }
	    } else {
        	var $quick_edit = $('#Wikiplus-Quickedit');
        	if($quick_edit && $quick_edit.length > 0){
	            var _text = $('#Wikiplus-Quickedit').val();
	            if (CH.test(_text)) {
	                $('#Wikiplus-Quickedit').val(_text.replace(CH, ''));
	                $('#Wikiplus-Quickedit-Summary-Input').val(SUMMARY);
	                $('#Wikiplus-Quickedit-MinorEdit').attr('checked', 'checked');
	                OO.ui.alert(wgULS('已清除不可见字符。', '已清除不可見字元。'));
	            }else{
	            	OO.ui.alert(wgULS('找不到不可见字符。', '找不到不可見字元。'));
	            }
        	}
	    }
	}
	function add_removehiddenchars_btm($, mw) {
		var portletLink = mw.util.addPortletLink('p-tb', '#', wgULS('清理不可见字符', '清理不可見字元'), 't-rhct', wgULS('清理不可见字符', '清理不可見字元'));
		$(portletLink).click(function(e) {
			e.preventDefault();
			removehiddenchars();
		});
	}
	if (mw.config.get('wgAction') === 'edit') {
	    add_removehiddenchars_btm($, mw);
	} else {
	    $('body').one('focus', '#Wikiplus-Quickedit', function () {
	        add_removehiddenchars_btm($, mw);
	    });
	}
})(jQuery, mw);