fix: news not loading

This commit is contained in:
SethBurkart123
2026-03-18 09:43:32 +11:00
parent 915ce6f5f1
commit 8a05d85344
3 changed files with 37 additions and 23 deletions
+13 -12
View File
@@ -92,8 +92,12 @@ const rssFeedsByCountry: Record<string, string[]> = {
* used to send the fetched news data back to the caller.
* It's called with an object like `{ news: { articles: [...] } }`.
*/
export async function fetchNews(source: string, sendResponse: any) {
if (source === "australia") {
export async function fetchNews(source: string | undefined, sendResponse: any) {
const normalizedSource = typeof source === "string" && source.trim()
? source.trim()
: "australia";
if (normalizedSource === "australia") {
const date = new Date();
const from =
@@ -111,18 +115,15 @@ export async function fetchNews(source: string, sendResponse: any) {
const parser = new Parser();
let feeds: string[];
console.log("fetchNews", source);
console.log("fetchNews", normalizedSource);
if (rssFeedsByCountry[source.toLowerCase()]) {
// If the source is a country, fetch from predefined feeds
feeds = rssFeedsByCountry[source.toLowerCase()];
} else if (source.startsWith("http")) {
// If the source is a URL, use it directly
feeds = [source];
if (rssFeedsByCountry[normalizedSource.toLowerCase()]) {
feeds = rssFeedsByCountry[normalizedSource.toLowerCase()];
} else if (normalizedSource.startsWith("http")) {
feeds = [normalizedSource];
} else {
throw new Error(
"Invalid source. Provide a country code or a valid RSS feed URL.",
);
console.warn("[BetterSEQTA+] Invalid news source, falling back to Australia", normalizedSource);
return fetchNews("australia", sendResponse);
}
const articlesPromises = feeds.map(async (feedUrl) => {
+1 -1
View File
@@ -21,7 +21,7 @@
"service_worker": "background.ts"
},
"content_security_policy": {
"extension_pages": "script-src 'self'; object-src 'self'; connect-src 'self' https://betterseqta.org https://accounts.betterseqta.org https://raw.githubusercontent.com"
"extension_pages": "script-src 'self'; object-src 'self'; connect-src 'self' http: https: https://betterseqta.org https://accounts.betterseqta.org https://raw.githubusercontent.com https://newsapi.org"
},
"content_scripts": [
{
+23 -10
View File
@@ -53,11 +53,22 @@ export async function SendNewsPage() {
const newscontainer = document.querySelector("#news-container");
document.getElementById("newsloading")?.remove();
// Create a document fragment to batch DOM operations
const articles = response?.news?.articles;
if (!Array.isArray(articles) || articles.length === 0) {
const emptyState = document.createElement("div");
emptyState.classList.add("day-empty");
const img = document.createElement("img");
img.src = browser.runtime.getURL(LogoLightOutline);
const text = document.createElement("p");
text.innerText = "No news articles available right now.";
emptyState.append(img, text);
newscontainer?.append(emptyState);
return;
}
const fragment = document.createDocumentFragment();
// Map over articles to create elements
response.news.articles.forEach((article: any) => {
articles.forEach((article: any) => {
const newsarticle = document.createElement("a");
newsarticle.classList.add("NewsArticle");
newsarticle.href = article.url;
@@ -85,12 +96,14 @@ export async function SendNewsPage() {
title.target = "_blank";
const description = document.createElement("p");
const articleDescription = typeof article.description === "string"
? article.description
: "No description available.";
article.description =
article.description.length > 400
? article.description.substring(0, 400) + "..."
: article.description;
description.innerHTML = article.description;
description.innerHTML =
articleDescription.length > 400
? articleDescription.substring(0, 400) + "..."
: articleDescription;
articletext.append(title, description);
newsarticle.append(articleimage, articletext);
@@ -102,10 +115,10 @@ export async function SendNewsPage() {
if (!settingsState.animations) return;
const articles = Array.from(document.querySelectorAll(".NewsArticle"));
const animatedArticles = Array.from(document.querySelectorAll(".NewsArticle"));
animate(
articles.slice(0, 20),
animatedArticles.slice(0, 20),
{ opacity: [0, 1], y: [10, 0], scale: [0.99, 1] },
{
delay: stagger(0.1),