GithubHelp home page GithubHelp logo

Comments (4)

laymonage avatar laymonage commented on June 16, 2024 1

Yes, add an element with a giscus class and it’ll be placed there.

from giscus.

laymonage avatar laymonage commented on June 16, 2024 1

Yeah the giscus script tag loads immediately instead of being in a load event listener so your script probably gets executed after giscus loads. Either make it run immediately (and put it before the giscus script tag), or add the giscus script tag dynamically: #336 (comment) (you can probably just avoid making the div.giscus element and put the dynamically created script right where the placeholder is).

from giscus.

jlxq0 avatar jlxq0 commented on June 16, 2024

Hmmm ... My approach was placing this in the header:

<script>
window.addEventListener('load', function () {
  var node_to_replace = document.querySelector('li a[title="PLACEHOLDER"]').parentElement;
  var giscus_div = document.createElement('div');
  giscus_div.className="giscus";
  node_to_replace.replaceWith(giscus_div);
})
</script>

Is that a valid approach?

It is working as in: it is replacing that PLACEHOLDER link with a div with the class giscus – however giscus is still loading at the end of the page. Maybe some kind of "race condition"?

(I should add that I cannot just add any random content to the page, and thus can't just add a div with a specific class myself. The only thing I can add in that position is links and the only thing I can influence of the links is the title, the href and the text between the a-tags.)

Edit: The site I'm playing around with is https://www.sleepless.sg/1

from giscus.

jlxq0 avatar jlxq0 commented on June 16, 2024

Sorry for not getting back earlier, @laymonage.

I'm still not sure this is the most elegant solution, but it works, as you can see on https://www.sleepless.sg/1 – and that's what counts for me. 👍

<script>
  window.addEventListener('load', function () {
    var node_to_replace = document.querySelector('li a[title="PLACEHOLDER"]').parentElement;
    var giscus_div = document.createElement('div');
    giscus_div.className="giscus";
    node_to_replace.replaceWith(giscus_div);

    let giscusAttributes = {
      "src": "https://giscus.app/client.js",
      "data-repo": "xxx",
      "data-repo-id": "xxx",
      "data-category": "Website Comments",
      "data-category-id": "xxx",
      "data-mapping": "pathname",
      "data-reactions-enabled": "1",
      "data-emit-metadata": "0",
      "data-theme": light,
      "data-lang": "en",
      "crossorigin": "anonymous",
      "async": "",
    };

    let giscusScript = document.createElement("script");
    Object.entries(giscusAttributes).forEach(([key, value]) => giscusScript.setAttribute(key, value));
    document.body.appendChild(giscusScript);
  })
</script>

Thank you so much; I appreciate the help a lot!

from giscus.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.