MediaWiki

Difference between revisions of "Gadget-Bibliography.js"

From ACES

Line 12: Line 12:
     console.log("!");
     console.log("!");
     if (!allowedPages.includes(mw.config.get("wgPageName"))) return;
     if (!allowedPages.includes(mw.config.get("wgPageName"))) return;
    console.log($(".bibliography"));
     $(".bibliography").each(function (i, e) {
     $(".bibliography").each(function (i, e) {
       var Cite = require("citation-js");
       var Cite = require("citation-js");
Line 42: Line 43:
     function onLoaded() {
     function onLoaded() {
       scriptLoaded++;
       scriptLoaded++;
       if (scriptLoaded === scripts.length + 1) callback();
       if (scriptLoaded === scripts.length)
        mw.hook("wikipage.content").add(callback);
     }
     }
     scripts.forEach(function (s) {
     scripts.forEach(function (s) {
       mw.loader.getScript(s).then(onLoaded);
       mw.loader.getScript(s).then(onLoaded);
     });
     });
    mw.hook("wikipage.content").add(onLoaded);
   }
   }


  mw.loader.load(
    "https:////cdn.datatables.net/1.11.0/css/jquery.dataTables.min.css",
    "text/css"
  );
   loadScripts(
   loadScripts(
     [
     [
Line 60: Line 57:
     ],
     ],
     run
     run
  );
  mw.loader.load(
    "https:////cdn.datatables.net/1.11.0/css/jquery.dataTables.min.css",
    "text/css"
   );
   );
});
});

Revision as of 05:05, 3 September 2021

$(function () {
  "use strict";
  var allowedPages = ["Publications"];
  console.log("!");
  if (window.BibLoaded) {
    return;
  }
  // window.BibLoaded = true;

  var scriptLoaded = 0;
  function run() {
    console.log("!");
    if (!allowedPages.includes(mw.config.get("wgPageName"))) return;
    console.log($(".bibliography"));
    $(".bibliography").each(function (i, e) {
      var Cite = require("citation-js");
      var rawBib = e.textContent;
      var bib = Cite(rawBib);
      console.log(bib);
      var data = [];
      bib.data.forEach(function (e) {
        e.render = Cite(e).format("bibliography", {
          format: "html",
          template: "apa",
          lang: "en-US",
        });
        console.log(e);
        data.push(e);
      });
      console.log("!");

      var tb = $(".bibliography-table");
      tb.append($("<thead>"));
      tb.append($("<tbody>"));
      tb.dataTable({
        data: data,
        columns: [{ data: "render" }],
      });
    });
  }

  function loadScripts(scripts, callback) {
    function onLoaded() {
      scriptLoaded++;
      if (scriptLoaded === scripts.length)
        mw.hook("wikipage.content").add(callback);
    }
    scripts.forEach(function (s) {
      mw.loader.getScript(s).then(onLoaded);
    });
  }

  loadScripts(
    [
      "https://unpkg.com/citation-js@0.5.1/build/citation.min.js",
      "https://cdn.datatables.net/1.11.0/js/jquery.dataTables.min.js",
    ],
    run
  );

  mw.loader.load(
    "https:////cdn.datatables.net/1.11.0/css/jquery.dataTables.min.css",
    "text/css"
  );
});