Skip to content

Block Domain

Solved Let's Build It
  • Hello @phenomlab

    I search a javascript code for nodebb to block and replace with a specific/customize message domain an URL Wirth a blacklist on topic, quick reply and composer like plugin-beep do it.

    I’m working on it but I don’t have anything that works, hence the request for help from your expertise.

    here an exemple who doesn’t work

    // Add this script in a custom JavaScript file or inline in your HTML
    
    document.addEventListener('DOMContentLoaded', function () {
      // List of blocked domains or URLs
      var blockedDomains = [
        'example.com',
        'blockeddomain.com',
        // Add more domains as needed
      ];
    
      // Custom message to be displayed for blocked content
      var blockedMessage = 'This content is blocked.';
    
      // Function to check if a URL contains a blocked domain
      function isBlocked(url) {
        for (var i = 0; i < blockedDomains.length; i++) {
          if (url.includes(blockedDomains[i])) {
            return true;
          }
        }
        return false;
      }
    
      // Function to replace blocked content
      function replaceBlockedContent(content) {
        // Check if the content contains a blocked domain
        if (isBlocked(content)) {
          // Replace the content with the blocked message
          return blockedMessage;
        }
        return content;
      }
    
      // Get all the elements containing topic content (adjust the selector as needed)
      var topicContentElements = document.querySelectorAll('.topic-content');
    
      // Iterate through each element and replace blocked content
      topicContentElements.forEach(function (element) {
        element.innerHTML = replaceBlockedContent(element.innerHTML);
      });
    });
    

    Thanks

  • I have a great code here. I managed to modify the HTTP URL link as well as the link text (description) by modifying them both with a warning message and the URL of my choice for each domain. I am currently redirecting to my checkout page.

    The code also differentiates with text before and after the link and does not take it into account.

    It works :

    • In the topics
    • In the carousel
    • In the previews of the last post in the recent page, unread etc…
    • In Chat

    There are certainly things to improve but I’ll share the code with you here.

    –> This will be my Christmas present 🎅

    If you have any improvements or suggestions, don’t hesitate @phenomlab

    // ------------------------------------------------------------------
    // Block Domain URL on topics & Topic teasers & caroussel topic info
    // ------------------------------------------------------------------
    
    $(document).ready(function () {
      // Function to replace URLs and link text in a given HTML content
      function replaceUrls(html) {
        // Replace the old domain URLs with the new domain URL and update link text
        html = html.replace(/(<a[^>]*href="https?:\/\/(?:www\.)?francesoir\.fr(?:\/[^\s>]*)?"[^>]*>)([^<]*)<\/a>/g, '<a href="https://YOUR_URL_REDIRECTION/rules"><strong>🤖 [Oups. The PW anti fake news system detected a disinformation domain name] 🤖</strong></a>');
        html = html.replace(/(<a[^>]*href="https?:\/\/(?:www\.)?francais\.rt\.com(?:\/[^\s>]*)?"[^>]*>)([^<]*)<\/a>/g, '<a href="https://YOUR_URL_REDIRECTION/rules"><strong>🤖 [Oups. The PW anti fake news system detected a domain name of disinformation and propaganda] 🤖</strong></a>');
        html = html.replace(/(<a[^>]*href="https?:\/\/(?:www\.)?fr\.sputniknews\.africa(?:\/[^\s>]*)?"[^>]*>)([^<]*)<\/a>/g, '<a href="https://YOUR_URL_REDIRECTION/rules"><strong>🤖 [Oups. The PW anti fake news system detected a domain name of disinformation and propaganda] 🤖</strong></a>');
        html = html.replace(/(<a[^>]*href="https?:\/\/(?:www\.)?lecourrierdesstrateges\.fr(?:\/[^\s>]*)?"[^>]*>)([^<]*)<\/a>/g, '<a href="https://YOUR_URL_REDIRECTION/rules"><strong>🤖 [Oups. The PW anti fake news system detected a disinformation domain name] 🤖</strong></a>');
        html = html.replace(/(<a[^>]*href="https?:\/\/(?:www\.)?lelibrepenseur\.org(?:\/[^\s>]*)?"[^>]*>)([^<]*)<\/a>/g, '<a href="https://YOUR_URL_REDIRECTION/rules"><strong>🤖 [Oups. The PW anti fake news system detected a disinformation domain name] 🤖</strong></a>');
        html = html.replace(/(<a[^>]*href="https?:\/\/(?:www\.)?lesmoutonsenrages\.fr(?:\/[^\s>]*)?"[^>]*>)([^<]*)<\/a>/g, '<a href="https://YOUR_URL_REDIRECTION/rules"><strong>🤖 [Oups. The anti fake news system detected a domain name spreading hate speech and anti-Semitic comments] 🤖</strong></a>');
        html = html.replace(/(<a[^>]*href="https?:\/\/(?:www\.)?upr\.fr(?:\/[^\s>]*)?"[^>]*>)([^<]*)<\/a>/g, '<a href="https://YOUR_URL_REDIRECTION/rules"><strong>🤖 [Oups. The PW anti fake news system detected a disinformation domain name] 🤖</strong></a>');
        html = html.replace(/(<a[^>]*href="https?:\/\/(?:www\.)?lecourrier-du-soir\.com(?:\/[^\s>]*)?"[^>]*>)([^<]*)<\/a>/g, '<a href="https://YOUR_URL_REDIRECTION/rules"><strong>🤖 [Oups. The PW anti fake news system detected a disinformation domain name] 🤖</strong></a>');
        html = html.replace(/(<a[^>]*href="https?:\/\/(?:www\.)?fdesouche\.com(?:\/[^\s>]*)?"[^>]*>)([^<]*)<\/a>/g, '<a href="https://YOUR_URL_REDIRECTION/rules"><strong>🤖 [Oups. The anti-fake news system detected a domain name disseminating racist hate articles] 🤖</strong></a>');
        // Add more lines for additional domains if needed
        return html;
      }
    
      // Function to replace URLs and link text in a given content element
      function replaceUrlsInContent($content) {
        $content.html(function (_, oldHtml) {
          return replaceUrls(oldHtml);
        });
      }
    
      // Replace URLs and link text when a new post is added
      $(window).on('action:posts.loaded', function () {
        $('[component="post"]').each(function () {
          var $postContent = $(this).find('.content, .topic-info.text-sm.text-break, .content.mt-2.text-break, .stretched-link, .message-body.ps-0.py-0.overflow-auto.text-break');
          // Replace URLs and link text in the post content
          $postContent.html(replaceUrls($postContent.html()));
        });
      });
    
      // Replace URLs and link text when a post is edited
      $(window).on('action:topic.loaded', function () {
        $('[component="post"]').each(function () {
          var $postContent = $(this).find('.content, .topic-info.text-sm.text-break, .content.mt-2.text-break, .stretched-link, .message-body.ps-0.py-0.overflow-auto.text-break');
          // Replace URLs and link text in the post content
          $postContent.html(replaceUrls($postContent.html()));
        });
      });
    
      // Replace URLs and link text when a new page is loaded or content is updated for topic teaser & carroussel topic info
      $(document).ajaxComplete(function () {
        $('.post-content.text-xs.ps-2.line-clamp-sm-2.lh-sm.text-break, .topic-info.text-sm.text-break, .content.mt-2.text-break, .stretched-link, .message-body.ps-0.py-0.overflow-auto.text-break').each(function () {
          var $content = $(this);
          replaceUrlsInContent($content);
        });
      });
    });
    

    PS : nothing to see but appearance code on topics bug :
    The Block code is no longer displayed in its entirety but only on one line :

    f28171b1-e396-49dc-a8f3-97be40f04371-image.png

  • @DownPW What should the desired effect be from the composer - for example, prevent submission etc? If you can provide a bit more info in relation to what should happen, this would be beneficial.

  • I would prefer the same behavior as the nodebb-plugin-beep plugin :

    The URL is replaced in the quick reply component, the composer and in topics but does not prevent the submission of the message or post, just replace URL


    I changed my approach and I have code that works but only on topics using [component="post"] but after posting the message, URL is not replaced I must refresh the page.

    $(document).ready(function () {
      $(window).on('action:ajaxify.end', function () {
        // Target the post content within the post component
        $('[component="post"]').each(function () {
          var $postContent = $(this).find('.content');
    
          // Replace the old domain URLs with the new domain URL
          $postContent.html(function (index, oldHtml) {
            // Match the old domain URLs
            oldHtml = oldHtml.replace(/https?:\/\/(www\.)?francesoir\.fr[^\s]*/g, 'https://new-domain.com');
            oldHtml = oldHtml.replace(/https?:\/\/(www\.)?francais\.rt.com[^\s]*/g, 'https://new-domain.com');
            return oldHtml;
          });
        });
      });
    });
    
  • working code only on topics :

    $(document).ready(function () {
      // Function to replace URLs in a given HTML content
      function replaceUrls(html) {
        // Replace the old domain URLs with the new domain URL
        html = html.replace(/https?:\/\/(www\.)?francesoir\.fr[^\s]*/g, 'https://new-domain.com');
        html = html.replace(/https?:\/\/(www\.)?francais\.rt.com[^\s]*/g, 'https://new-domain.com');
        // Add more lines for additional domains if needed
        return html;
      }
    
      // Replace URLs when a new post is added
      $(window).on('action:posts.loaded', function () {
        $('[component="post"]').each(function () {
          var $postContent = $(this).find('.content');
          // Replace URLs in the post content
          $postContent.html(replaceUrls($postContent.html()));
        });
      });
    
      // Replace URLs when a post is edited
      $(window).on('action:topic.loaded', function () {
        $('[component="post"]').each(function () {
          var $postContent = $(this).find('.content');
          // Replace URLs in the post content
          $postContent.html(replaceUrls($postContent.html()));
        });
      });
    });
    

    It’s already not bad, just adapt it for the quick reply and composer

  • @DownPW what you have is already what I would have suggested - particularly the hooks - as you indicate, it just needs expansion to cover other aspects.

    However, it’s worth noting that none of the code you have will prevent someone from viewing the source, and seeing the true URLs as you are not modifying any of the database entries.

  • yes you are right but it is not very serious. Most of my users won’t look at the source code but if you have better why not.

    If you have any ideas for adapting it to quick reply and composing. I’m interested. I’m taking a little break for now

  • PS : nothing to see but appearance code on topics bug :

    12c57654-b8fb-49bf-9c75-849e51ee39b1-image.png

  • @DownPW said in Block Domain:

    If you have any ideas for adapting it to quick reply and composing. I’m interested.

    Let me have a look at this in more detail in DEV.

  • In previous code, i want to add html link to rules page like this :

    html = html.replace(/https?:\/\/(www\.)?francesoir\.fr[^\s]*/g, '<a href="https://XXXXX.XXXXX.XXX/rules">Test</a>');
    

    But i have this on result :

    image.png

    an idea ?

  • @DownPW Yes, direct HTML rendering is disabled (as is JS) in the composer so it will be stripped. For this to work, you’d need to enable HTML support in the composer which isn’t recommended for security reasons.

  • too bad I would have liked to be redirected to the necessary page

    ChatGPT doesn’t help on this 😉

  • I have a great code here. I managed to modify the HTTP URL link as well as the link text (description) by modifying them both with a warning message and the URL of my choice for each domain. I am currently redirecting to my checkout page.

    The code also differentiates with text before and after the link and does not take it into account.

    It works :

    • In the topics
    • In the carousel
    • In the previews of the last post in the recent page, unread etc…
    • In Chat

    There are certainly things to improve but I’ll share the code with you here.

    –> This will be my Christmas present 🎅

    If you have any improvements or suggestions, don’t hesitate @phenomlab

    // ------------------------------------------------------------------
    // Block Domain URL on topics & Topic teasers & caroussel topic info
    // ------------------------------------------------------------------
    
    $(document).ready(function () {
      // Function to replace URLs and link text in a given HTML content
      function replaceUrls(html) {
        // Replace the old domain URLs with the new domain URL and update link text
        html = html.replace(/(<a[^>]*href="https?:\/\/(?:www\.)?francesoir\.fr(?:\/[^\s>]*)?"[^>]*>)([^<]*)<\/a>/g, '<a href="https://YOUR_URL_REDIRECTION/rules"><strong>🤖 [Oups. The PW anti fake news system detected a disinformation domain name] 🤖</strong></a>');
        html = html.replace(/(<a[^>]*href="https?:\/\/(?:www\.)?francais\.rt\.com(?:\/[^\s>]*)?"[^>]*>)([^<]*)<\/a>/g, '<a href="https://YOUR_URL_REDIRECTION/rules"><strong>🤖 [Oups. The PW anti fake news system detected a domain name of disinformation and propaganda] 🤖</strong></a>');
        html = html.replace(/(<a[^>]*href="https?:\/\/(?:www\.)?fr\.sputniknews\.africa(?:\/[^\s>]*)?"[^>]*>)([^<]*)<\/a>/g, '<a href="https://YOUR_URL_REDIRECTION/rules"><strong>🤖 [Oups. The PW anti fake news system detected a domain name of disinformation and propaganda] 🤖</strong></a>');
        html = html.replace(/(<a[^>]*href="https?:\/\/(?:www\.)?lecourrierdesstrateges\.fr(?:\/[^\s>]*)?"[^>]*>)([^<]*)<\/a>/g, '<a href="https://YOUR_URL_REDIRECTION/rules"><strong>🤖 [Oups. The PW anti fake news system detected a disinformation domain name] 🤖</strong></a>');
        html = html.replace(/(<a[^>]*href="https?:\/\/(?:www\.)?lelibrepenseur\.org(?:\/[^\s>]*)?"[^>]*>)([^<]*)<\/a>/g, '<a href="https://YOUR_URL_REDIRECTION/rules"><strong>🤖 [Oups. The PW anti fake news system detected a disinformation domain name] 🤖</strong></a>');
        html = html.replace(/(<a[^>]*href="https?:\/\/(?:www\.)?lesmoutonsenrages\.fr(?:\/[^\s>]*)?"[^>]*>)([^<]*)<\/a>/g, '<a href="https://YOUR_URL_REDIRECTION/rules"><strong>🤖 [Oups. The anti fake news system detected a domain name spreading hate speech and anti-Semitic comments] 🤖</strong></a>');
        html = html.replace(/(<a[^>]*href="https?:\/\/(?:www\.)?upr\.fr(?:\/[^\s>]*)?"[^>]*>)([^<]*)<\/a>/g, '<a href="https://YOUR_URL_REDIRECTION/rules"><strong>🤖 [Oups. The PW anti fake news system detected a disinformation domain name] 🤖</strong></a>');
        html = html.replace(/(<a[^>]*href="https?:\/\/(?:www\.)?lecourrier-du-soir\.com(?:\/[^\s>]*)?"[^>]*>)([^<]*)<\/a>/g, '<a href="https://YOUR_URL_REDIRECTION/rules"><strong>🤖 [Oups. The PW anti fake news system detected a disinformation domain name] 🤖</strong></a>');
        html = html.replace(/(<a[^>]*href="https?:\/\/(?:www\.)?fdesouche\.com(?:\/[^\s>]*)?"[^>]*>)([^<]*)<\/a>/g, '<a href="https://YOUR_URL_REDIRECTION/rules"><strong>🤖 [Oups. The anti-fake news system detected a domain name disseminating racist hate articles] 🤖</strong></a>');
        // Add more lines for additional domains if needed
        return html;
      }
    
      // Function to replace URLs and link text in a given content element
      function replaceUrlsInContent($content) {
        $content.html(function (_, oldHtml) {
          return replaceUrls(oldHtml);
        });
      }
    
      // Replace URLs and link text when a new post is added
      $(window).on('action:posts.loaded', function () {
        $('[component="post"]').each(function () {
          var $postContent = $(this).find('.content, .topic-info.text-sm.text-break, .content.mt-2.text-break, .stretched-link, .message-body.ps-0.py-0.overflow-auto.text-break');
          // Replace URLs and link text in the post content
          $postContent.html(replaceUrls($postContent.html()));
        });
      });
    
      // Replace URLs and link text when a post is edited
      $(window).on('action:topic.loaded', function () {
        $('[component="post"]').each(function () {
          var $postContent = $(this).find('.content, .topic-info.text-sm.text-break, .content.mt-2.text-break, .stretched-link, .message-body.ps-0.py-0.overflow-auto.text-break');
          // Replace URLs and link text in the post content
          $postContent.html(replaceUrls($postContent.html()));
        });
      });
    
      // Replace URLs and link text when a new page is loaded or content is updated for topic teaser & carroussel topic info
      $(document).ajaxComplete(function () {
        $('.post-content.text-xs.ps-2.line-clamp-sm-2.lh-sm.text-break, .topic-info.text-sm.text-break, .content.mt-2.text-break, .stretched-link, .message-body.ps-0.py-0.overflow-auto.text-break').each(function () {
          var $content = $(this);
          replaceUrlsInContent($content);
        });
      });
    });
    

    PS : nothing to see but appearance code on topics bug :
    The Block code is no longer displayed in its entirety but only on one line :

    f28171b1-e396-49dc-a8f3-97be40f04371-image.png

  • DownPWundefined DownPW has marked this topic as solved on
  • @DownPW that code looks great. I seem to have trained you well!

    I’ll investigate the single line of code issue. I think that’s probably because of a modified version of highlight. js I use.

  • @phenomlab

    My code work great but a little more 😉
    My code conflicts with your OGPROXY code.

    Do you have an idea so that the two can live together?

  • @DownPW Potentially, yes, but what is the conflict exactly?

  • I don’t know, I just see that OG proxy does not format the links when my code to block domains is active.

    If I disable it, OG proxy works fine.

    I would have to make sure that OG proxy does not activate when a blacklist link is detected or that it works one after the other

    Actually I don’t really know at the moment.

    EDIT:

    Maybe add an ignore list to OG PROXY

  • @DownPW said in Block Domain:

    I don’t know, I just see that OG proxy does not format the links when my code to block domains is active.

    This just means that the callback / hook isn’t monitored by OGProxy and can probably be quite easily rectified.

    @DownPW said in Block Domain:

    Maybe add an ignore list to OG PROXY

    There is one already in function.js - see below

    https://github.com/phenomlab/ogproxy/blob/03d5ff125611361700d785bd82a6ab16fcd68bfc/function.js#L9

  • In any case, it doesn’t change anything in fact I think to add exclusions in OG Proxy, because links which are not to be ignored are not formatted by OG proxy

    @phenomlab said in Block Domain:

    This just means that the callback / hook isn’t monitored by OGProxy and can probably be quite easily rectified.

    I’ll look and try to figure it out but can you help me with this ?

  • @DownPW said in Block Domain:

    I’ll look and try to figure it out but can you help me with this ?

    Yes, of course.

  • no luck for now


Did this solution help you?
Did you find the suggested solution useful? Why not buy me a coffee? It's a nice gesture, and a great way to show your appreciation 💗

  • Upgrade issues

    Solved Configure
    2
    2 Votes
    2 Posts
    136 Views

    Use this code

    git fetch # Grab the latest code from the NodeBB repository git checkout v3.x git reset --hard origin/v3.x

    And you will have the latest version without specifying it

    https://docs.nodebb.org/configuring/upgrade/

  • NodeBB Theme/Skin Switcher

    Solved Customisation
    38
    7 Votes
    38 Posts
    2k Views

    @Teemberland great spot ! You should create a PR for that so they can include it in the official repository.

    Just be aware that any subsequent releases will overwrite your fix without the PR.

  • Error install plugin

    Solved Customisation
    8
    1 Votes
    8 Posts
    461 Views

    @pobojmoks

    Not WP plugin but nodeBB but it a known bug

  • 9 Votes
    20 Posts
    911 Views

    @crazycells Yeah, looking at the plugin itself, that’s a hard coded limit

    3d6dbc10-185b-4102-9470-0c2731a10750-image.png

    I’ll probably remove that… eventually…

  • 7 Votes
    10 Posts
    543 Views

    @phenomlab Thanks 🙏

  • 5 Votes
    9 Posts
    2k Views

    @phenomlab

    Very very great Mark 😉
    Thanks again, It’s perfect now !

    –> I share my code that I modified.

    I’ve added French and English comments.
    If you see things to change Mark, don’t hesitate.

    As usual, all the access paths (FA icons, logo) will have to be modified according to your architecture.

    You can also very well add/remove time slots and change welcome messages to suit your needs.

    Widgets ACP/HTML Widget Footer Logo <center> <br><br> <img id="thislogo" src="path/to/my/image"> </center> Widget Welcome Message <!-- IF loggedIn --> <div class="getUsername">, <a href="/me"><span class="username"></span></a></div> <!-- ENDIF loggedIn --> CSS

    – I added the size font-weight: 900; in the CSS because otherwise some FA icon wasn’t displayed correctly and reduce margin :

    i#thisicon { font-family: "Font Awesome 5 Free"; font-style: normal; margin-right: 8px; font-weight: 900; } .getUsername { padding-top: 20px; text-align: right; } /*Smartphone*/ /*On désactive le message de bienvenue"*/ /*We disable the welcome message"*/ @media all and (max-width: 1024px) { .getUsername { display: none; } } JAVASCRIPT // ------------------------------------------ // Welcome Message avec icône et Footer logo // Welcome Message with icon and Footer logo // ------------------------------------------ $(window).on('action:ajaxify.end', function (data) { //On récupère le username dans le DOM et on l'affiche //We retrieve the username from the DOM and display it function updateUsername() { $('.getUsername .username').text(app.user.username); } if (document.readyState === 'loading') { document.addEventListener('DOMContentLoaded', updateUsername); } else { updateUsername(); } //On déclare les variables principales (themessage & thehours) ainsi que les variables secondaires correspondants aux plages horaires //We declare the main variables (themessage & thehours) as well as the secondary variables corresponding to the time slots var thehours = new Date().getHours(); var themessage; var wakeup = ('Good day'); var morning = ('Good morning'); var lunch = ('Bon appétit'); var afternoon = ('Good afternoon'); var drink = ('Cheers'); var evening = ('Good evening'); var night = ('Good night'); var welcome = ('Welcome'); var matched = false; //On peux ici tester le résultat du code en spécifiant une heure (!!!IMPORTANT: Commenter une fois le script testé!!!) //Here we can test the result of the code by specifying a time (!!!IMPORTANT: Comment once the script has been tested!!!) //thehours = 20 //On déclare les plages horaires avec les icones FA et les logos //We declare the time slots with FA icons and logos path if (thehours >= 0 && thehours < 6) { themessage = night; theicon = "fa-solid fa-moon"; thelogo = "/assets/customlogo/XXX.png"; } else if (thehours >= 6 && thehours < 8) { themessage = wakeup; theicon = "fa-solid fa-mug-hot"; thelogo = "/assets/customlogo/XXX.png"; } else if (thehours >= 8 && thehours < 12) { themessage = morning; theicon = "fa-solid fa-sun"; thelogo = "/assets/customlogo/XXX.png"; } else if (thehours >= 12 && thehours < 13) { themessage = lunch; theicon = "fas fa-hamburger"; thelogo = "/assets/customlogo/XXX.png"; } else if (thehours >= 13 && thehours < 16) { themessage = afternoon; theicon = "fa-solid fa-sun"; thelogo = "/assets/customlogo/XXX.png"; } else if (thehours >= 16 && thehours < 18) { themessage = welcome; theicon = "fa-solid fa-rocket"; thelogo = "/assets/customlogo/XXX.png"; } else if (thehours >= 18 && thehours < 19) { themessage = drink; theicon = "fa-solid fa-wine-glass"; thelogo = "/assets/customlogo/XXX.png"; } else if (thehours >= 19 && thehours < 20) { themessage = lunch; theicon = "fas fa-pizza-slice"; thelogo = "/assets/customlogo/XXX.png"; } else if (thehours >= 20 && thehours < 24) { themessage = evening; theicon = "fa-solid fa-tv"; thelogo = "/assets/customlogo/XXX.png"; } // Si la page active est un topic, on désactive/cache le message de bienvenue // If the active page is a topic, we deactivate/hide the welcome message if (window.location.href.indexOf("topic") > -1) { console.log("This is a topic, so hide the user welcome message"); $('#thisuser').hide(); } // Sinon, on affiche le message en fonction, l'icone FA et son emplacement (prepend) // Otherwise, we display the message in function, the FA icon and its location (prepend) else { $('.getUsername').prepend("<i id='thisicon' class='" + theicon + "'></i>" + themessage); $("#thislogo").attr("src", thelogo); //$('.getUsername').prepend("<img id='thisicon' src='" + thelogo + "'></>" + themessage); } });
  • 4 Votes
    12 Posts
    760 Views

    Placing this here for reference
    https://sudonix.com/topic/216/nodebb-js-script-css-theme-switcher

    Further information and posts can be found at this link

  • unable to upvote on forum

    Solved Performance
    10
    3 Votes
    10 Posts
    542 Views

    @phenomlab yes, i can understand. it is working now 🙂