init
This commit is contained in:
83
script.js
Normal file
83
script.js
Normal file
@@ -0,0 +1,83 @@
|
||||
let currentLang = "zh";
|
||||
let data, i18n;
|
||||
|
||||
document.addEventListener("DOMContentLoaded", () => {
|
||||
setRandomBackground();
|
||||
loadData("data.json", loadBlockchainOptions);
|
||||
changeLanguage(currentLang);
|
||||
updateCopyrightYear();
|
||||
});
|
||||
|
||||
function setRandomBackground() {
|
||||
const images = ["2.jpg", "12.png", "13.png"];
|
||||
const randomIndex = Math.floor(Math.random() * images.length);
|
||||
const selectedImage = images[randomIndex];
|
||||
document.body.style.backgroundImage = `url('img/bg/${selectedImage}')`;
|
||||
}
|
||||
|
||||
function updateCopyrightYear() {
|
||||
const currentYear = new Date().getFullYear();
|
||||
let copyrightText;
|
||||
if (currentYear > 2024) {
|
||||
copyrightText = `© 2024-${currentYear} <a href="https://earthjasonlin.cn" target="_blank" rel="noopener noreferrer">earthjasonlin</a>. All rights reserved.`;
|
||||
} else {
|
||||
copyrightText = `© 2024 earthjasonlin. All rights reserved.`;
|
||||
}
|
||||
document.getElementById("copyright-me").innerHTML = copyrightText;
|
||||
}
|
||||
|
||||
function loadData(url, callback) {
|
||||
fetch(url)
|
||||
.then((response) => response.json())
|
||||
.then((json) => {
|
||||
data = json;
|
||||
callback();
|
||||
})
|
||||
.catch((error) => console.error("Error loading JSON:", error));
|
||||
}
|
||||
|
||||
function loadI18n(url, callback) {
|
||||
fetch(url)
|
||||
.then((response) => response.json())
|
||||
.then((json) => {
|
||||
i18n = json;
|
||||
callback();
|
||||
})
|
||||
.catch((error) => console.error("Error loading i18n JSON:", error));
|
||||
}
|
||||
|
||||
function loadBlockchainOptions() {
|
||||
const blockchainSelect = document.getElementById("blockchain");
|
||||
blockchainSelect.innerHTML = "";
|
||||
|
||||
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`, () => {
|
||||
document.getElementById("thank-you").innerHTML = i18n.thankYou;
|
||||
document.getElementById("title-text").textContent = i18n.title;
|
||||
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);
|
||||
});
|
||||
}
|
Reference in New Issue
Block a user