MediaWiki:Common.js: Diferență între versiuni

m
m
Linia 19: Linia 19:
 
(function () {
 
(function () {
 
     mw.hook('wikipage.content').add(function() {
 
     mw.hook('wikipage.content').add(function() {
         function showLoot(monster) {
+
        var selectedMonster = null;
 +
        var selectedCategory = 'Weapondrops';
 +
 
 +
         function showLoot(monster, category) {
 
             var lootList = document.getElementById('loot-list');
 
             var lootList = document.getElementById('loot-list');
 
             if (!lootList) return;  
 
             if (!lootList) return;  
Linia 59: Linia 62:
 
                         });
 
                         });
  
 +
                        var itemsToDisplay = drops[category] || [];
  
                         var selectedCategory = document.querySelector('#category-list li.active');
+
                         itemsToDisplay.forEach(function(item) {
                        if (selectedCategory) {
+
                            var li = document.createElement('li');
                             var categoryType = selectedCategory.getAttribute('data-category');
+
                            li.classList.add('loot-list-item');
                             var itemsToDisplay = drops[categoryType] || [];
+
                             var link = document.createElement('a');
 +
                            link.textContent = item;
 +
                            link.href = 'https://ro-wiki.metin2.gameforge.com/index.php/' + encodeURIComponent(item);
 +
                             link.target = '_blank';
 +
                            link.style.color = 'white';  
  
                             itemsToDisplay.forEach(function(item) {
+
                             li.appendChild(link);
                                var li = document.createElement('li');
+
                            lootList.appendChild(li);
                                li.classList.add('loot-list-item');
+
                        });
                                var link = document.createElement('a');
 
                                link.textContent = item;
 
                                link.href = 'https://ro-wiki.metin2.gameforge.com/index.php/' + encodeURIComponent(item);
 
                                link.target = '_blank';
 
                                link.style.color = 'white';
 
 
 
                                li.appendChild(link);
 
                                lootList.appendChild(li);
 
                            });
 
  
                            if (lootList.children.length === 0) {
+
                        if (lootList.children.length === 0) {
                                var li = document.createElement('li');
+
                            var li = document.createElement('li');
                                li.textContent = 'No items found';
+
                            li.textContent = 'No items found';
                                li.classList.add('loot-list-item');
+
                            li.classList.add('loot-list-item');
                                lootList.appendChild(li);
+
                            lootList.appendChild(li);
                            }
 
 
                         }
 
                         }
 
                     } else {
 
                     } else {
Linia 95: Linia 93:
 
         monsterItems.forEach(function(item) {
 
         monsterItems.forEach(function(item) {
 
             item.addEventListener('click', function() {
 
             item.addEventListener('click', function() {
                 var monster = item.getAttribute('data-monster');
+
                 selectedMonster = item.getAttribute('data-monster');
                 showLoot(monster);
+
                 showLoot(selectedMonster, selectedCategory);
 
             });
 
             });
 
         });
 
         });
 +
  
 
         var categoryItems = document.querySelectorAll('#category-list li');
 
         var categoryItems = document.querySelectorAll('#category-list li');
 
         categoryItems.forEach(function(item) {
 
         categoryItems.forEach(function(item) {
 
             item.addEventListener('click', function() {
 
             item.addEventListener('click', function() {
 +
 
                 categoryItems.forEach(function(catItem) {
 
                 categoryItems.forEach(function(catItem) {
 
                     catItem.classList.remove('active');
 
                     catItem.classList.remove('active');
 
                 });
 
                 });
 +
  
 
                 item.classList.add('active');
 
                 item.classList.add('active');
 
                  
 
                  
                 var selectedMonster = document.querySelector('#monster-list li.active');
+
 
 +
                 selectedCategory = item.getAttribute('data-category');
 +
               
 +
 
 
                 if (selectedMonster) {
 
                 if (selectedMonster) {
                    var monsterName = selectedMonster.getAttribute('data-monster');
+
                     showLoot(selectedMonster, selectedCategory);
                     showLoot(monsterName);
 
 
                 }
 
                 }
 
             });
 
             });
 
         });
 
         });
 +
 +
        if (monsterItems.length > 0) {
 +
            selectedMonster = monsterItems[0].getAttribute('data-monster');
 +
            showLoot(selectedMonster, selectedCategory);
 +
        }
 
     });
 
     });
 
})();
 
})();

Versiunea de la data 22 august 2024 12:35

(function () {
    // Load GDPR Cookie Script
    var req = new XMLHttpRequest();
    req.addEventListener('load', function (ev) {
        if (this.status >= 200 && this.status < 300) {
            var data = JSON.parse(this.responseText);
            if (data.hasOwnProperty('version')) {
                var gdpr = document.createElement("script");
                gdpr.src = "https://s3-static.geo.gfsrv.net/cookiebanner/" + data.version + "/cookie.min.js";
                document.head.appendChild(gdpr);
            }
        }
    });
    req.open('GET', "https://s3-static.geo.gfsrv.net/cookiebanner/version.json");
    req.send();
})();


(function () {
    mw.hook('wikipage.content').add(function() {
        var selectedMonster = null;
        var selectedCategory = 'Weapondrops'; 

        function showLoot(monster, category) {
            var lootList = document.getElementById('loot-list');
            if (!lootList) return; 
            lootList.innerHTML = '';

            var apiUrl = 'https://ro-wiki.metin2.gameforge.com/api.php?action=parse&page=' + encodeURIComponent(monster) + '&prop=wikitext&format=json&origin=*';

            fetch(apiUrl)
                .then(function(response) {
                    if (!response.ok) {
                        throw new Error('Network response was not ok: ' + response.statusText);
                    }
                    return response.json();
                })
                .then(function(data) {
                    if (data.parse && data.parse.wikitext) {
                        var wikitext = data.parse.wikitext['*'];
                        var drops = {
                            Weapondrops: [],
                            Armordrops: [],
                            Otherdrops: []
                        };

                        ['Weapondrops', 'Armordrops', 'Otherdrops'].forEach(function(dropType) {
                            var match = wikitext.match(new RegExp('\\|' + dropType + '\\s*=\\s*([\\s\\S]*?)(?=\\n\\|)', 'i'));
                            if (match) {
                                var items = match[1].trim().split(/\n\s*::\s*/);
                                drops[dropType] = items.map(function(item) {
                                    var textMatch = item.match(/\{\{Ti\|[^}]+\}\}/);
                                    if (textMatch) {
                                        var linkTextMatch = textMatch[0].match(/\|\s*([^|}]+)\s*\}\}/);
                                        if (linkTextMatch) {
                                            return linkTextMatch[1].trim();
                                        }
                                    }
                                    return null;
                                }).filter(Boolean);
                            }
                        });

                        var itemsToDisplay = drops[category] || [];

                        itemsToDisplay.forEach(function(item) {
                            var li = document.createElement('li');
                            li.classList.add('loot-list-item');
                            var link = document.createElement('a');
                            link.textContent = item;
                            link.href = 'https://ro-wiki.metin2.gameforge.com/index.php/' + encodeURIComponent(item);
                            link.target = '_blank'; 
                            link.style.color = 'white'; 

                            li.appendChild(link);
                            lootList.appendChild(li);
                        });

                        if (lootList.children.length === 0) {
                            var li = document.createElement('li');
                            li.textContent = 'No items found';
                            li.classList.add('loot-list-item');
                            lootList.appendChild(li);
                        }
                    } else {
                        console.error('Unexpected data format:', data);
                    }
                })
        }


        var monsterItems = document.querySelectorAll('#monster-list li');
        monsterItems.forEach(function(item) {
            item.addEventListener('click', function() {
                selectedMonster = item.getAttribute('data-monster');
                showLoot(selectedMonster, selectedCategory);
            });
        });


        var categoryItems = document.querySelectorAll('#category-list li');
        categoryItems.forEach(function(item) {
            item.addEventListener('click', function() {

                categoryItems.forEach(function(catItem) {
                    catItem.classList.remove('active');
                });


                item.classList.add('active');
                

                selectedCategory = item.getAttribute('data-category');
                

                if (selectedMonster) {
                    showLoot(selectedMonster, selectedCategory);
                }
            });
        });

        if (monsterItems.length > 0) {
            selectedMonster = monsterItems[0].getAttribute('data-monster');
            showLoot(selectedMonster, selectedCategory);
        }
    });
})();