User:ShowierData9978/wikicrawl/module.js

function i(e, t, a) {

 return localStorage.getItem(e) === null ? (a && a(), !1) : (t(), localStorage.removeItem(e), !0);

} function n(e, t) {

 var a = $(document.createElement(e));
 return a.attr(t), a;

} function s() {

 window.wh.data.tree = {}, window.wh.data.root = [], window.wh.data.lastUpdated = mw.now(), localStorage.setItem("wk:list", JSON.stringify(window.wh.data)), o();

} function c() {

 return n("button", {
   class: "wikicrawl-clear-button"
 }).append(
   $('')
 ).on("click", s);

} function u() {

 var w;
 function e() {
   localStorage.setItem("wk:list", JSON.stringify(window.wh.data));
 }
 const t = (w = JSON.parse(localStorage.getItem("rwRecentlyVisited") || "[]")[0]) == null ? void 0 : w.replace("_", " ");
 let a = mw.config.get("wgPageName").replace("_", " ");
 return window.wh.data.lastUpdated = mw.now(), i("wk:randompage", () => {
   window.wh.data.root.push(a), window.wh.data.tree[a] || (window.wh.data.tree[a] = []);
 }) ? e() : (a in window.wh.data.tree || (window.wh.data.tree[a] = []), !t || window.wh.data.root.length === 0 ? (window.wh.data.root.push(a), e()) : (window.wh.data.tree[t] || (window.wh.data.tree[t] = []), t !== a && !(a in window.wh.data.tree[t]) && window.wh.data.tree[t].push(a), e()));

} function p(e) {

 e in window.wh.data.tree && delete window.wh.data.tree[e], Object.entries(window.wh.data.tree).forEach((t) => {
   window.wh.data.tree[t[0]] = t[1].filter((a) => a !== e);
 }), window.wh.data.root = window.wh.data.root.filter((t) => t !== e), window.wh.data.lastUpdated = mw.now(), localStorage.setItem("wk:list", JSON.stringify(window.wh.data)), o();

} let r = []; function h(e) {

 const t = e.replace(" ", "_") === mw.config.get("wgPageName");
 return n(t ? "b" : "a", {
   class: "wikicrawl-tree-link",
   ...!t && {
     href: mw.util.getUrl(e),
     id: `wh-${e}`
   }
 }).text(e).on("click", (a) => {
   localStorage.setItem("wk:skipAdd", "true");
 });

} function m(e) {

 return n("button", {
   class: "wikicrawl-remove-button",
   id: `wh-remove-${e}`
 }).text("Remove").on("click", () => p(e));

} function d(e, t) {

 r.includes(e) ? t = [] : r.push(e);
 let a = n("ul", {});
 return t == null || t.forEach((w) => {
   const l = window.wh.data.tree[w];
   d(w, l).appendTo(a);
 }), n("details", {
   class: "wikicrawl-tree-details"
 }).append(
   n("summary", {}).append(h(e)).append(m(e))
 ).append(a);

} function f() {

 return n("button", {
   class: "wikicrawl-close-button"
 }).append(
   $('')
 ).on("click", () => {
   $("#wikicrawl").addClass("hidden"), $("body").removeClass("hidden");
 });

} function k() {

 const e = $("body").hasClass("hidden"), t = n("div", {
   id: "wikicrawl",
   ...!e && {
     class: "hidden"
   }
 });
 return t.append(
   n("h2", {
     class: "wikicrawl-title"
   }).text("Wikicrawl")
 ), t.append(f()), t.append(c()), window.wh.data.root.forEach((a) => {
   const w = window.wh.data.tree[a];
   t.append(d(a, w));
 }), t;

} function o() {

 let e = document.getElementById("wikicrawl");
 $("body").removeClass("hidden"), $("body").prepend(k()), e == null || e.remove();

} function g() {

 let e = window.wh.url;
 mw.loader.load(`${e}/styles.css`, "text/css"), window.wh.data = JSON.parse(localStorage.getItem("wk:list") || "{}");
 let t = mw.util.addPortletLink("p-views", "#", "Open Wikicrawl", "wikicrawl-open", "Open the wikicrawl sidebar");
 if (t === null)
   throw new Error("Failed to add portlet link");
 $(t).on("click", (a) => {
   a.preventDefault(), $("#wikicrawl").removeClass("hidden"), $("body").addClass("hidden");
 });

} function v() {

 i("wk:skipAdd", () => {
 }, u), $("#n-randompage a").on("click", (e) => {
   localStorage.setItem("wk:randompage", "true");
 }), o();

} mw.loader.using("mediawiki.util", () => {

 $(async () => {
   try {
     g(), v();
   } catch (e) {
     throw alert(e), e;
   }
 });

}); export {

 o as draw

};