MediaWiki:Common.js: Diferență între versiuni
m |
m |
||
| Linia 35: | Linia 35: | ||
console.log(wikitext); | console.log(wikitext); | ||
| + | // Handle redirects | ||
| + | if (wikitext.startsWith('#Redirect')) { | ||
| + | var targetPageMatch = wikitext.match(/\[\[([^\]]+)\]\]/); | ||
| + | if (targetPageMatch) { | ||
| + | var targetPage = targetPageMatch[1]; | ||
| + | return fetch(apiUrl.replace(encodeURIComponent(monster), encodeURIComponent(targetPage))); | ||
| + | } | ||
| + | } | ||
| + | |||
| + | // If not a redirect, proceed with processing | ||
var weapondropsMatch = wikitext.match(/\|Weapondrops\s*=\s*([^\n|]*)/); | var weapondropsMatch = wikitext.match(/\|Weapondrops\s*=\s*([^\n|]*)/); | ||
| − | |||
if (weapondropsMatch) { | if (weapondropsMatch) { | ||
var weapondrops = weapondropsMatch[1].trim(); | var weapondrops = weapondropsMatch[1].trim(); | ||
| Linia 53: | Linia 62: | ||
lootList.appendChild(li); | lootList.appendChild(li); | ||
} | } | ||
| − | |||
| − | |||
}) | }) | ||
| − | + | .then(function(response) { | |
| + | return response.json(); | ||
| + | }) | ||
.catch(function(error) { | .catch(function(error) { | ||
var lootList = document.getElementById('loot-list'); | var lootList = document.getElementById('loot-list'); | ||
| Linia 68: | Linia 77: | ||
console.error('Error:', error); | console.error('Error:', error); | ||
}); | }); | ||
| − | |||
| − | |||
} | } | ||
Versiunea de la data 22 august 2024 10:33
/* Any JavaScript here will be loaded for all users on every page load. */
(function () {
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();
})();
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);
// Handle redirects
if (wikitext.startsWith('#Redirect')) {
var targetPageMatch = wikitext.match(/\[\[([^\]]+)\]\]/);
if (targetPageMatch) {
var targetPage = targetPageMatch[1];
return fetch(apiUrl.replace(encodeURIComponent(monster), encodeURIComponent(targetPage)));
}
}
// If not a redirect, proceed with processing
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);
}
})
.then(function(response) {
return response.json();
})
.catch(function(error) {
var lootList = document.getElementById('loot-list');
if (lootList) {
var li = document.createElement('li');
li.textContent = 'Error fetching loot: ' + error.message;
lootList.appendChild(li);
} else {
console.error('Element with ID "loot-list" not found.');
}
console.error('Error:', error);
});
}
var items = document.querySelectorAll('#monster-list li');
items.forEach(function(item) {
item.addEventListener('click', function() {
showLoot(item.getAttribute('data-monster'));
});
});
});