donate/script.js

84 lines
2.6 KiB
JavaScript
Raw Permalink Normal View History

2024-08-22 22:51:09 +08:00
let currentLang = "zh";
2024-08-22 22:12:36 +08:00
let data, i18n;
document.addEventListener("DOMContentLoaded", () => {
setRandomBackground();
2024-08-22 22:51:09 +08:00
loadData("data.json", loadBlockchainOptions);
2024-08-22 22:12:36 +08:00
changeLanguage(currentLang);
2024-08-22 22:51:09 +08:00
updateCopyrightYear();
2024-08-22 22:12:36 +08:00
});
function setRandomBackground() {
2024-08-23 13:15:10 +08:00
const images = ['1.jpg', '2.jpg', '3.png', '4.png', '5.png'];
const randomIndex = Math.floor(Math.random() * images.length);
const selectedImage = images[randomIndex];
document.body.style.backgroundImage = `url('img/bg/${selectedImage}')`;
}
2024-08-22 22:12:36 +08:00
function updateCopyrightYear() {
const currentYear = new Date().getFullYear();
2024-08-22 22:51:09 +08:00
let copyrightText;
if (currentYear > 2024) {
2024-08-22 22:12:36 +08:00
copyrightText = `© 2024-${currentYear} earthjasonlin. All rights reserved.`;
} else {
copyrightText = `© 2024 earthjasonlin. All rights reserved.`;
}
document.querySelector("footer p").innerHTML = copyrightText;
}
function loadData(url, callback) {
fetch(url)
2024-08-22 22:51:09 +08:00
.then((response) => response.json())
.then((json) => {
2024-08-22 22:12:36 +08:00
data = json;
callback();
})
2024-08-22 22:51:09 +08:00
.catch((error) => console.error("Error loading JSON:", error));
2024-08-22 22:12:36 +08:00
}
function loadI18n(url, callback) {
fetch(url)
2024-08-22 22:51:09 +08:00
.then((response) => response.json())
.then((json) => {
2024-08-22 22:12:36 +08:00
i18n = json;
callback();
})
2024-08-22 22:51:09 +08:00
.catch((error) => console.error("Error loading i18n JSON:", error));
2024-08-22 22:12:36 +08:00
}
function loadBlockchainOptions() {
const blockchainSelect = document.getElementById("blockchain");
2024-08-22 22:51:09 +08:00
blockchainSelect.innerHTML = "";
2024-08-22 22:12:36 +08:00
for (const key in data) {
const option = document.createElement("option");
option.value = key;
option.textContent = i18n[key];
blockchainSelect.appendChild(option);
}
updateDonationInfo();
}
function changeLanguage(lang) {
loadI18n(`i18n/${lang}.json`, () => {
2024-08-22 23:10:38 +08:00
document.getElementById("thank-you").innerHTML = i18n.thankYou;
2024-08-22 22:49:04 +08:00
document.getElementById("title-text").textContent = i18n.title;
2024-08-22 22:12:36 +08:00
document.getElementById("copyAddress").textContent = i18n.copyAddress;
loadBlockchainOptions();
});
}
function updateDonationInfo() {
const blockchain = document.getElementById("blockchain").value;
document.getElementById("qr-code").src = data[blockchain].qr;
document.getElementById("address").textContent = data[blockchain].address;
document.getElementById("blockchain-icon").src = data[blockchain].icon;
}
function copyAddress() {
const address = document.getElementById("address").textContent;
navigator.clipboard.writeText(address).then(() => {
alert(i18n.copyAddressSuccess);
});
}