MediaWiki:VisitorsIndicator.js: Difference between revisions

From VRChat Wiki
No edit summary
No edit summary
Line 3: Line 3:
     const element = document.querySelector("#mw-indicator-visitors-count");
     const element = document.querySelector("#mw-indicator-visitors-count");
     if (!element) return;
     if (!element) return;
    const language = mw.config.get("wgPageContentLanguage");
    const pathname = location.pathname.replace(`/${language}`, "");
    const languages = [...document.querySelectorAll(".mw-pt-languages-list a")].map(({ lang }) => lang.toLowerCase());
    const alternatives = [pathname, `${pathname}/${language}`, ...languages.map((language) => `${pathname}/${language}`)];


     async function refresh() {
     async function refresh() {
       const visitors = (await fetch(`https://plausible.io/api/stats/wiki.vrchat.com/pages/?period=realtime&filters=[["is","event:page",["${location.pathname}"]]]`)
       const filters = [["is", "event:page", alternatives]];
         .then((response) => response.json()))
      const query = new URLSearchParams({
        .results[0].visitors
        period: "realtime",
        filters: JSON.stringify(filters)
      });
 
      const value = (await fetch(`https://plausible.io/api/stats/wiki.vrchat.com/pages/?${query.toString()}`)
         .then((response) => response.json()));
 
      if (typeof value !== "object" || !("results" in value) || !Array.isArray(value.results)) return;


       element.textContent = visitors;
       element.textContent = value.results.reduce((prev, curr) => {
        prev += curr.visitors;
        return prev;
      }, 0);
     }
     }



Revision as of 02:24, 4 October 2024

$(
  (function () {
    const element = document.querySelector("#mw-indicator-visitors-count");
    if (!element) return;

    const language = mw.config.get("wgPageContentLanguage");
    const pathname = location.pathname.replace(`/${language}`, "");
    const languages = [...document.querySelectorAll(".mw-pt-languages-list a")].map(({ lang }) => lang.toLowerCase());
    const alternatives = [pathname, `${pathname}/${language}`, ...languages.map((language) => `${pathname}/${language}`)];

    async function refresh() {
      const filters = [["is", "event:page", alternatives]];
      const query = new URLSearchParams({
        period: "realtime",
        filters: JSON.stringify(filters)
      });

      const value = (await fetch(`https://plausible.io/api/stats/wiki.vrchat.com/pages/?${query.toString()}`)
        .then((response) => response.json()));

      if (typeof value !== "object" || !("results" in value) || !Array.isArray(value.results)) return;

      element.textContent = value.results.reduce((prev, curr) => {
        prev += curr.visitors;
        return prev;
      }, 0);
    }

    refresh();
    setInterval(refresh, 1000)
  })()
);