MediaWiki:Common.js: Diferență între versiuni
m |
m |
||
Linia 92: | Linia 92: | ||
function updateHighlighting() { | function updateHighlighting() { | ||
var monsterItems = document.querySelectorAll('#monster-list li'); | var monsterItems = document.querySelectorAll('#monster-list li'); | ||
+ | if (!monsterItems.length) return; | ||
+ | |||
monsterItems.forEach(function(item) { | monsterItems.forEach(function(item) { | ||
item.classList.remove('highlighted'); | item.classList.remove('highlighted'); | ||
Linia 109: | Linia 111: | ||
function filterMonsters() { | function filterMonsters() { | ||
− | var searchText = document.getElementById('monster-search').value.toLowerCase(); | + | var searchText = document.getElementById('monster-search'); |
+ | if (!searchText) return; | ||
+ | |||
+ | var searchValue = searchText.value.toLowerCase(); | ||
var monsterItems = document.querySelectorAll('#monster-list li'); | var monsterItems = document.querySelectorAll('#monster-list li'); | ||
+ | |||
+ | if (!monsterItems.length) return; | ||
var firstVisibleItem = null; | var firstVisibleItem = null; | ||
− | + | monsterItems.forEach(function(item) { | |
− | |||
var monsterName = item.textContent.toLowerCase(); | var monsterName = item.textContent.toLowerCase(); | ||
− | if (monsterName.includes( | + | if (monsterName.includes(searchValue)) { |
item.style.display = ''; | item.style.display = ''; | ||
if (!firstVisibleItem) { | if (!firstVisibleItem) { | ||
Linia 124: | Linia 130: | ||
item.style.display = 'none'; | item.style.display = 'none'; | ||
} | } | ||
− | } | + | }); |
if (firstVisibleItem) { | if (firstVisibleItem) { | ||
Linia 133: | Linia 139: | ||
} | } | ||
− | document.getElementById('monster-search').addEventListener('input', filterMonsters); | + | var monsterSearch = document.getElementById('monster-search'); |
+ | if (monsterSearch) { | ||
+ | monsterSearch.addEventListener('input', filterMonsters); | ||
+ | } | ||
var monsterItems = document.querySelectorAll('#monster-list li'); | var monsterItems = document.querySelectorAll('#monster-list li'); |
Versiunea de la data 22 august 2024 14:29
(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');
if (!monsterItems.length) return;
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');
}
});
}
function filterMonsters() {
var searchText = document.getElementById('monster-search');
if (!searchText) return;
var searchValue = searchText.value.toLowerCase();
var monsterItems = document.querySelectorAll('#monster-list li');
if (!monsterItems.length) return;
var firstVisibleItem = null;
monsterItems.forEach(function(item) {
var monsterName = item.textContent.toLowerCase();
if (monsterName.includes(searchValue)) {
item.style.display = '';
if (!firstVisibleItem) {
firstVisibleItem = item;
}
} else {
item.style.display = 'none';
}
});
if (firstVisibleItem) {
selectedMonster = firstVisibleItem.getAttribute('data-monster');
showLoot(selectedMonster, selectedCategory);
updateHighlighting();
}
}
var monsterSearch = document.getElementById('monster-search');
if (monsterSearch) {
monsterSearch.addEventListener('input', filterMonsters);
}
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');
categoryItems.forEach(function(catItem) {
catItem.classList.remove('highlighted');
});
item.classList.add('highlighted');
if (selectedMonster) {
showLoot(selectedMonster, selectedCategory);
}
updateHighlighting();
});
});
if (monsterItems.length > 0) {
selectedMonster = monsterItems[0].getAttribute('data-monster');
showLoot(selectedMonster, selectedCategory);
updateHighlighting();
}
});
})();