HodlEye_Crypto_Price_Tracker/public/news.js
Gerald Hasani f751559878 Release 1.0.6
- Responsive design addet
- Crypto News Auto Update without Browser Refresh every 3 minutes
- Crypto Price Refresh Reduce from 5 Second to 1 Second
- Docker command updated to saved alarms and registered cryptocurrencies that are still there after a Docker image update
--> docker run -p 3099:3099 -p 5001:5001 -v hodleye_data:/app/data --name hodleye-container hodleye-crypto-tracker

- Bugfix "Edit List" works now
- Addet Update Notification every 5 Days
- re-Design from Crypto-box
- improving the logic of the price display for small cryptocurrencies
2025-03-09 02:07:54 +01:00

84 lines
2.9 KiB
JavaScript

document.addEventListener("DOMContentLoaded", function () {
const apiUrl = "http://localhost:5001/api/news";
const newsFeed = document.getElementById("news-feed");
const searchInput = document.getElementById("search");
let allArticles = [];
function refreshNewsFeed() {
fetch(apiUrl)
.then(response => response.json())
.then(data => {
allArticles = data;
displayArticles(allArticles);
})
.catch(error => {
newsFeed.innerHTML = "Error loading the news.";
console.error("Error when retrieving the news:", error);
});
}
function displayArticles(items) {
newsFeed.innerHTML = "";
items.forEach(item => {
let newsItem = document.createElement("div");
newsItem.classList.add("news-item");
newsItem.innerHTML = `
<div class="news-content">
<a href="${item.link}" target="_blank" class="news-title">${item.title}</a>
<div class="news-meta">
<span class="news-source">${formatSourceName(item.source)}</span>
<span class="time">${getTimeAgo(item.pubDate)}</span>
</div>
</div>
`;
newsFeed.appendChild(newsItem);
});
}
searchInput.addEventListener("input", function () {
const searchTerm = searchInput.value.toLowerCase();
const filteredArticles = allArticles.filter(item =>
item.title.toLowerCase().includes(searchTerm) ||
item.description.toLowerCase().includes(searchTerm)
);
displayArticles(filteredArticles);
});
function getTimeAgo(date) {
const now = new Date();
const seconds = Math.floor((now - new Date(date)) / 1000);
if (seconds < 60) return `vor ${seconds} Sekunden`;
const minutes = Math.floor(seconds / 60);
if (minutes < 60) return `vor ${minutes} Minuten`;
const hours = Math.floor(minutes / 60);
if (hours < 24) return `vor ${hours} Stunden`;
const days = Math.floor(hours / 24);
if (days < 7) return `vor ${days} Tagen`;
if (days < 30) return `vor ${Math.floor(days / 7)} Wochen`;
if (days < 365) return `vor ${Math.floor(days / 30)} Monaten`;
return `vor ${Math.floor(days / 365)} Jahren`;
}
function formatSourceName(source) {
const sourceMap = {
"crypto_news": "Crypto News",
"cointelegraph": "Cointelegraph",
"thedefiant": "The Defiant",
"newsbtc": "NewsBTC",
"bitcoin_news": "Bitcoin.com",
"decrypt": "Decrypt"
};
return sourceMap[source] || source;
}
refreshNewsFeed();
setInterval(refreshNewsFeed, 180000);
});