MediaWiki:CustomItemTracker.js: Difference between revisions
Jump to navigation
Jump to search
No edit summary |
No edit summary |
||
| Line 1: | Line 1: | ||
mw.hook('wikipage.content').add(function () { | mw.hook('wikipage.content').add(function () { | ||
const container = document.getElementById( | const container = document.getElementById("customItemTracker"); | ||
if (!container) return; | if (!container) return; | ||
const allowedUsers = [ | const allowedUsers = [ | ||
| Line 12: | Line 9: | ||
]; | ]; | ||
const currentUser = mw.config.get("wgUserName"); | |||
const currentUser = | |||
let trackerData = { | let trackerData = { | ||
tiers: [], | |||
completed: {} | completed: {} | ||
}; | }; | ||
function loadData() { | function loadData() { | ||
$.getJSON( | $.getJSON( | ||
mw.util.wikiScript( | mw.util.wikiScript("api"), | ||
{ | { | ||
action: | action: "query", | ||
titles: | titles: "Module:CustomItemTracker/data.json", | ||
prop: | prop: "revisions", | ||
rvprop: | rvprop: "content", | ||
format: | format: "json", | ||
formatversion: 2 | formatversion: 2 | ||
} | } | ||
).done(function(data) { | ).done(function (data) { | ||
const page = data.query.pages[0]; | |||
if (!page.revisions) | if (!page.revisions) | ||
return; | return; | ||
try { | try { | ||
| Line 55: | Line 42: | ||
page.revisions[0].content | page.revisions[0].content | ||
); | ); | ||
render(); | render(); | ||
} catch (e) { | |||
console.error(e); | |||
console.error( | |||
} | } | ||
}); | }); | ||
| Line 74: | Line 55: | ||
} | } | ||
function render() { | function render() { | ||
container.innerHTML = ""; | container.innerHTML = ""; | ||
let totalItems = 0; | |||
trackerData.tiers.forEach(function (tier) { | |||
totalItems += tier.items.length; | |||
}); | |||
let completed = 0; | |||
Object.keys(trackerData.completed).forEach(function (key) { | |||
if (trackerData.completed[key]) | |||
completed++; | |||
}); | |||
const progress = | const progress = | ||
document.createElement("div"); | document.createElement("div"); | ||
progress.className = "trackerProgress"; | |||
progress.className = | |||
progress.innerHTML = | progress.innerHTML = | ||
`Completed: <strong>${completed}/${totalItems}</strong> (${Math.floor((completed / totalItems) * 100) || 0}%)`; | |||
container.appendChild(progress); | container.appendChild(progress); | ||
let globalIndex = 0; | |||
trackerData.tiers.forEach(function (tier) { | |||
const heading = | |||
document.createElement("h2"); | |||
const | |||
document.createElement(" | |||
heading.textContent = tier.name; | |||
heading.style.color = "#fff"; | |||
heading.style.margin = "25px 0 10px 0"; | |||
heading.style.borderBottom = "1px solid #596e96"; | |||
heading.style.paddingBottom = "5px"; | |||
container.appendChild(heading); | |||
const | const grid = | ||
document.createElement("div"); | document.createElement("div"); | ||
grid.className = "trackerGrid"; | |||
container.appendChild(grid); | |||
tier.items.forEach(function (item) { | |||
const currentIndex = globalIndex++; | |||
const tile = | |||
document.createElement("div"); | |||
tile.className = "trackerTile"; | |||
if (trackerData.completed[currentIndex]) | |||
tile.classList.add("completed"); | |||
const image = | |||
document.createElement("div"); | |||
image.className = "itemImage"; | |||
tile.appendChild(image); | |||
const name = | |||
document.createElement("div"); | |||
name.className = "itemName"; | |||
name.textContent = item.name; | |||
tile.appendChild(name); | |||
loadImage(item.name, image); | |||
tile.onclick = function () { | |||
if (!allowedUsers.includes(currentUser)) | |||
return; | |||
trackerData.completed[currentIndex] = | |||
!trackerData.completed[currentIndex]; | |||
saveData(); | |||
} | }; | ||
grid.appendChild(tile); | |||
}); | |||
} | |||
}); | }); | ||
} | } | ||
function loadImage(itemName, target) { | |||
const template = "{{plinkp|" + itemName + "}}"; | |||
$.getJSON("/api.php", { | |||
action: "parse", | |||
text: template, | |||
format: "json" | |||
}).done(function (data) { | |||
if (!data.parse) | |||
if( | |||
return; | return; | ||
const wrapper = | const wrapper = | ||
document.createElement("div"); | document.createElement("div"); | ||
wrapper.innerHTML = | wrapper.innerHTML = | ||
data.parse.text["*"]; | data.parse.text["*"]; | ||
const img = | const img = | ||
wrapper.querySelector("img"); | wrapper.querySelector("img"); | ||
if (img) { | |||
img.style.maxWidth = "50px"; | |||
img.style.maxHeight = "50px"; | |||
img.style.width = "auto"; | |||
img.style.height = "auto"; | |||
img.style.maxWidth = | img.style.objectFit = "contain"; | ||
img.style.maxHeight = | |||
img.style.width = | |||
img.style.height = | |||
img.style.objectFit = | |||
target.appendChild(img); | target.appendChild(img); | ||
} | } | ||
}); | }); | ||
} | } | ||
function saveData() { | function saveData() { | ||
$.post( | $.post( | ||
mw.util.wikiScript( | mw.util.wikiScript("api"), | ||
{ | { | ||
action: | action: "edit", | ||
title: "Module:CustomItemTracker/data.json", | |||
token: mw.user.tokens.get("csrfToken"), | |||
format: | format: "json", | ||
text: JSON.stringify( | |||
text: | |||
trackerData, | trackerData, | ||
null, | null, | ||
| Line 359: | Line 215: | ||
} | } | ||
).done(function() { | ).done(function () { | ||
render(); | render(); | ||
console.log( | console.log( | ||
"Tracker updated" | "Tracker updated." | ||
); | ); | ||
}); | }); | ||
} | } | ||
loadData(); | loadData(); | ||
}); | }); | ||
Revision as of 16:24, 30 July 2026
mw.hook('wikipage.content').add(function () {
const container = document.getElementById("customItemTracker");
if (!container) return;
const allowedUsers = [
"Hefner"
];
const currentUser = mw.config.get("wgUserName");
let trackerData = {
tiers: [],
completed: {}
};
function loadData() {
$.getJSON(
mw.util.wikiScript("api"),
{
action: "query",
titles: "Module:CustomItemTracker/data.json",
prop: "revisions",
rvprop: "content",
format: "json",
formatversion: 2
}
).done(function (data) {
const page = data.query.pages[0];
if (!page.revisions)
return;
try {
trackerData =
JSON.parse(
page.revisions[0].content
);
render();
} catch (e) {
console.error(e);
}
});
}
function render() {
container.innerHTML = "";
let totalItems = 0;
trackerData.tiers.forEach(function (tier) {
totalItems += tier.items.length;
});
let completed = 0;
Object.keys(trackerData.completed).forEach(function (key) {
if (trackerData.completed[key])
completed++;
});
const progress =
document.createElement("div");
progress.className = "trackerProgress";
progress.innerHTML =
`Completed: <strong>${completed}/${totalItems}</strong> (${Math.floor((completed / totalItems) * 100) || 0}%)`;
container.appendChild(progress);
let globalIndex = 0;
trackerData.tiers.forEach(function (tier) {
const heading =
document.createElement("h2");
heading.textContent = tier.name;
heading.style.color = "#fff";
heading.style.margin = "25px 0 10px 0";
heading.style.borderBottom = "1px solid #596e96";
heading.style.paddingBottom = "5px";
container.appendChild(heading);
const grid =
document.createElement("div");
grid.className = "trackerGrid";
container.appendChild(grid);
tier.items.forEach(function (item) {
const currentIndex = globalIndex++;
const tile =
document.createElement("div");
tile.className = "trackerTile";
if (trackerData.completed[currentIndex])
tile.classList.add("completed");
const image =
document.createElement("div");
image.className = "itemImage";
tile.appendChild(image);
const name =
document.createElement("div");
name.className = "itemName";
name.textContent = item.name;
tile.appendChild(name);
loadImage(item.name, image);
tile.onclick = function () {
if (!allowedUsers.includes(currentUser))
return;
trackerData.completed[currentIndex] =
!trackerData.completed[currentIndex];
saveData();
};
grid.appendChild(tile);
});
});
}
function loadImage(itemName, target) {
const template = "{{plinkp|" + itemName + "}}";
$.getJSON("/api.php", {
action: "parse",
text: template,
format: "json"
}).done(function (data) {
if (!data.parse)
return;
const wrapper =
document.createElement("div");
wrapper.innerHTML =
data.parse.text["*"];
const img =
wrapper.querySelector("img");
if (img) {
img.style.maxWidth = "50px";
img.style.maxHeight = "50px";
img.style.width = "auto";
img.style.height = "auto";
img.style.objectFit = "contain";
target.appendChild(img);
}
});
}
function saveData() {
$.post(
mw.util.wikiScript("api"),
{
action: "edit",
title: "Module:CustomItemTracker/data.json",
token: mw.user.tokens.get("csrfToken"),
format: "json",
text: JSON.stringify(
trackerData,
null,
4
)
}
).done(function () {
render();
console.log(
"Tracker updated."
);
});
}
loadData();
});