Mudanças entre as edições de "Widget:Droflax"
Ir para navegação
Ir para pesquisar
| Linha 16: | Linha 16: | ||
<script> | <script> | ||
// 1. | // Datos de ejemplo para cuando falle la carga | ||
const dadosExemplo = { | |||
"baratie": { | |||
"b1": { | |||
"nome": "Carne", | |||
"thumb": "https://via.placeholder.com/50", | |||
"cost": 5000 | |||
}, | |||
"b2": { | |||
"nome": "Peixe", | |||
"thumb": "https://via.placeholder.com/50", | |||
"cost": 5000 | |||
} | |||
}, | |||
"alianca": { | |||
"a1": { | |||
"nome": "Bife e Ovo de Frigideira", | |||
"thumb": "https://via.placeholder.com/50", | |||
"cost": 720 | |||
}, | |||
"a2": { | |||
"nome": "Medalhão de Carne", | |||
"thumb": "https://via.placeholder.com/50", | |||
"cost": 850 | |||
} | |||
} | |||
}; | |||
// 1. Elementos del DOM | |||
const loadingEl = document.getElementById('loading'); | const loadingEl = document.getElementById('loading'); | ||
const errorEl = document.getElementById('error'); | const errorEl = document.getElementById('error'); | ||
| Linha 22: | Linha 50: | ||
const aliancaList = document.querySelector('#alianca-container .receitas-list'); | const aliancaList = document.querySelector('#alianca-container .receitas-list'); | ||
// 2. Función para cargar el JSON | // 2. Función mejorada para cargar el JSON | ||
async function cargarReceitas() { | async function cargarReceitas() { | ||
try { | try { | ||
const response = await fetch('https://wiki.gla.com.br/index.php/Receitas.json'); | const response = await fetch('https://wiki.gla.com.br/index.php/Receitas.json', { | ||
if (!response.ok) throw new Error( | headers: { | ||
'Accept': 'application/json' | |||
}, | |||
cache: 'no-cache' | |||
}); | |||
// Verificar si la respuesta es HTML en lugar de JSON | |||
const contentType = response.headers.get('content-type'); | |||
if (contentType && contentType.includes('text/html')) { | |||
throw new Error('O servidor retornou uma página HTML em vez de JSON'); | |||
} | |||
if (!response.ok) { | |||
throw new Error(`Erro HTTP: ${response.status}`); | |||
} | |||
return await response.json(); | return await response.json(); | ||
} catch (err) { | } catch (err) { | ||
errorEl.textContent = ` | console.error('Erro ao cargar receitas:', err); | ||
errorEl.textContent = `Não foi possível carregar as receitas. Usando dados locais. (${err.message})`; | |||
errorEl.style.display = 'block'; | errorEl.style.display = 'block'; | ||
return | return dadosExemplo; // Retorna dados de ejemplo | ||
} | } | ||
} | } | ||
| Linha 39: | Linha 83: | ||
if (!data) return; | if (!data) return; | ||
baratieList.innerHTML = ''; | baratieList.innerHTML = ''; | ||
aliancaList.innerHTML = ''; | aliancaList.innerHTML = ''; | ||
// Mostrar recetas de Baratie | // Mostrar recetas de Baratie | ||
for (const [id, receita] of Object.entries(data.baratie)) { | if (data.baratie) { | ||
for (const [id, receita] of Object.entries(data.baratie)) { | |||
baratieList.innerHTML += ` | |||
<div class="receita" style="border:1px solid #ddd; padding:10px; margin:5px 0; display:flex; align-items:center; gap:10px;"> | |||
<img src="${receita.thumb}" width="50" height="50" style="object-fit:cover;"> | |||
<div> | |||
<strong>${receita.nome}</strong><br> | |||
${receita.cost.toLocaleString()} Berry | |||
</div> | |||
</div> | |||
`; | |||
} | |||
} | } | ||
// Mostrar recetas de Aliança | // Mostrar recetas de Aliança | ||
for (const [id, receita] of Object.entries(data.alianca)) { | if (data.alianca) { | ||
for (const [id, receita] of Object.entries(data.alianca)) { | |||
aliancaList.innerHTML += ` | |||
<div class="receita" style="border:1px solid #ddd; padding:10px; margin:5px 0; display:flex; align-items:center; gap:10px;"> | |||
<img src="${receita.thumb}" width="50" height="50" style="object-fit:cover;"> | |||
<div> | |||
<strong>${receita.nome}</strong><br> | |||
${receita.cost.toLocaleString()} Berry | |||
</div> | |||
</div> | |||
`; | |||
} | |||
} | } | ||
} | } | ||
// 4. | // 4. Inicialización mejorada | ||
(async function init() { | (async function init() { | ||
const receitas = await cargarReceitas(); | try { | ||
const receitas = await cargarReceitas(); | |||
mostrarReceitas(receitas); | |||
} catch (err) { | |||
console.error('Erro na inicialização:', err); | |||
errorEl.textContent = 'Erro crítico ao cargar o widget'; | |||
errorEl.style.display = 'block'; | |||
mostrarReceitas(dadosExemplo); // Muestra datos de ejemplo como último recurso | |||
} finally { | |||
loadingEl.style.display = 'none'; | |||
} | |||
})(); | })(); | ||
</script> | </script> | ||
Edição das 18h23min de 8 de abril de 2025
Receitas do Baratie e Aliança
Carregando receitas...
Baratie
Aliança
<script> // Datos de ejemplo para cuando falle la carga const dadosExemplo = {
"baratie": {
"b1": {
"nome": "Carne",
"thumb": "https://via.placeholder.com/50",
"cost": 5000
},
"b2": {
"nome": "Peixe",
"thumb": "https://via.placeholder.com/50",
"cost": 5000
}
},
"alianca": {
"a1": {
"nome": "Bife e Ovo de Frigideira",
"thumb": "https://via.placeholder.com/50",
"cost": 720
},
"a2": {
"nome": "Medalhão de Carne",
"thumb": "https://via.placeholder.com/50",
"cost": 850
}
}
};
// 1. Elementos del DOM const loadingEl = document.getElementById('loading'); const errorEl = document.getElementById('error'); const baratieList = document.querySelector('#baratie-container .receitas-list'); const aliancaList = document.querySelector('#alianca-container .receitas-list');
// 2. Función mejorada para cargar el JSON async function cargarReceitas() {
try {
const response = await fetch('https://wiki.gla.com.br/index.php/Receitas.json', {
headers: {
'Accept': 'application/json'
},
cache: 'no-cache'
});
// Verificar si la respuesta es HTML en lugar de JSON
const contentType = response.headers.get('content-type');
if (contentType && contentType.includes('text/html')) {
throw new Error('O servidor retornou uma página HTML em vez de JSON');
}
if (!response.ok) {
throw new Error(`Erro HTTP: ${response.status}`);
}
return await response.json();
} catch (err) {
console.error('Erro ao cargar receitas:', err);
errorEl.textContent = `Não foi possível carregar as receitas. Usando dados locais. (${err.message})`;
errorEl.style.display = 'block';
return dadosExemplo; // Retorna dados de ejemplo
}
}
// 3. Función para mostrar las recetas function mostrarReceitas(data) {
if (!data) return;
baratieList.innerHTML = ;
aliancaList.innerHTML = ;
// Mostrar recetas de Baratie
if (data.baratie) {
for (const [id, receita] of Object.entries(data.baratie)) {
baratieList.innerHTML += `
<img src="${receita.thumb}" width="50" height="50" style="object-fit:cover;">
${receita.nome}
${receita.cost.toLocaleString()} Berry
`;
}
}
// Mostrar recetas de Aliança
if (data.alianca) {
for (const [id, receita] of Object.entries(data.alianca)) {
aliancaList.innerHTML += `
<img src="${receita.thumb}" width="50" height="50" style="object-fit:cover;">
${receita.nome}
${receita.cost.toLocaleString()} Berry
`; } }
}
// 4. Inicialización mejorada (async function init() {
try {
const receitas = await cargarReceitas();
mostrarReceitas(receitas);
} catch (err) {
console.error('Erro na inicialização:', err);
errorEl.textContent = 'Erro crítico ao cargar o widget';
errorEl.style.display = 'block';
mostrarReceitas(dadosExemplo); // Muestra datos de ejemplo como último recurso
} finally {
loadingEl.style.display = 'none';
}
})(); </script>