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 312: Line 312:
         }
         }


        var api = '/index.php?action=api&format=json';
        // Use parse API to get rendered content
         var url = '/api.php?action=parse&page=' + encodeURIComponent(title) + '&prop=text&format=json&redirects=1';
         var url = '/api.php?action=parse&page=' + encodeURIComponent(title) + '&prop=text&format=json&redirects=1';


Line 328: Line 326:
                     temp.innerHTML = html;
                     temp.innerHTML = html;


                    // Get first image
                     var img = null;
                     var img = null;
                     var infoboxImg = temp.querySelector('.flatmmo-infobox-image img, .flatmmo-infobox img');
                     var infoboxImg = temp.querySelector('.flatmmo-infobox-image img, .flatmmo-infobox img');
Line 340: Line 337:
                     }
                     }


                    // Get first paragraph text
                     var text = '';
                     var text = '';
                     var paragraphs = temp.querySelectorAll('p');
                     var paragraphs = temp.querySelectorAll('p');
Line 351: Line 347:
                     }
                     }


                    // Truncate if too long
                     if (text.length > 250) {
                     if (text.length > 250) {
                         text = text.substring(0, 247) + '...';
                         text = text.substring(0, 247) + '...';
Line 360: Line 355:
                     callback(result);
                     callback(result);
                 }
                 }
             } catch(e) {}
             } catch(e) {
                console.log('Preview fetch error:', e);
            }
        };
        xhr.onerror = function() {
            console.log('Preview request failed for:', title);
         };
         };
         xhr.send();
         xhr.send();
Line 371: Line 371:
         if (!href) return;
         if (!href) return;


        // Only handle internal wiki links
         var match = href.match(/\/index\.php\/(.+)/);
         var match = href.match(/\/index\.php\/(.+)/);
         if (!match) return;
         if (!match) return;
Line 377: Line 376:
         var title = decodeURIComponent(match[1]).replace(/_/g, ' ');
         var title = decodeURIComponent(match[1]).replace(/_/g, ' ');


        // Skip special pages, edit links, red links
         if (title.match(/^Special:|^Talk:|^User:|^Category:|^File:|^Template:|^MediaWiki:/i)) return;
         if (title.match(/^Special:|^Talk:|^User:|^Category:|^File:|^Template:|^MediaWiki:/i)) return;
         if (href.indexOf('action=edit') !== -1) return;
         if (href.indexOf('action=edit') !== -1) return;
Line 401: Line 399:
     }
     }


    // Attach to content area links
     document.addEventListener('DOMContentLoaded', function() {
     document.addEventListener('DOMContentLoaded', function() {
         var content = document.getElementById('mw-content-text') || document.getElementById('bodyContent') || document.body;
         var content = document.getElementById('mw-content-text') || document.getElementById('bodyContent') || document.body;
Line 413: Line 410:
             clearTimeout(showTimer);
             clearTimeout(showTimer);


// Remove default browser tooltip
             if (link.getAttribute('title')) {
             if (link.getAttribute('title')) {
                 link.setAttribute('data-title', link.getAttribute('title'));
                 link.setAttribute('data-title', link.getAttribute('title'));
Line 421: Line 417:
             showTimer = setTimeout(function() {
             showTimer = setTimeout(function() {
                 showPreview(link, e);
                 showPreview(link, e);
             }, 300); });
             }, 300);
        });


         content.addEventListener('mouseout', function(e) {
         content.addEventListener('mouseout', function(e) {
Line 427: Line 424:
             if (!link) return;
             if (!link) return;


clearTimeout(showTimer);
            clearTimeout(showTimer);
            // Restore default title
 
             if (link.getAttribute('data-title')) {
             if (link.getAttribute('data-title')) {
                 link.setAttribute('title', link.getAttribute('data-title'));
                 link.setAttribute('title', link.getAttribute('data-title'));
                 link.removeAttribute('data-title');
                 link.removeAttribute('data-title');
             }
             }
hideTimer = setTimeout(function() {
 
            hideTimer = setTimeout(function() {
                 if (popup) popup.style.display = 'none';
                 if (popup) popup.style.display = 'none';
             }, 200);
             }, 200);

Revision as of 04:54, 18 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     // Scroll to top in header mode (banner removal shifts content)
 77     if (searchMode === 'header') {
 78         window.scrollTo(0, 0);
 79         document.addEventListener('DOMContentLoaded', function() {
 80             window.scrollTo(0, 0);
 81         });
 82     }
 83 
 84     // Create toggle button
 85     var searchToggle = document.createElement('button');
 86     searchToggle.className = 'search-toggle-btn';
 87     searchToggle.title = 'Toggle search bar position';
 88     searchToggle.textContent = searchMode === 'top' ? '🔍' : '🔎';
 89 
 90     searchToggle.addEventListener('click', function() {
 91         var isTop = document.body.classList.contains('search-top');
 92         var newMode = isTop ? 'header' : 'top';
 93 
 94         document.body.classList.remove('search-top', 'search-header');
 95         document.body.classList.add('search-' + newMode);
 96         document.documentElement.classList.remove('search-top', 'search-header');
 97         document.documentElement.classList.add('search-' + newMode);
 98 
 99         localStorage.setItem('flatmmo-search', newMode);
100         searchToggle.textContent = newMode === 'top' ? '🔍' : '🔎';
101     });
102 
103     // Create compact header search bar
104     var searchWrap = document.createElement('div');
105     searchWrap.className = 'header-search-wrap';
106 
107     var searchInput = document.createElement('input');
108     searchInput.type = 'text';
109     searchInput.className = 'header-search-input';
110     searchInput.placeholder = 'Search wiki...';
111     searchInput.setAttribute('autocomplete', 'off');
112 
113     var searchBtn = document.createElement('button');
114     searchBtn.className = 'header-search-btn';
115     searchBtn.innerHTML = '🔍';
116     searchBtn.title = 'Search';
117 
118     function doSearch() {
119         var q = searchInput.value.trim();
120         if (q) {
121             window.location.href = mw.util.getUrl('Special:Search') + '?search=' + encodeURIComponent(q);
122         }
123     }
124 
125     searchBtn.addEventListener('click', doSearch);
126     searchInput.addEventListener('keydown', function(e) {
127         if (e.key === 'Enter') doSearch();
128     });
129 
130     searchWrap.appendChild(searchInput);
131     searchWrap.appendChild(searchBtn);
132 
133     // Insert toggle button inline with other header buttons
134     var target = document.querySelector('.cosmos-header__wiki-buttons')
135               || document.querySelector('.cosmos-actions')
136               || document.querySelector('.wds-button-group')
137               || document.querySelector('#content');
138     if (target && target.id !== 'content') {
139         target.appendChild(searchToggle);
140         // Make target the positioning anchor, append search bar inside
141         target.style.position = 'relative';
142         target.appendChild(searchWrap);
143 
144         // Relocate username from banner into header for header mode
145         var bannerUser = document.querySelector('.cosmos-userButton-label')
146                       || document.querySelector('#p-personal-label');
147         var userName = bannerUser ? bannerUser.textContent.trim() : '';
148         if (userName) {
149             var userLink = document.createElement('a');
150             userLink.className = 'relocated-user';
151             userLink.href = mw.util.getUrl('User:' + userName);
152             userLink.title = userName;
153 
154             var userIcon = document.createElement('span');
155             userIcon.className = 'relocated-user-icon';
156             userIcon.innerHTML = '👤';
157 
158             var userText = document.createElement('span');
159             userText.className = 'relocated-user-name';
160             userText.textContent = userName;
161 
162             userLink.appendChild(userIcon);
163             userLink.appendChild(userText);
164             target.appendChild(userLink);
165         }
166     }
167 })();
168 
169 $.getScript(mw.util.getUrl("MediaWiki:ProfileTags.js") + "?action=raw&ctype=text/javascript");
170 	if (document.getElementById("interactiveMap")) {
171 console.log("map loaded")
172 		$.getScript(mw.util.getUrl("MediaWiki:InteractiveMap/Objects.js") + "?action=raw&ctype=text/javascript", function() {
173             $.getScript(mw.util.getUrl("MediaWiki:InteractiveMap/NPCs.js") + "?action=raw&ctype=text/javascript", function() {
174                 $.getScript(mw.util.getUrl("MediaWiki:InteractiveMap/Maps.js") + "?action=raw&ctype=text/javascript", function() {
175                     $.getScript(mw.util.getUrl("MediaWiki:InteractiveMap.js") + "?action=raw&ctype=text/javascript", function() {
176                     });
177                 });
178             });
179         });
180 	}
181 
182 	if (document.getElementById("wardrobe")) {
183 		$.getScript(mw.util.getUrl("MediaWiki:Wardrobe.js") + "?action=raw&ctype=text/javascript");
184 		console.log("wardrobe loaded")
185 	}
186 
187     if (document.getElementById("mapEditor")) {
188         $.getScript("https://cdn.jsdelivr.net/npm/interactjs/dist/interact.min.js", function() {
189             $.getScript(mw.util.getUrl("MediaWiki:InteractiveMap/Objects.js") + "?action=raw&ctype=text/javascript", function() {
190                 $.getScript(mw.util.getUrl("MediaWiki:InteractiveMap/NPCs.js") + "?action=raw&ctype=text/javascript", function() {
191                     $.getScript(mw.util.getUrl("MediaWiki:InteractiveMap/Maps.js") + "?action=raw&ctype=text/javascript", function() {
192                         $.getScript(mw.util.getUrl("MediaWiki:MapEditor.js") + "?action=raw&ctype=text/javascript", function() {
193                         });
194                     });
195                 });
196             });
197         });
198     }
199 if (document.querySelector(".code")) {
200 	$.getScript("../custom/highlight.min.js", ()=>{
201 		document.querySelectorAll(".code").forEach(el=>hljs.highlightElement(el))
202 		console.log('Code highlighted');
203 	});
204 }
205 
206 if (document.querySelector('[class*="Tracker"]') && !document.querySelector('[class*="page-MediaWiki"]')) {
207 	$.getScript(mw.util.getUrl("MediaWiki:Trackers.js") + "?action=raw&ctype=text/javascript");
208     console.log("Tracker loaded");
209 }
210 
211 if (document.getElementById("enemiesDB")) {
212 	$.getScript(mw.util.getUrl("MediaWiki:Update_DB.js") + "?action=raw&ctype=text/javascript");
213 	console.log("DB Updater Loaded")
214 }
215 function hitChance(accuracy, defence) {
216     accuracy = parseInt(accuracy);
217     defence = parseInt(defence);
218     let hitChance = 0;
219     if(accuracy >= defence) {
220         hitChance = 1;
221     } else {
222         hitChance = 1 / (1 + defence - accuracy)
223     }
224     return (hitChance * 100).toFixed(2);
225 }
226 if (document.querySelector('.hitChance')) {
227     document.querySelectorAll(".hitChance").forEach(el => {
228         let [acc, def] = el.innerText.split(",")
229         el.innerText = "";
230         const accSpan = document.createElement("span");
231         const defSpan = document.createElement("span");
232         const hitSpan = document.createElement("span");
233         const accInput = document.createElement("input");
234         const defInput = document.createElement("input");
235         const hitValueSpan = document.createElement("span");
236 
237         accSpan.innerText = "Accuracy:";
238         defSpan.innerText = "Defence:";
239         hitSpan.innerText = "Hit Chance:";
240 
241         accInput.type = "number";
242         defInput.type = "number";
243         accInput.min = 0;
244         defInput.min = 0;
245         if(acc !== "0") {
246             accInput.disabled = true;
247         }
248         if(def !== "0") {
249             defInput.disabled = true
250         }
251         def = def < 0 ? 0 : def;
252         acc = acc < 0 ? 0 : acc;
253         accInput.defaultValue = acc;
254         defInput.defaultValue = def;
255         accInput.onchange = function(){
256             hitValueSpan.innerText = hitChance(accInput.value, defInput.value) + "%";
257         }
258         defInput.onchange = function(){
259             hitValueSpan.innerText = hitChance(accInput.value, defInput.value) + "%";
260         }
261         el.append(accSpan, accInput, defSpan, defInput, hitSpan, hitValueSpan);
262         hitValueSpan.innerText = hitChance(accInput.value, defInput.value) + "%";
263     })
264 }
265 
266 });
267 
268 /* Page Preview Popup */
269 (function() {
270     var popup = null;
271     var hideTimer = null;
272     var showTimer = null;
273     var cache = {};
274     var currentRequest = null;
275 
276     function createPopup() {
277         popup = document.createElement('div');
278         popup.className = 'flatmmo-preview';
279         document.body.appendChild(popup);
280 
281         popup.addEventListener('mouseenter', function() {
282             clearTimeout(hideTimer);
283         });
284         popup.addEventListener('mouseleave', function() {
285             hideTimer = setTimeout(function() {
286                 popup.style.display = 'none';
287             }, 200);
288         });
289     }
290 
291     function positionPopup(e) {
292         var x = e.pageX + 15;
293         var y = e.pageY + 15;
294         var popupWidth = 340;
295         var popupHeight = 200;
296 
297         if (x + popupWidth > document.documentElement.scrollLeft + window.innerWidth) {
298             x = e.pageX - popupWidth - 15;
299         }
300         if (y + popupHeight > document.documentElement.scrollTop + window.innerHeight) {
301             y = e.pageY - popupHeight - 15;
302         }
303 
304         popup.style.left = x + 'px';
305         popup.style.top = y + 'px';
306     }
307 
308     function fetchPreview(title, callback) {
309         if (cache[title]) {
310             callback(cache[title]);
311             return;
312         }
313 
314         var url = '/api.php?action=parse&page=' + encodeURIComponent(title) + '&prop=text&format=json&redirects=1';
315 
316         var xhr = new XMLHttpRequest();
317         currentRequest = xhr;
318         xhr.open('GET', url);
319         xhr.onload = function() {
320             if (xhr !== currentRequest) return;
321             try {
322                 var data = JSON.parse(xhr.responseText);
323                 if (data.parse && data.parse.text) {
324                     var html = data.parse.text['*'];
325                     var temp = document.createElement('div');
326                     temp.innerHTML = html;
327 
328                     var img = null;
329                     var infoboxImg = temp.querySelector('.flatmmo-infobox-image img, .flatmmo-infobox img');
330                     if (infoboxImg) {
331                         img = infoboxImg.src;
332                     } else {
333                         var firstImg = temp.querySelector('img');
334                         if (firstImg && firstImg.width > 20) {
335                             img = firstImg.src;
336                         }
337                     }
338 
339                     var text = '';
340                     var paragraphs = temp.querySelectorAll('p');
341                     for (var i = 0; i < paragraphs.length; i++) {
342                         var t = paragraphs[i].textContent.trim();
343                         if (t.length > 10) {
344                             text = t;
345                             break;
346                         }
347                     }
348 
349                     if (text.length > 250) {
350                         text = text.substring(0, 247) + '...';
351                     }
352 
353                     var result = { image: img, text: text, title: title };
354                     cache[title] = result;
355                     callback(result);
356                 }
357             } catch(e) {
358                 console.log('Preview fetch error:', e);
359             }
360         };
361         xhr.onerror = function() {
362             console.log('Preview request failed for:', title);
363         };
364         xhr.send();
365     }
366 
367     function showPreview(link, e) {
368         if (!popup) createPopup();
369 
370         var href = link.getAttribute('href');
371         if (!href) return;
372 
373         var match = href.match(/\/index\.php\/(.+)/);
374         if (!match) return;
375 
376         var title = decodeURIComponent(match[1]).replace(/_/g, ' ');
377 
378         if (title.match(/^Special:|^Talk:|^User:|^Category:|^File:|^Template:|^MediaWiki:/i)) return;
379         if (href.indexOf('action=edit') !== -1) return;
380         if (link.classList.contains('new')) return;
381 
382         positionPopup(e);
383         popup.innerHTML = '<div class="flatmmo-preview-title">' + title + '</div><div class="flatmmo-preview-loading">Loading...</div>';
384         popup.style.display = 'block';
385 
386         fetchPreview(title, function(data) {
387             var html = '<div class="flatmmo-preview-title">' + data.title + '</div>';
388             if (data.image) {
389                 html += '<div class="flatmmo-preview-image"><img src="' + data.image + '"></div>';
390             }
391             if (data.text) {
392                 html += '<div class="flatmmo-preview-text">' + data.text + '</div>';
393             }
394             if (!data.image && !data.text) {
395                 html += '<div class="flatmmo-preview-text" style="color:#999;">No preview available.</div>';
396             }
397             popup.innerHTML = html;
398         });
399     }
400 
401     document.addEventListener('DOMContentLoaded', function() {
402         var content = document.getElementById('mw-content-text') || document.getElementById('bodyContent') || document.body;
403 
404         content.addEventListener('mouseover', function(e) {
405             var link = e.target.closest('a');
406             if (!link) return;
407             if (link.closest('.flatmmo-preview')) return;
408 
409             clearTimeout(hideTimer);
410             clearTimeout(showTimer);
411 
412             if (link.getAttribute('title')) {
413                 link.setAttribute('data-title', link.getAttribute('title'));
414                 link.removeAttribute('title');
415             }
416 
417             showTimer = setTimeout(function() {
418                 showPreview(link, e);
419             }, 300);
420         });
421 
422         content.addEventListener('mouseout', function(e) {
423             var link = e.target.closest('a');
424             if (!link) return;
425 
426             clearTimeout(showTimer);
427 
428             if (link.getAttribute('data-title')) {
429                 link.setAttribute('title', link.getAttribute('data-title'));
430                 link.removeAttribute('data-title');
431             }
432 
433             hideTimer = setTimeout(function() {
434                 if (popup) popup.style.display = 'none';
435             }, 200);
436         });
437     });
438 })();