Help > Forum > 主题和帖子 > 限制帖子中的字数

限制帖子中的字数

您可以按照以下步骤为新主题或回复设置字数限制。文字计数器将出现在文本框下方,以帮助用户保持在限制范围内,如果他们查看了帖子,他们将无法提交帖子。版主和管理员不受此限制的约束。

  1. 登录您的Website Toolbox账户。
  2. 点击 整合 主菜单中的链接。
  3. 点击 HTML 链接。
  4. 将下面的 HTML 代码复制到 论坛头标记 HTML 代码 文本框。

    <script>
    var maximumWords = 100;
    window.addEventListener("load", function () {
    	if (!isAdministratorOrModerator()) {	
    		if(jQ("#PostTopic").length) {
    			waitForEditor("message", function () {
    				setupWordCountObserver("message");
    			});
    			jQ("#post_submit, #previewpost_sbt").on("click", function () {
    				if (showErrorMessage("message") === 1) {
    					return false;
    				} else {
    					removeWordCountMessage("message");
    				}
    			});
    		}    
    		if (jQ("#posts-list").length) {
    			checkEditorLoaded();
    			jQ("#reply_submit, #submitreply").on("click", function () {
    				if (showErrorMessage("message") === 1) {
    					return false;
    				} else {
    					removeWordCountMessage("message");
    				}
    			});
    			jQ(document).on("click", "[id^='save_']", function () {
    				var savePostId = jQ(this).closest(".post-body-content").find("textarea").attr("id");
    				savePostId = savePostId.replace("message", "");
    				var postEditEditorId = "message" + parseInt(savePostId);
    				if (showErrorMessage(postEditEditorId) === 1) {
    					var editor = tinymce.get(postEditEditorId);
    					if (editor && editor.startContent) {
    						editor.setContent(editor.startContent);
    					}
    					return false;
    				} else {
    					removeWordCountMessage(postEditEditorId);
    				}
    			});
    		}
    	}
    });
    
    function checkEditorLoaded() {
    	const observer = new MutationObserver((mutations, obs) => {
    		for (const mutation of mutations) {
    			for (const node of mutation.addedNodes) {
    				if (node.nodeType === 1 && /^message\d*_ifr$/.test(node.id)) {
    					var messageId = node.id.replace("_ifr", "");
    					waitForEditor(messageId, function () {
    						setupWordCountObserver(messageId);
    					});
    				}
    			}
    		}
    	});
    	observer.observe(document.body, {
    		childList: true,
    		subtree: true
    	});
    }
    
    function waitForEditor(id, callback) {
    	const interval = setInterval(() => {
    		const editor = tinymce.get(id);
    		if (editor && editor.initialized) {
    			clearInterval(interval);
    			callback(editor);
    		}
    	}, 100);
    }
    
    function setupWordCountObserver(id) {
    	const editor = tinymce.get(id);
    	if (!editor) return;
    	showWordCountMessage(id); 
    	// Update message on any change
    	editor.on("input", function () {
    		showWordCountMessage(id);
    	});
    	editor.on("change", function () {
    		showWordCountMessage(id);
    	});
    	editor.on("undo redo paste", function () {
    		showWordCountMessage(id);
    	});
    }
    
    function isAdministratorOrModerator() {
        return jQ(".dropdown-menu .admin_user:not(.hidden)").length || jQ(".dropdown-menu .moderator_user:not(.hidden)").length;
    }
    
    function countWords(str) {
        return str
            .replace(/<\ ?a[^>]*>/g, '')
            .replace(/(<[^>]*>)+/gi, '')
            .replace(/&(.*?);/gi, ' ')
            .replace(/(^\s*)|(\s*$)/gi, '')
            .replace(/\n/g, ' ')
            .replace(/\s+/g, ' ')
            .trim()
            .split(' ')
            .filter(Boolean)
            .length;
    }
    
    function showErrorMessage(id) {
        let postContent = "";
        const editor = tinymce.get(id);
    
        if (editor) {
            postContent = editor.getContent();
        } else {
            postContent = jQ("#" + id).val();
        }
    
        const totalWord = countWords(postContent);
    
        if (!isAdministratorOrModerator() && totalWord > maximumWords) {
            wtbx.dialog.alert("Error: The text that you have entered is too long (" + totalWord + " words). Please shorten it to " + numberWithCommas(maximumWords) + " words.");
            return 1;
        }
    
        return 0;
    }
    
    function showWordCountMessage(id) {
        const editor = tinymce.get(id);
        if (!editor) return;
    
        const content = editor.getContent();
        const totalWord = countWords(content);
        let message = "";
        if (totalWord && totalWord <= '< (totalword + 100) else if message="<span style="color: #27415f !important; padding-top:10px; font-weight:bold;">" span>'; totalword { }> 100) {
            message = '<span style="color: #dc2f2f; padding-top:10px; font-weight:bold;">' + totalWord + '</span>';
        }
        if (message) {    
          	let wordText = 'word';
          	if(totalWord > 1) {
              wordText = 'words';
            }
            message = 'You\'ve typed ' + message + ' '+wordText+'. Your submission must not exceed ' + numberWithCommas(maximumWords) + ' words.';
        }
    
        const containerId = 'errorMessage' + id;
        const existingContainer = jQ('#' + containerId);
    
        if (!existingContainer.length) {
            const editorContainer = jQ(editor.iframeElement).closest('.mce-tinymce');
            editorContainer.after('
    ' + message + '
    '); } else { existingContainer.html(message); } } function removeWordCountMessage(id) { jQ('#errorMessage' + id).remove(); } function numberWithCommas(num) { return num.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ","); } </script> <style> #errorMessagemessage:has(span), #errorMessagemessage1:has(span) { font-size: 14px; color: #777; } #quickReplyPost #errorMessagemessage:has(span) ~ #reply_options { margin-top: -70px; } #quickReplyPost:has(#showCaptcha:not(.hidden)) #errorMessagemessage:has(span) ~ #reply_options { margin-top: -6px; } @media (max-width: 767px) { #quickReplyPost #errorMessagemessage:has(span) ~ #reply_options { margin-top: -87px; } } @media (max-width: 555px) { #quickReplyPost [id^="showCaptcha"]:not(.hidden) ~ .reply-menu .reply-options { margin-top: -187px; } } </style>
  5. 在你复制/粘贴的代码的第二行上,更改旁边的数字 最大字数 达到您希望允许的最大字数。
  6. 保存 这些变化。


If you still need help, please contact us.