Discourse注册跳转异常:点击注册未重定向至指定URL
Hey there! Let's work through why your custom code isn't blocking the default registration behavior or finding the target elements properly.
First, let's break down the core problem: your thingy variable has a length of 0 even though you can see the element in the DOM. That's almost always because Discourse is a dynamic single-page app (built with Ember.js)—when your code runs in the </head> or </body> section, the registration button (or form) hasn't been rendered yet. Your script executes before the element exists, so querySelectorAll comes up empty.
Fix 1: Wait for Dynamic Content to Load
Instead of running your code immediately, use a MutationObserver to watch for the registration element to appear in the DOM. This ensures your code only runs once the target is available. Here's a working example:
document.addEventListener('DOMContentLoaded', function() { // Set up an observer to watch for changes to the page content const observer = new MutationObserver((mutations) => { mutations.forEach((mutation) => { // Update this selector to match your actual registration button/link const registerElements = document.querySelectorAll('a[href="/register"], button.register-btn'); if (registerElements.length > 0) { // Stop observing once we find the elements we need observer.disconnect(); // Loop through each element and attach the click handler registerElements.forEach((el) => { el.addEventListener('click', (e) => { // Block the default registration action e.preventDefault(); e.stopPropagation(); // Extra step to prevent Discourse's internal handlers // Your custom redirect logic here window.location.href = '/your-custom-registration-page'; }); }); } }); }); // Watch the entire body for dynamic content changes observer.observe(document.body, { childList: true, subtree: true }); });
Fix 2: Verify Your Selector is Correct
Double-check the selector you're using to target the registration element. Use your browser's DevTools (right-click the button → Inspect) to confirm the exact class, ID, or attribute. For example, Discourse's default registration link might have a class like btn or signup-button, or an href of /register.
Fix 3: Use Discourse's Built-in Events (Advanced)
If you want a more robust approach, leverage Discourse's internal API to hook into page changes. This works well because it's aware of Ember's routing:
// Wait for Discourse to be fully initialized Discourse.ExternalNavItem = Discourse.ExternalNavItem || {}; Discourse.ExternalNavItem.build = function() { api.onPageChange(() => { // Check if we're on the page with the registration button if (window.location.pathname === '/') { const registerBtn = document.querySelector('a[href="/register"]'); if (registerBtn) { registerBtn.addEventListener('click', (e) => { e.preventDefault(); window.location.href = '/your-custom-page'; }); } } }); };
Key Notes:
- Always test your code in Discourse's safe mode (add
?safe_mode=1to your URL) to rule out conflicts with other plugins. - Make sure you're adding the code in the section of the custom HTML/CSS panel—scripts in the
<head>might run too early even withDOMContentLoaded.
内容的提问来源于stack exchange,提问作者nadermx




