Difference between revisions of "Gadget-Bibliography.js"
From ACES
(Created page with "var allowedPages = [ "Publications" ] $(function () { "use strict"; if (window.BibLoaded || !allowedPages.includes(mw.config.get('wgPageName'))) { return; } //...") |
|||
Line 1: | Line 1: | ||
var allowedPages = [ | var allowedPages = ["Publications"]; | ||
] | |||
$(function () { | $(function () { | ||
"use strict"; | "use strict"; | ||
if (window.BibLoaded | if (window.BibLoaded) { | ||
return; | return; | ||
} | } | ||
Line 12: | Line 10: | ||
var scriptLoaded = 0; | var scriptLoaded = 0; | ||
function run() { | function run() { | ||
if (!allowedPages.includes(mw.config.get("wgPageName"))) return; | |||
$(".bibliography").each(function (i, e) { | $(".bibliography").each(function (i, e) { | ||
var Cite = require("citation-js"); | var Cite = require("citation-js"); | ||
Line 17: | Line 16: | ||
var bib = Cite(rawBib); | var bib = Cite(rawBib); | ||
console.log(bib); | console.log(bib); | ||
var data = [] | var data = []; | ||
bib.data.forEach(function(e) { | bib.data.forEach(function (e) { | ||
e.render = Cite(e).format( | e.render = Cite(e).format("bibliography", { | ||
format: | format: "html", | ||
template: | template: "apa", | ||
lang: | lang: "en-US", | ||
}) | }); | ||
console.log(e) | console.log(e); | ||
data.push(e) | data.push(e); | ||
}) | }); | ||
console.log("!"); | console.log("!"); | ||
Line 34: | Line 33: | ||
tb.dataTable({ | tb.dataTable({ | ||
data: data, | data: data, | ||
columns: [ | columns: [{ data: "render" }], | ||
}); | }); | ||
}); | }); |
Revision as of 04:59, 3 September 2021
var allowedPages = ["Publications"];
$(function () {
"use strict";
if (window.BibLoaded) {
return;
}
// window.BibLoaded = true;
var scriptLoaded = 0;
function run() {
if (!allowedPages.includes(mw.config.get("wgPageName"))) return;
$(".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 + 1) callback();
}
scripts.forEach(function (s) {
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(
[
"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
);
});