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

m
m
Linia 148: Linia 148:
 
             container.classList.add('overlay-container');
 
             container.classList.add('overlay-container');
  
            // Ensure the container is positioned relatively for the overlay points
 
            container.style.position = 'relative';
 
            container.style.display = 'inline-block';
 
           
 
            // Get the image's natural dimensions for scaling the circles accordingly
 
            const imgWidth = img.naturalWidth;
 
            const imgHeight = img.naturalHeight;
 
 
            // Wrap the image inside the overlay container
 
 
             img.parentNode.insertBefore(container, img);
 
             img.parentNode.insertBefore(container, img);
 
             container.appendChild(img);
 
             container.appendChild(img);
  
             // Parse the coordinates from the class or alt attribute
+
             const imgWidth = img.width;
             const dataAttributes = img.getAttribute('alt').match(/\[(.*?)\]/g);
+
            const imgHeight = img.height;
              
+
 
             if (dataAttributes) {
+
             const altText = img.getAttribute('alt');
                dataAttributes.forEach(data => {
+
            const regex = /\[(\d+),(\d+),(\d+)\|([^\]]+)\]/g;
                    const [coords, link] = data.slice(1, -1).split('|'); // Strip brackets and split
+
             let match;
                    const [x, y, radius] = coords.split(',').map(Number);
+
 
 +
             while ((match = regex.exec(altText)) !== null) {
 +
                const x = parseInt(match[1], 10);
 +
                const y = parseInt(match[2], 10);
 +
                const radius = parseInt(match[3], 10);
 +
                const link = match[4];
  
                    if (coords && link) {
+
                const relativeX = (x / imgWidth) * 100;
                        // Calculate percentage positions relative to the image size
+
                const relativeY = (y / imgHeight) * 100;
                        const relativeX = (x / imgWidth) * 100;
+
                const relativeRadius = (radius / imgWidth) * 100;
                        const relativeY = (y / imgHeight) * 100;
 
                        const relativeRadius = (radius / imgWidth) * 100;
 
  
                        // Create the circle for overlay
+
                const overlayPoint = document.createElement('div');
                        const overlayPoint = document.createElement('div');
+
                overlayPoint.classList.add('overlay-point');
                        overlayPoint.classList.add('overlay-point');
+
                overlayPoint.style.top = relativeY + '%';
                        overlayPoint.style.top = relativeY + '%';
+
                overlayPoint.style.left = relativeX + '%';
                        overlayPoint.style.left = relativeX + '%';
+
                overlayPoint.style.width = relativeRadius * 2 + '%';
                        overlayPoint.style.width = relativeRadius * 2 + '%';
+
                overlayPoint.style.height = relativeRadius * 2 + '%';
                        overlayPoint.style.height = relativeRadius * 2 + '%';
 
  
                        // Create the clickable link
+
                const anchor = document.createElement('a');
                        const anchor = document.createElement('a');
+
                anchor.href = `/index.php/${link}`;
                        anchor.href = `/index.php/${link}`;
+
                anchor.title = link;
                        anchor.title = link;
+
                anchor.textContent = link;
                        anchor.textContent = link;
 
  
                        // Append the anchor to the overlay point
+
                overlayPoint.appendChild(anchor);
                        overlayPoint.appendChild(anchor);
 
  
                        // Append the overlay point to the container
+
                container.appendChild(overlayPoint);
                        container.appendChild(overlayPoint);
 
                    }
 
                });
 
 
             }
 
             }
 
         });
 
         });
 
     }
 
     }
  
    // Initialize the overlay function
 
 
     addOverlayFromImage();
 
     addOverlayFromImage();
 
});
 
});

Versiunea de la data 26 septembrie 2024 19: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 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('selected');
                if (item.getAttribute('data-monster') === selectedMonster) {
                    item.classList.add('selected');
                }
            });

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

        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(); 
            });
        });


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

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


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

document.addEventListener("DOMContentLoaded", function() {
    function addOverlayFromImage() {
        const images = document.querySelectorAll('img.base-image');

        images.forEach(img => {
            const container = document.createElement('div');
            container.classList.add('overlay-container');

            img.parentNode.insertBefore(container, img);
            container.appendChild(img);

            const imgWidth = img.width;
            const imgHeight = img.height;

            const altText = img.getAttribute('alt');
            const regex = /\[(\d+),(\d+),(\d+)\|([^\]]+)\]/g;
            let match;

            while ((match = regex.exec(altText)) !== null) {
                const x = parseInt(match[1], 10);
                const y = parseInt(match[2], 10);
                const radius = parseInt(match[3], 10);
                const link = match[4];

                const relativeX = (x / imgWidth) * 100;
                const relativeY = (y / imgHeight) * 100;
                const relativeRadius = (radius / imgWidth) * 100;

                const overlayPoint = document.createElement('div');
                overlayPoint.classList.add('overlay-point');
                overlayPoint.style.top = relativeY + '%';
                overlayPoint.style.left = relativeX + '%';
                overlayPoint.style.width = relativeRadius * 2 + '%';
                overlayPoint.style.height = relativeRadius * 2 + '%';

                const anchor = document.createElement('a');
                anchor.href = `/index.php/${link}`;
                anchor.title = link;
                anchor.textContent = link;

                overlayPoint.appendChild(anchor);

                container.appendChild(overlayPoint);
            }
        });
    }

    addOverlayFromImage();
});