Skip to content

Interesting Widget code, but can't fetch API

Solved Customisation
  • To eliminate any confusion I made a simpler widget which just does hardcoded API call for image of a cat. No user input etc
    Again it runs stand alone, but returns quickly, empty, as a widget
    This example sits on aignite.nodebb.com category 8 (‘Diamond’)
    First example is still in category 10 (‘3d’)

  • @Panda the result=undefined says it all. It still returns null and we need to see the error in the browser console.

  • @Panda Just looking at this, and it seems that the service expects authentication

    49e50e3b-9744-49ef-b524-197f00c07c0b-image.png

    I also see the below in the console

    Failed to load resource: the server responded with a status of 401 (Unauthorized)
    GET data:image/png;base64,undefined net::ERR_INVALID_URL
    {"code":401,"message":"error occurred during proxy authentication: no token in request"}
    
  • Yes. There is a key included and the exact code works as stand-alone, thats why Im wonderinging why not working in a widget

  • @Panda I think this might have something to do with the reverse proxy that nginx runs for NodeBB. Do you use this, or are you using something else?

  • @phenomlab ah! Its nodebb hosted site, so they do all that

  • Pandaundefined Panda has marked this topic as solved on
  • @phenomlab
    Two things

    1. this particular forum had plans to integrate tools which use API calls into widgets.
      Now we reached the point of finding possible reason why API calls dont work, is there anyway round that?

    2. General forum question: because there were few ‘Red Herrings’ while exploring this issue, I was tempted to tidy up this thread by deleting some of the erroneous comments.
      I noticed before this forum has tight restriction times on editing / removing comments. I guess this is intentional. Is your experience that it is better to have this on a forum rather than unlimited edit / delete timer?

  • @Panda said in Interesting Widget code, but can’t fetch API:

    this particular forum had plans to integrate tools which use API calls into widgets.
    Now we reached the point of finding possible reason why API calls dont work, is there anyway round that?

    Actually, yes. I had a similar issue with the below (which you might enjoy also) when running in a page widget

    https://sudonix.org/cbg

    For this to work, I needed to call the script externally, and so used the below widget code as a guide

    <head>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.7.0/jquery.min.js" integrity="sha512-3gJwYpMe3QewGELv8k/BX9vcqhryRdzRMxVfq6ngyWXwo03GFEzjsUm8Q7RZcHPHksttq7/GFoxjCVUjkjvPdw==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
    </head>
    <style>
    .cbgresult {
        margin: 10px 0 15px 0;
        padding: 15px;
        border-radius: 6px;
        font-family: "Source Code Pro", Menlo,Monaco,Consolas,"Courier New",monospace;
        font-size: 2rem;
        text-align: center;
        background: var(--bs-code-block) !important;
    }
    .cbgresult:after {
    content: '"';
    }
    .cbgresult:before {
    content: '"';
    }
    .cbgheader {
        margin-top: 40px;
        text-align: center;
    }
    .generate {
        text-align: center;
    }
    </style>
    <script src="/assets/js/cbg.js"></script>
    <div class="cbgheader">
    <h2>Corporate BS Generator</h2>
    <p>
    Need a better sounding line? Just click the "Generate Another" button to get another killer phrase.
    </p>
    </div>
    <div id="cbg" class="cbgresult"></div>
    <div class="generate">
    <button id="reloadme" class="btn btn-primary">Generate Another</button>
    </div>
    <script>
    $(function() { // after page load
        $('.cbgresult').fadeOut(0, function() {
        $("#cbg").show();
            $(this).html(phrase());
       });
    });
    $(document).ready(function(){
    $("#cbg").show();
        $('#reloadme').click(function(){
     $("#cbg").html(phrase());
        });
    });
    </script>
    

    @Panda said in Interesting Widget code, but can't fetch API:

    I noticed before this forum has tight restriction times on editing / removing comments. I guess this is intentional. Is your experience that it is better to have this on a forum rather than unlimited edit / delete timer?

    This is correct. Posts can only be deleted before a set time period elapses, which is 30 minutes

    915f61df-01e9-40bb-9e9f-937abb2e1494-image.png

    The intention here is to give you sufficient time to remove something you feel wasn’t relevant, or you really shouldn’t have posted (see policies), but for the sake of conversational flow, posts are then indelible because they add weight and value to the overall thread. For example, if you reference a comment in the thread, but that particular post is deleted, then the discussion becomes diluted.

    Using this approach, it makes it easier for anyone else with a similar issue to see how we resolved it.

  • phenomlabundefined phenomlab has marked this topic as unsolved on
  • @phenomlab
    That is interesting code example!
    It raises new question
    How did you drop that widget into the post there?
    I hadnt seen this BSgenerator anywhere on sudonix site, do you use it somewhere already?

    Also can you explain more what you mean by calling the code externally. In my API call example, how would I go about doing that?

  • @Panda said in Interesting Widget code, but can’t fetch API:

    How did you drop that widget into the post there?
    I hadnt seen this BSgenerator anywhere on sudonix site, do you use it somewhere already?

    Yes, here

    https://sudonix.org/topic/414/corporate-bullshit-generator?_=1687774393044

    It’s not a “post” or “topic” in the common sense. It is actually a page in it’s own right and leverages nodebb-plugin-custom-pages. This in turn creates a new “route” which behaves like a page, meaning it is then exposed for widgets.

    @Panda said in Interesting Widget code, but can’t fetch API:

    Also can you explain more what you mean by calling the code externally. In my API call example, how would I go about doing that?

    By this, I mean create all the required code in an external JS file that is reachable by the NodeBB instance - so, in “public” for example - or in my case /public/js. The widget then “calls” that file and because it runs outside of the scope of NodeBB, you just need to return the values to the widget.

    Hope this makes sense?

  • Pandaundefined Panda has marked this topic as solved on

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 💗

  • 2 Votes
    18 Posts
    455 Views

    @Panda You’ll need to do that with js. With some quick CSS changes, it looks like this

    d619844f-fbfe-4cf1-a283-6b7364f6bf18-image.png

    The colour choice is still really hard on the eye, but at least you can now read the text

  • 1 Votes
    23 Posts
    1k Views

    @DownPW it’s your only realistic option at this stage.

  • 2 Votes
    8 Posts
    251 Views

    @Panda you’d be surprised. If you consider that you’d need to use the API to be able to populate a WordPress widget for example (which in turn would of course be PHP), taking this route is still immensely popular.

  • NodeBB v3

    Announcements
    2
    3 Votes
    2 Posts
    174 Views

    @cagatay JS will work fine - no changes there, and there are no plans to drop support for jQuery. More of an issue is the CSS - for which there are quite a few breaking changes. Keep an eye on sudonix.dev (my development site) where you can see progress in relation to how I am tackling the compatibility issues.

  • 3 Votes
    9 Posts
    410 Views

    The real issue here is that most people consider forums to be “dead” in the sense that nobody uses them anymore, and social media groups have taken their place. Their once dominant stance in the 90’s and early 00’s will never be experienced again, but having said that, there are a number of forums that did in fact survive the social media onslaught, and still enjoy a large user base.

    Forums tend to be niche. One that immediately sticks out is Reddit - despite looking like it was designed in the 80s, it still has an enormous user base. Another is Stack Overflow, which needs no introduction. The key to any forum is the content it offers, and the more people whom contribute in terms of posting , the more popular and widely respected it becomes as a reliable source of information.

    Forums are still intensely popular with gamers, alongside those that offer tips on hacking etc.

  • 1 Votes
    11 Posts
    555 Views

    @DownPW yes, because of the modifications that Sudonix uses, you’ll need to tailor to fit your needs.

  • 10 Votes
    16 Posts
    808 Views

    @crazycells said in creating topic specific widgets:

    Additionally if hide class exists, why are we re-defining it?

    We’re not 🤭 I misspelled it - it should be hidden

  • unable to upvote on forum

    Solved Performance
    10
    3 Votes
    10 Posts
    542 Views

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