注意:在保存之后,您可能需要清除浏览器缓存才能看到所作出的变更的影响。
?_=1
来访问最新页面。https://zh.moegirl.org.cn/User:D41D8CD98F/FlowThreadPreview.js?_=1
// [[MediaWiki:Gadget-FlowThreadPreview.js]]的修改版 // 相比于原版,做出的改动: // * 改正BUG // * ext.flowthread 会在单击某条评论的回复按钮时动态构造回复框,防止其影响原有功能。如果浏览器支持,也为这种回复框也加上预览 // * 暴露 wgFlowThreadPreviewExportedFunctions = {addPreviewButton,doPreview} 给外部,坏处是污染全局空间,好处是外部可以自行选择调用预览函数(添加预览按钮、执行预览操作)的时机 // * 暴露 wgFlowThreadPreviewConfig 作为可选的设置参数 // * 调整样式。不再手动调整评论预览框的高度 // * 在找不到回复框的时候会尝试加载 ext.flowthread 然后重试 window.wgFlowThreadPreviewConfig = window.wgFlowThreadPreviewConfig || {}; (function() { if ($('#flowthreadPreviewBox')[0]) return; var exportFunction = window.wgFlowThreadPreviewConfig.exportFunction === false ? false : true, useDefaultStyle = window.wgFlowThreadPreviewConfig.useDefaultStyle === false ? false : true, useExtFlowthread = window.wgFlowThreadPreviewConfig.useExtFlowthread === false ? false : true, autoAddPreviewBTN = window.wgFlowThreadPreviewConfig.autoAddPreviewBTN === false ? false : true, previewBoxTemplate = window.wgFlowThreadPreviewConfig.previewBoxTemplate || '<div id="flowthreadPreviewBox" style="margin-left:calc((99vw - 600px)/2);height:auto;"><div id="flowthreadPreviewBoxTitle" style="padding:20px 10px 0 10px;"><div id="flowthreadPreviewBoxTitleContent">评论预览<span id="flowthreadPreviewBoxCloseButton">×</span></div></div><div id="flowthreadPreviewBoxContent"><div id="flowthreadPreviewBoxContentHTML" style="position:relative;top:0;margin:0 10px 20px 10px;"></div><div id="flowthreadPreviewBoxContentLoading"><img src="/extensions/FancyBoxThumbs/modules/fancyBox/source/fancybox_loading.gif"/>正在加载中……</div></div></div>', previewButtonTemplate = window.wgFlowThreadPreviewConfig.previewButtonTemplate || '<button class="comment-submit comment-preview" style="right:99px">预览</button>'; matchedBlacklistTemplate = window.wgFlowThreadPreviewConfig.matchedBlacklistTemplate || '<hr /><div><div style="color: red;">检测到评论内容命中过滤规则:</div><ol style="font-family: monospace;" class="comment-matched-rules"></ol><div style="color:red;">请修改评论内容!</div><div>以上规则使用正则表达式定义,如果您不清楚,就不要在意了。</div></div>'; if (!exportFunction && !autoAddPreviewBTN) { console && console.error("User:D41D8CD98F/FlowThreadPreview: " + "Neither exportFunction nor autoAddPreviewBTN is set. " + "Can't do anything useful! "); return; } if (useExtFlowthread && !mw.config.exists('canpost')) return; if (useDefaultStyle) mw.loader.load('/index.php?title=MediaWiki:Gadget-FlowThreadPreview.css&action=raw&ctype=text/css', 'text/css'); var box = $(previewBoxTemplate), button = $(previewButtonTemplate), err = $(matchedBlacklistTemplate), rules = []; function bindFunc() { var replybox = $(this).closest('.comment-replybox'), button = replybox.find('.comment-preview'), checkbox = replybox.find('.comment-toolbar input:checkbox'), textarea = replybox.find('textarea'); if (checkbox.is(':checked') && textarea.val() !== '') { button.data('disable', false).css({ 'opacity': '1', 'cursor': 'pointer' }); } else { button.data('disable', true).css({ 'opacity': '0.73', 'cursor': 'not-allowed' }); } } function addPreviewButton(replybox) { replybox = replybox || $('.comment-replybox'); var r = replybox.filter(':not(:has(.comment-preview))'); if (r.length !== 0) button.clone(true).appendTo(r.find('.comment-toolbar')); replybox.find('.comment-toolbar input:checkbox').on('click.D4.FlowThreadPreview', bindFunc); replybox.find('textarea').on('input.D4.FlowThreadPreview',bindFunc); bindFunc.call(replybox); } function autoAddPreviewButton() { var MutationObserver = window.MutationObserver || window.WebKitMutationObserver; addPreviewButton(); if (MutationObserver) { var observer = new MutationObserver(function(mutationRecords) { mutationRecords.forEach(function(rec) { var replybox = $(rec.addedNodes).filter('.comment-replybox'); if (replybox.length === 0) return; addPreviewButton(replybox); }); }); observer.observe($('.comment-container')[0], {childList: true, subtree: true}); } } function doPreview(event, box) { if ($(this).data('disable')) return false; box = box || $('#flowthreadPreviewBox'); var text = $(this).closest('.comment-replybox').find('textarea').val(); if (box.is(':visible')) { box.find('#flowthreadPreviewBoxContentLoading').fadeIn(370); box.find('#flowthreadPreviewBoxContentHTML').fadeOut(370); } else { box.fadeIn(370); } function error(text) { box.find('#flowthreadPreviewBoxContentLoading').fadeOut(370); box.find('#flowthreadPreviewBoxContentHTML').text(text).fadeIn(370); } $.ajax({ url: '/api.php', type: "POST", data: { action: 'parse', format: 'json', formatversion: 2, text: text, prop: 'text', preview: true, contentformat: 'text/x-wiki', contentmodel: 'wikitext' }, success: function(data) { if (data.parse && data.parse.text) { var html = data.parse.text, e = []; box.find('#flowthreadPreviewBoxContentLoading').fadeOut(370); box.find('#flowthreadPreviewBoxContentHTML').html(html); rules.forEach(function(v) { if (v.test(text)) e.push(v); }); if (e[0]) { e.forEach(function(v, i) { e[i] = v.toString().slice(1, -1); }); err.find('.comment-matched-rules').html('<li>' + e.join('</li><li>') + '</li>'); box.find('#flowthreadPreviewBoxContentHTML').append(err); } box.find('#flowthreadPreviewBoxContentHTML').fadeIn(370); } else if (data.error && data.error.info) error('萌百服务器返回一个错误:' + data.error.info); else error(''); }, error: function(eO, eM, eC) { eC ? eC += '.' + eM : eC = eM; error('和萌百服务器通信失败,无法获取预览内容!\n错误信息:' + eC); } }); } box.appendTo('body'); $.get('/index.php?title=MediaWiki:Flowthread-blacklist&action=raw', function(data) { var lines = data.split('\n'); lines.forEach(function(v, i) { var length = v.indexOf('#') === -1 ? v.length : v.indexOf('#'), regexp = v.slice(0, length).trim(); if (regexp) rules.push(RegExp(regexp)); }); }); button.on('click.D4.FlowThreadPreview', doPreview); box.find('#flowthreadPreviewBoxCloseButton').on('click.D4.FlowThreadPreview', function() { box.fadeOut(370).delay(370).queue(function() { box.find('#flowthreadPreviewBoxContentLoading').show(); box.find('#flowthreadPreviewBoxContentHTML').empty(); $(this).dequeue(); }); }); if (exportFunction) { window.wgFlowThreadPreviewExportedFunctions = { addPreviewButton: addPreviewButton, doPreview: doPreview, }; } if (autoAddPreviewBTN) { if (!$('.comment-replybox')[0]) { if (useExtFlowthread) $(function(){mw.loader.using('ext.flowthread').done(autoAddPreviewButton)}); } else { autoAddPreviewButton(); } } })();