איך להריץ קוד לאחר שהדף נטען

סיכום מבוסס בינה מלאכותית

In the discussion How to run code after page has rendered, pfaffman wants to remove parentheses from span.category-topics--posts-count using an api.onPageChange() call, but it’s running before the elements are rendered. merefield suggests that this won’t work and recommends customizing the component via CSS or the Ember run loop, or forking the component. pfaffman acknowledges that forking the component is the best approach, citing laziness as the initial reason for not doing so.

I want to remove the parenthesis from span.category-topics--posts-count (rendered by GitHub - discourse/discourse-right-sidebar-blocks · GitHub). I’m trying this api.onPageChange() call, but it’s not getting any countSpans, I think because it’s running before the other theme component has put them on the page.

What can I do? :crying_cat:


  api.onPageChange(() => {
    console.log("page changed");

    const countSpans = document.querySelectorAll(
      "span.category-topics--posts-count"
    );
    console.log("spans", countSpans);
    countSpans.forEach((span) => {
      const currentText = span.textContent.trim();
      const newText = currentText.replace(/[()]/g, "");
      span.textContent = newText;
      const number = parseInt(newText);
      if (isNaN(number)) {
        span.style.padding = "0px";
      } else if (number < 10) {
        span.style.padding = "0px";
      } else {
        span.style.padding = "0px 3px";
      }
    });
  });
});

אני לא מאמין שזה יעבוד.

אתה חייב לבצע את הדברים האלה באמצעות CSS או בתוך לולאת הריצה של Ember.

מה שמנע ממך להעתיק את אותו רכיב ולשנות אותו לטעמך תחת שם שונה, לטעון רכיב נושא אחר ואז להוסיף אותו בשם ב-RSB?

Laziness, mostly. And when I started it was just going to be a little css that would likely be static and they’d not need to maintain the fork, but yeah, it’s now obviously silly not to fork it.

Thanks