MediaWiki:Common.js: Diferență între versiuni
m (Ștergerea conținutului paginii) (Etichetă: Golire) |
m |
||
Linia 1: | Linia 1: | ||
+ | (function() { | ||
+ | document.addEventListener('DOMContentLoaded', function() { | ||
+ | function showLoot(monster) { | ||
+ | var lootList = document.getElementById('loot-list'); | ||
+ | lootList.innerHTML = ''; | ||
+ | var apiUrl = mw.util.wikiScript('api') + '?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) { | ||
+ | var wikitext = data.parse.wikitext['*']; | ||
+ | console.log(wikitext); | ||
+ | |||
+ | if (wikitext.startsWith('#Redirect')) { | ||
+ | var targetPageMatch = wikitext.match(/\[\[([^\]]+)\]\]/); | ||
+ | if (targetPageMatch) { | ||
+ | var targetPage = targetPageMatch[1]; | ||
+ | var redirectUrl = mw.util.wikiScript('api') + '?action=parse&page=' + encodeURIComponent(targetPage) + '&prop=wikitext&format=json&origin=*'; | ||
+ | return fetch(redirectUrl) | ||
+ | .then(function(response) { | ||
+ | if (!response.ok) { | ||
+ | throw new Error('Redirect Network response was not ok: ' + response.statusText); | ||
+ | } | ||
+ | return response.json(); | ||
+ | }); | ||
+ | } | ||
+ | } | ||
+ | return data; | ||
+ | }) | ||
+ | .then(function(data) { | ||
+ | var wikitext = data.parse.wikitext['*']; | ||
+ | var weapondropsMatch = wikitext.match(/\|Weapondrops\s*=\s*([^\n|]*)/); | ||
+ | if (weapondropsMatch) { | ||
+ | var weapondrops = weapondropsMatch[1].trim(); | ||
+ | var lootItems = weapondrops.split('::'); | ||
+ | |||
+ | lootItems.forEach(function(item) { | ||
+ | if (item.trim()) { | ||
+ | var li = document.createElement('li'); | ||
+ | li.textContent = item.trim(); | ||
+ | lootList.appendChild(li); | ||
+ | } | ||
+ | }); | ||
+ | } else { | ||
+ | var li = document.createElement('li'); | ||
+ | li.textContent = 'No weapon drops found'; | ||
+ | lootList.appendChild(li); | ||
+ | } | ||
+ | }, function(error) { | ||
+ | console.error('Error:', error); | ||
+ | }); | ||
+ | } | ||
+ | |||
+ | var items = document.querySelectorAll('#monster-list li'); | ||
+ | items.forEach(function(item) { | ||
+ | item.addEventListener('click', function() { | ||
+ | showLoot(item.getAttribute('data-monster')); | ||
+ | }); | ||
+ | }); | ||
+ | }); | ||
+ | })(); |
Versiunea de la data 22 august 2024 11:21
(function() {
document.addEventListener('DOMContentLoaded', function() {
function showLoot(monster) {
var lootList = document.getElementById('loot-list');
lootList.innerHTML = '';
var apiUrl = mw.util.wikiScript('api') + '?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) {
var wikitext = data.parse.wikitext['*'];
console.log(wikitext);
if (wikitext.startsWith('#Redirect')) {
var targetPageMatch = wikitext.match(/\[\[([^\]]+)\]\]/);
if (targetPageMatch) {
var targetPage = targetPageMatch[1];
var redirectUrl = mw.util.wikiScript('api') + '?action=parse&page=' + encodeURIComponent(targetPage) + '&prop=wikitext&format=json&origin=*';
return fetch(redirectUrl)
.then(function(response) {
if (!response.ok) {
throw new Error('Redirect Network response was not ok: ' + response.statusText);
}
return response.json();
});
}
}
return data;
})
.then(function(data) {
var wikitext = data.parse.wikitext['*'];
var weapondropsMatch = wikitext.match(/\|Weapondrops\s*=\s*([^\n|]*)/);
if (weapondropsMatch) {
var weapondrops = weapondropsMatch[1].trim();
var lootItems = weapondrops.split('::');
lootItems.forEach(function(item) {
if (item.trim()) {
var li = document.createElement('li');
li.textContent = item.trim();
lootList.appendChild(li);
}
});
} else {
var li = document.createElement('li');
li.textContent = 'No weapon drops found';
lootList.appendChild(li);
}
}, function(error) {
console.error('Error:', error);
});
}
var items = document.querySelectorAll('#monster-list li');
items.forEach(function(item) {
item.addEventListener('click', function() {
showLoot(item.getAttribute('data-monster'));
});
});
});
})();