Anonymous
×
Create a new article
Write your page title here:
We currently have 922 articles on WIKI - Flat MMO. Type your article name above or click on one of the titles below and start writing!



WIKI - Flat MMO
922Articles

MediaWiki:Common.js: Difference between revisions

Coolrock (talk | contribs)
No edit summary
Coolrock (talk | contribs)
No edit summary
Line 130: Line 130:
     if (target && target.id !== 'content') {
     if (target && target.id !== 'content') {
         target.appendChild(searchToggle);
         target.appendChild(searchToggle);
         // Insert search bar BELOW the button row (as a sibling)
         // Insert search bar in its own row BELOW the button row
         target.parentNode.insertBefore(searchWrap, target.nextSibling);
        var searchRow = document.createElement('div');
        searchRow.className = 'header-search-row';
        searchRow.appendChild(searchWrap);
         target.parentNode.insertBefore(searchRow, target.nextSibling);
     }
     }
})();
})();

Revision as of 20:16, 15 February 2026

  1 /* Any JavaScript here will be loaded for all users on every page load. */
  2 
  3 /* ============================================================
  4    THEME TOGGLE — Dark/Light mode switcher
  5    Defaults to dark. Saves preference to localStorage.
  6    ============================================================ */
  7 (function() {
  8     // Apply theme from localStorage BEFORE page renders (avoid flash)
  9     var saved = localStorage.getItem('flatmmo-theme') || 'dark';
 10     document.documentElement.className += ' ' + saved + '-theme';
 11 
 12     // Apply search bar mode BEFORE page renders (avoid flash)
 13     var searchMode = localStorage.getItem('flatmmo-search') || 'top';
 14     document.documentElement.className += ' search-' + searchMode;
 15 
 16     document.addEventListener('DOMContentLoaded', function() {
 17         document.body.classList.add(saved + '-theme');
 18         document.body.classList.add('search-' + searchMode);
 19     });
 20 })();
 21 
 22 $(document).ready(function () {
 23 console.log("loaded");
 24 
 25 // --- Theme toggle button ---
 26 (function() {
 27     var currentTheme = localStorage.getItem('flatmmo-theme') || 'dark';
 28 
 29     // Ensure body has the right class
 30     document.body.classList.remove('dark-theme', 'light-theme');
 31     document.body.classList.add(currentTheme + '-theme');
 32 
 33     // Create toggle button
 34     var btn = document.createElement('button');
 35     btn.className = 'theme-toggle-btn';
 36     btn.title = 'Toggle light/dark mode';
 37     btn.textContent = currentTheme === 'dark' ? '☀️' : '🌙';
 38 
 39     btn.addEventListener('click', function() {
 40         var isDark = document.body.classList.contains('dark-theme');
 41         var newTheme = isDark ? 'light' : 'dark';
 42 
 43         document.body.classList.remove('dark-theme', 'light-theme');
 44         document.body.classList.add(newTheme + '-theme');
 45         document.documentElement.classList.remove('dark-theme', 'light-theme');
 46         document.documentElement.classList.add(newTheme + '-theme');
 47 
 48         localStorage.setItem('flatmmo-theme', newTheme);
 49         btn.textContent = newTheme === 'dark' ? '☀️' : '🌙';
 50     });
 51 
 52     // Insert into Cosmos header (next to wiki buttons) or fallback to content area
 53     var target = document.querySelector('.cosmos-header__wiki-buttons')
 54               || document.querySelector('.cosmos-actions')
 55               || document.querySelector('.wds-button-group')
 56               || document.querySelector('#content');
 57     if (target) {
 58         if (target.id === 'content') {
 59             // Fallback: put it at the top of content
 60             target.insertBefore(btn, target.firstChild);
 61         } else {
 62             target.appendChild(btn);
 63         }
 64     }
 65 
 66 })();
 67 
 68 // --- Search bar toggle + compact header search bar ---
 69 (function() {
 70     var searchMode = localStorage.getItem('flatmmo-search') || 'top';
 71 
 72     // Ensure body has the right class
 73     document.body.classList.remove('search-top', 'search-header');
 74     document.body.classList.add('search-' + searchMode);
 75 
 76     // Create toggle button
 77     var searchToggle = document.createElement('button');
 78     searchToggle.className = 'search-toggle-btn';
 79     searchToggle.title = 'Toggle search bar position';
 80     searchToggle.textContent = searchMode === 'top' ? '🔍' : '🔎';
 81 
 82     searchToggle.addEventListener('click', function() {
 83         var isTop = document.body.classList.contains('search-top');
 84         var newMode = isTop ? 'header' : 'top';
 85 
 86         document.body.classList.remove('search-top', 'search-header');
 87         document.body.classList.add('search-' + newMode);
 88         document.documentElement.classList.remove('search-top', 'search-header');
 89         document.documentElement.classList.add('search-' + newMode);
 90 
 91         localStorage.setItem('flatmmo-search', newMode);
 92         searchToggle.textContent = newMode === 'top' ? '🔍' : '🔎';
 93     });
 94 
 95     // Create compact header search bar
 96     var searchWrap = document.createElement('div');
 97     searchWrap.className = 'header-search-wrap';
 98 
 99     var searchInput = document.createElement('input');
100     searchInput.type = 'text';
101     searchInput.className = 'header-search-input';
102     searchInput.placeholder = 'Search wiki...';
103     searchInput.setAttribute('autocomplete', 'off');
104 
105     var searchBtn = document.createElement('button');
106     searchBtn.className = 'header-search-btn';
107     searchBtn.innerHTML = '🔍';
108     searchBtn.title = 'Search';
109 
110     function doSearch() {
111         var q = searchInput.value.trim();
112         if (q) {
113             window.location.href = mw.util.getUrl('Special:Search') + '?search=' + encodeURIComponent(q);
114         }
115     }
116 
117     searchBtn.addEventListener('click', doSearch);
118     searchInput.addEventListener('keydown', function(e) {
119         if (e.key === 'Enter') doSearch();
120     });
121 
122     searchWrap.appendChild(searchInput);
123     searchWrap.appendChild(searchBtn);
124 
125     // Insert toggle button inline with other header buttons
126     var target = document.querySelector('.cosmos-header__wiki-buttons')
127               || document.querySelector('.cosmos-actions')
128               || document.querySelector('.wds-button-group')
129               || document.querySelector('#content');
130     if (target && target.id !== 'content') {
131         target.appendChild(searchToggle);
132         // Insert search bar in its own row BELOW the button row
133         var searchRow = document.createElement('div');
134         searchRow.className = 'header-search-row';
135         searchRow.appendChild(searchWrap);
136         target.parentNode.insertBefore(searchRow, target.nextSibling);
137     }
138 })();
139 
140 $.getScript(mw.util.getUrl("MediaWiki:ProfileTags.js") + "?action=raw&ctype=text/javascript");
141 	if (document.getElementById("interactiveMap")) {
142 console.log("map loaded")
143 		$.getScript(mw.util.getUrl("MediaWiki:InteractiveMap/Objects.js") + "?action=raw&ctype=text/javascript", function() {
144             $.getScript(mw.util.getUrl("MediaWiki:InteractiveMap/NPCs.js") + "?action=raw&ctype=text/javascript", function() {
145                 $.getScript(mw.util.getUrl("MediaWiki:InteractiveMap/Maps.js") + "?action=raw&ctype=text/javascript", function() {
146                     $.getScript(mw.util.getUrl("MediaWiki:InteractiveMap.js") + "?action=raw&ctype=text/javascript", function() {
147                     });
148                 });
149             });
150         });
151 	}
152 
153 	if (document.getElementById("wardrobe")) {
154 		$.getScript(mw.util.getUrl("MediaWiki:Wardrobe.js") + "?action=raw&ctype=text/javascript");
155 		console.log("wardrobe loaded")
156 	}
157 
158     if (document.getElementById("mapEditor")) {
159         $.getScript("https://cdn.jsdelivr.net/npm/interactjs/dist/interact.min.js", function() {
160             $.getScript(mw.util.getUrl("MediaWiki:InteractiveMap/Objects.js") + "?action=raw&ctype=text/javascript", function() {
161                 $.getScript(mw.util.getUrl("MediaWiki:InteractiveMap/NPCs.js") + "?action=raw&ctype=text/javascript", function() {
162                     $.getScript(mw.util.getUrl("MediaWiki:InteractiveMap/Maps.js") + "?action=raw&ctype=text/javascript", function() {
163                         $.getScript(mw.util.getUrl("MediaWiki:MapEditor.js") + "?action=raw&ctype=text/javascript", function() {
164                         });
165                     });
166                 });
167             });
168         });
169     }
170 if (document.querySelector(".code")) {
171 	$.getScript("../custom/highlight.min.js", ()=>{
172 		document.querySelectorAll(".code").forEach(el=>hljs.highlightElement(el))
173 		console.log('Code highlighted');
174 	});
175 }
176 
177 if (document.querySelector('[class*="Tracker"]') && !document.querySelector('[class*="page-MediaWiki"]')) {
178 	$.getScript(mw.util.getUrl("MediaWiki:Trackers.js") + "?action=raw&ctype=text/javascript");
179     console.log("Tracker loaded");
180 }
181 
182 if (document.getElementById("enemiesDB")) {
183 	$.getScript(mw.util.getUrl("MediaWiki:Update_DB.js") + "?action=raw&ctype=text/javascript");
184 	console.log("DB Updater Loaded")
185 }
186 function hitChance(accuracy, defence) {
187     accuracy = parseInt(accuracy);
188     defence = parseInt(defence);
189     let hitChance = 0;
190     if(accuracy >= defence) {
191         hitChance = 1;
192     } else {
193         hitChance = 1 / (1 + defence - accuracy)
194     }
195     return (hitChance * 100).toFixed(2);
196 }
197 if (document.querySelector('.hitChance')) {
198     document.querySelectorAll(".hitChance").forEach(el => {
199         let [acc, def] = el.innerText.split(",")
200         el.innerText = "";
201         const accSpan = document.createElement("span");
202         const defSpan = document.createElement("span");
203         const hitSpan = document.createElement("span");
204         const accInput = document.createElement("input");
205         const defInput = document.createElement("input");
206         const hitValueSpan = document.createElement("span");
207 
208         accSpan.innerText = "Accuracy:";
209         defSpan.innerText = "Defence:";
210         hitSpan.innerText = "Hit Chance:";
211 
212         accInput.type = "number";
213         defInput.type = "number";
214         accInput.min = 0;
215         defInput.min = 0;
216         if(acc !== "0") {
217             accInput.disabled = true;
218         }
219         if(def !== "0") {
220             defInput.disabled = true
221         }
222         def = def < 0 ? 0 : def;
223         acc = acc < 0 ? 0 : acc;
224         accInput.defaultValue = acc;
225         defInput.defaultValue = def;
226         accInput.onchange = function(){
227             hitValueSpan.innerText = hitChance(accInput.value, defInput.value) + "%";
228         }
229         defInput.onchange = function(){
230             hitValueSpan.innerText = hitChance(accInput.value, defInput.value) + "%";
231         }
232         el.append(accSpan, accInput, defSpan, defInput, hitSpan, hitValueSpan);
233         hitValueSpan.innerText = hitChance(accInput.value, defInput.value) + "%";
234     })
235 }
236 
237 });