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

m
m
Linia 115: Linia 115:
 
                 showLoot(selectedMonster, selectedCategory);
 
                 showLoot(selectedMonster, selectedCategory);
 
                 updateHighlighting();
 
                 updateHighlighting();
            });
 
        });
 
  
        var categoryItems = document.querySelectorAll('#category-list li');
 
        categoryItems.forEach(function(item) {
 
            item.addEventListener('click', function() {
 
                selectedCategory = item.getAttribute('data-category');
 
  
                 categoryItems.forEach(function(catItem) {
+
                 const existingCategoryColumn = document.querySelector('#category-column');
                    catItem.classList.remove('highlighted');
+
                const existingLootColumn = document.querySelector('#loot-column');
                 });
+
                if (existingCategoryColumn) existingCategoryColumn.remove();
 +
                 if (existingLootColumn) existingLootColumn.remove();
  
                item.classList.add('highlighted');
 
               
 
                if (selectedMonster) {
 
                    showLoot(selectedMonster, selectedCategory);
 
                }
 
                updateHighlighting();
 
            });
 
        });
 
  
        if (monsterItems.length > 0) {
+
                const categoryColumn = document.createElement('div');
            selectedMonster = monsterItems[0].getAttribute('data-monster');
+
                categoryColumn.className = 'encyclopedia-column';
            showLoot(selectedMonster, selectedCategory);
+
                categoryColumn.id = 'category-column';
            updateHighlighting();
+
                categoryColumn.innerHTML = `
        }
+
                    <div class="encyclopedia-header">Categorii</div>
    });
+
                    <ul class="encyclopedia-list" id="category-list">
})();
+
                        <li data-category="Weapondrops">Arme</li>
 +
                        <li data-category="Armordrops">Armuri</li>
 +
                        <li data-category="Otherdrops">Drop General</li>
 +
                    </ul>
 +
                `;
 +
                document.querySelector('.encyclopedia-container').appendChild(categoryColumn);
  
  
document.addEventListener('DOMContentLoaded', function () {
+
                const categories = categoryColumn.querySelectorAll('li');
    const monsters = document.querySelectorAll('#monster-list li');
+
                categories.forEach(category => {
   
+
                    category.addEventListener('click', function () {
    monsters.forEach(monster => {
 
        monster.addEventListener('click', function () {
 
            // Remove previous selection
 
            document.querySelectorAll('#monster-list li').forEach(li => li.classList.remove('selected'));
 
            this.classList.add('selected');
 
  
            // Remove existing category and loot columns if they exist
+
                        categories.forEach(li => li.classList.remove('selected'));
            const existingCategoryColumn = document.querySelector('#category-column');
+
                        this.classList.add('selected');
            const existingLootColumn = document.querySelector('#loot-column');
 
            if (existingCategoryColumn) existingCategoryColumn.remove();
 
            if (existingLootColumn) existingLootColumn.remove();
 
  
            // Create new Category column
 
            const categoryColumn = document.createElement('div');
 
            categoryColumn.className = 'encyclopedia-column';
 
            categoryColumn.id = 'category-column';
 
            categoryColumn.innerHTML = `
 
                <div class="encyclopedia-header">Categorii</div>
 
                <ul class="encyclopedia-list" id="category-list">
 
                    <li data-category="Weapondrops">Arme</li>
 
                    <li data-category="Armordrops">Armuri</li>
 
                    <li data-category="Otherdrops">Drop General</li>
 
                </ul>
 
            `;
 
            document.querySelector('.encyclopedia-container').appendChild(categoryColumn);
 
  
            // Add event listeners to the new categories
+
                        const existingLootColumn = document.querySelector('#loot-column');
            const categories = categoryColumn.querySelectorAll('li');
+
                        if (existingLootColumn) existingLootColumn.remove();
            categories.forEach(category => {
 
                category.addEventListener('click', function () {
 
                    // Remove previous selection
 
                    categories.forEach(li => li.classList.remove('selected'));
 
                    this.classList.add('selected');
 
  
                    // Remove existing loot column if it exists
+
                        const lootColumn = document.createElement('div');
                    const existingLootColumn = document.querySelector('#loot-column');
+
                        lootColumn.className = 'encyclopedia-column';
                    if (existingLootColumn) existingLootColumn.remove();
+
                        lootColumn.id = 'loot-column';
 +
                        lootColumn.innerHTML = `
 +
                            <div class="encyclopedia-header">Prază</div>
 +
                            <ul class="encyclopedia-list" id="loot-list">
 +
                                <!-- Loot items will be populated here -->
 +
                            </ul>
 +
                        `;
 +
                        document.querySelector('.encyclopedia-container').appendChild(lootColumn);
  
                    // Create new Loot column
+
                        if (selectedMonster) {
                    const lootColumn = document.createElement('div');
+
                            selectedCategory = category.getAttribute('data-category');
                    lootColumn.className = 'encyclopedia-column';
+
                             showLoot(selectedMonster, selectedCategory);
                    lootColumn.id = 'loot-column';
+
                             updateHighlighting();
                    lootColumn.innerHTML = `
+
                         }
                        <div class="encyclopedia-header">Prază</div>
+
                     });
                        <ul class="encyclopedia-list" id="loot-list">
 
                             <li>Item 1</li>
 
                             <li>Item 2</li>
 
                            <li>Item 3</li>
 
                         </ul>
 
                    `;
 
                     document.querySelector('.encyclopedia-container').appendChild(lootColumn);
 
 
                 });
 
                 });
 
             });
 
             });
 
         });
 
         });
 +
 +
        if (monsterItems.length > 0) {
 +
            selectedMonster = monsterItems[0].getAttribute('data-monster');
 +
            showLoot(selectedMonster, selectedCategory);
 +
            updateHighlighting();
 +
        }
 
     });
 
     });
});
+
})();

Versiunea de la data 22 august 2024 19:01

(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 regex = new RegExp('\\|' + dropType + '\\s*=([\\s\\S]*?)(?=(\\n\\|\\w+\\s*=|$))', 'i');
                            var match = wikitext.match(regex);
                            if (match && match[1].trim()) {
                                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 = 'Nu am gasit nimic pentru aceasta categorie.';
                            li.classList.add('loot-list-item');
                            lootList.appendChild(li);
                        }
                    } else {
                        console.error('Unexpected data format:', data);
                    }
                })
        }

        function updateHighlighting() {
            var monsterItems = document.querySelectorAll('#monster-list li');
            monsterItems.forEach(function(item) {
                item.classList.remove('highlighted');
                if (item.getAttribute('data-monster') === selectedMonster) {
                    item.classList.add('highlighted');
                }
            });

            var categoryItems = document.querySelectorAll('#category-list li');
            categoryItems.forEach(function(item) {
                item.classList.remove('highlighted');
                if (item.getAttribute('data-category') === selectedCategory) {
                    item.classList.add('highlighted');
                }
            });
        }

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


                const existingCategoryColumn = document.querySelector('#category-column');
                const existingLootColumn = document.querySelector('#loot-column');
                if (existingCategoryColumn) existingCategoryColumn.remove();
                if (existingLootColumn) existingLootColumn.remove();


                const categoryColumn = document.createElement('div');
                categoryColumn.className = 'encyclopedia-column';
                categoryColumn.id = 'category-column';
                categoryColumn.innerHTML = `
                    <div class="encyclopedia-header">Categorii</div>
                    <ul class="encyclopedia-list" id="category-list">
                        <li data-category="Weapondrops">Arme</li>
                        <li data-category="Armordrops">Armuri</li>
                        <li data-category="Otherdrops">Drop General</li>
                    </ul>
                `;
                document.querySelector('.encyclopedia-container').appendChild(categoryColumn);


                const categories = categoryColumn.querySelectorAll('li');
                categories.forEach(category => {
                    category.addEventListener('click', function () {

                        categories.forEach(li => li.classList.remove('selected'));
                        this.classList.add('selected');


                        const existingLootColumn = document.querySelector('#loot-column');
                        if (existingLootColumn) existingLootColumn.remove();

                        const lootColumn = document.createElement('div');
                        lootColumn.className = 'encyclopedia-column';
                        lootColumn.id = 'loot-column';
                        lootColumn.innerHTML = `
                            <div class="encyclopedia-header">Prază</div>
                            <ul class="encyclopedia-list" id="loot-list">
                                <!-- Loot items will be populated here -->
                            </ul>
                        `;
                        document.querySelector('.encyclopedia-container').appendChild(lootColumn);

                        if (selectedMonster) {
                            selectedCategory = category.getAttribute('data-category');
                            showLoot(selectedMonster, selectedCategory);
                            updateHighlighting();
                        }
                    });
                });
            });
        });

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