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 in its own row BELOW the button row
         // Make target the positioning anchor, append search bar inside
         var searchRow = document.createElement('div');
         target.style.position = 'relative';
        searchRow.className = 'header-search-row';
         target.appendChild(searchWrap);
         searchRow.appendChild(searchWrap);
        target.parentNode.insertBefore(searchRow, target.nextSibling);
     }
     }
})();
})();

Revision as of 20:18, 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         // Make target the positioning anchor, append search bar inside
133         target.style.position = 'relative';
134         target.appendChild(searchWrap);
135     }
136 })();
137 
138 $.getScript(mw.util.getUrl("MediaWiki:ProfileTags.js") + "?action=raw&ctype=text/javascript");
139 	if (document.getElementById("interactiveMap")) {
140 console.log("map loaded")
141 		$.getScript(mw.util.getUrl("MediaWiki:InteractiveMap/Objects.js") + "?action=raw&ctype=text/javascript", function() {
142             $.getScript(mw.util.getUrl("MediaWiki:InteractiveMap/NPCs.js") + "?action=raw&ctype=text/javascript", function() {
143                 $.getScript(mw.util.getUrl("MediaWiki:InteractiveMap/Maps.js") + "?action=raw&ctype=text/javascript", function() {
144                     $.getScript(mw.util.getUrl("MediaWiki:InteractiveMap.js") + "?action=raw&ctype=text/javascript", function() {
145                     });
146                 });
147             });
148         });
149 	}
150 
151 	if (document.getElementById("wardrobe")) {
152 		$.getScript(mw.util.getUrl("MediaWiki:Wardrobe.js") + "?action=raw&ctype=text/javascript");
153 		console.log("wardrobe loaded")
154 	}
155 
156     if (document.getElementById("mapEditor")) {
157         $.getScript("https://cdn.jsdelivr.net/npm/interactjs/dist/interact.min.js", function() {
158             $.getScript(mw.util.getUrl("MediaWiki:InteractiveMap/Objects.js") + "?action=raw&ctype=text/javascript", function() {
159                 $.getScript(mw.util.getUrl("MediaWiki:InteractiveMap/NPCs.js") + "?action=raw&ctype=text/javascript", function() {
160                     $.getScript(mw.util.getUrl("MediaWiki:InteractiveMap/Maps.js") + "?action=raw&ctype=text/javascript", function() {
161                         $.getScript(mw.util.getUrl("MediaWiki:MapEditor.js") + "?action=raw&ctype=text/javascript", function() {
162                         });
163                     });
164                 });
165             });
166         });
167     }
168 if (document.querySelector(".code")) {
169 	$.getScript("../custom/highlight.min.js", ()=>{
170 		document.querySelectorAll(".code").forEach(el=>hljs.highlightElement(el))
171 		console.log('Code highlighted');
172 	});
173 }
174 
175 if (document.querySelector('[class*="Tracker"]') && !document.querySelector('[class*="page-MediaWiki"]')) {
176 	$.getScript(mw.util.getUrl("MediaWiki:Trackers.js") + "?action=raw&ctype=text/javascript");
177     console.log("Tracker loaded");
178 }
179 
180 if (document.getElementById("enemiesDB")) {
181 	$.getScript(mw.util.getUrl("MediaWiki:Update_DB.js") + "?action=raw&ctype=text/javascript");
182 	console.log("DB Updater Loaded")
183 }
184 function hitChance(accuracy, defence) {
185     accuracy = parseInt(accuracy);
186     defence = parseInt(defence);
187     let hitChance = 0;
188     if(accuracy >= defence) {
189         hitChance = 1;
190     } else {
191         hitChance = 1 / (1 + defence - accuracy)
192     }
193     return (hitChance * 100).toFixed(2);
194 }
195 if (document.querySelector('.hitChance')) {
196     document.querySelectorAll(".hitChance").forEach(el => {
197         let [acc, def] = el.innerText.split(",")
198         el.innerText = "";
199         const accSpan = document.createElement("span");
200         const defSpan = document.createElement("span");
201         const hitSpan = document.createElement("span");
202         const accInput = document.createElement("input");
203         const defInput = document.createElement("input");
204         const hitValueSpan = document.createElement("span");
205 
206         accSpan.innerText = "Accuracy:";
207         defSpan.innerText = "Defence:";
208         hitSpan.innerText = "Hit Chance:";
209 
210         accInput.type = "number";
211         defInput.type = "number";
212         accInput.min = 0;
213         defInput.min = 0;
214         if(acc !== "0") {
215             accInput.disabled = true;
216         }
217         if(def !== "0") {
218             defInput.disabled = true
219         }
220         def = def < 0 ? 0 : def;
221         acc = acc < 0 ? 0 : acc;
222         accInput.defaultValue = acc;
223         defInput.defaultValue = def;
224         accInput.onchange = function(){
225             hitValueSpan.innerText = hitChance(accInput.value, defInput.value) + "%";
226         }
227         defInput.onchange = function(){
228             hitValueSpan.innerText = hitChance(accInput.value, defInput.value) + "%";
229         }
230         el.append(accSpan, accInput, defSpan, defInput, hitSpan, hitValueSpan);
231         hitValueSpan.innerText = hitChance(accInput.value, defInput.value) + "%";
232     })
233 }
234 
235 });