News for hackers. Stuff that matters.
Re:HN-comments-drawer: one line HN comments integration into... by surprisetalk on Monday December 11, 2023 @07:29AM (#38600021)
Here's my version of this (no deps, js has good html fallback), as posted on my blog.

Note that it's a share button that also works as a link to the latest comments section, so you get the best of all worlds IMO

[1] https://taylor.town/hn-discuss

    <a id="hn-discuss" href="https://news.ycombinator.com/submitlink">
      Discuss this page on HackerNews.
    </a>
    const hn = document.getElementById('hn-discuss');

    // Set href to "share current URL and title".
    hn.setAttribute(
      'href',
      `https://news.ycombinator.com/submitlink`
        + `?u=${encodeURIComponent(window.location.href)}`
        + `&t=${encodeURIComponent(document.title)}`
    );

    setTimeout(
      () => {
        // Replace href with most popular HN link from the past 4 months.
        fetch(
          `https://hn.algolia.com/api/v1/search`
            + `?tags=story`
            + `&restrictSearchableAttributes=url`
            + `&numericFilters=created_at_i>`
              + Math.round(new Date().getTime()/1000 - 4*30*86400)
            + `&query=${encodeURIComponent(window.location.href)}`
        )
          .then(res => res.json())
          .then(data => {
            const id = data?.hits?.[0]?.objectID;
            if (id)
              hn.setAttribute(
                'href',
                `https://news.ycombinator.com/item?id=${id}`
              );
          })
          .catch(err => console.error(err));
      },
      // Wait 10 seconds before hitting Algolia!
      10*1000
    );

There are no comments on this comment yet.