mirror of
https://github.com/BetterSEQTA/BetterSEQTA-Plus.git
synced 2026-06-05 19:24:39 +00:00
fix: news not loading
This commit is contained in:
+13
-12
@@ -92,8 +92,12 @@ const rssFeedsByCountry: Record<string, string[]> = {
|
|||||||
* used to send the fetched news data back to the caller.
|
* used to send the fetched news data back to the caller.
|
||||||
* It's called with an object like `{ news: { articles: [...] } }`.
|
* It's called with an object like `{ news: { articles: [...] } }`.
|
||||||
*/
|
*/
|
||||||
export async function fetchNews(source: string, sendResponse: any) {
|
export async function fetchNews(source: string | undefined, sendResponse: any) {
|
||||||
if (source === "australia") {
|
const normalizedSource = typeof source === "string" && source.trim()
|
||||||
|
? source.trim()
|
||||||
|
: "australia";
|
||||||
|
|
||||||
|
if (normalizedSource === "australia") {
|
||||||
const date = new Date();
|
const date = new Date();
|
||||||
|
|
||||||
const from =
|
const from =
|
||||||
@@ -111,18 +115,15 @@ export async function fetchNews(source: string, sendResponse: any) {
|
|||||||
|
|
||||||
const parser = new Parser();
|
const parser = new Parser();
|
||||||
let feeds: string[];
|
let feeds: string[];
|
||||||
console.log("fetchNews", source);
|
console.log("fetchNews", normalizedSource);
|
||||||
|
|
||||||
if (rssFeedsByCountry[source.toLowerCase()]) {
|
if (rssFeedsByCountry[normalizedSource.toLowerCase()]) {
|
||||||
// If the source is a country, fetch from predefined feeds
|
feeds = rssFeedsByCountry[normalizedSource.toLowerCase()];
|
||||||
feeds = rssFeedsByCountry[source.toLowerCase()];
|
} else if (normalizedSource.startsWith("http")) {
|
||||||
} else if (source.startsWith("http")) {
|
feeds = [normalizedSource];
|
||||||
// If the source is a URL, use it directly
|
|
||||||
feeds = [source];
|
|
||||||
} else {
|
} else {
|
||||||
throw new Error(
|
console.warn("[BetterSEQTA+] Invalid news source, falling back to Australia", normalizedSource);
|
||||||
"Invalid source. Provide a country code or a valid RSS feed URL.",
|
return fetchNews("australia", sendResponse);
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const articlesPromises = feeds.map(async (feedUrl) => {
|
const articlesPromises = feeds.map(async (feedUrl) => {
|
||||||
|
|||||||
@@ -21,7 +21,7 @@
|
|||||||
"service_worker": "background.ts"
|
"service_worker": "background.ts"
|
||||||
},
|
},
|
||||||
"content_security_policy": {
|
"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": [
|
"content_scripts": [
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -53,11 +53,22 @@ export async function SendNewsPage() {
|
|||||||
const newscontainer = document.querySelector("#news-container");
|
const newscontainer = document.querySelector("#news-container");
|
||||||
document.getElementById("newsloading")?.remove();
|
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();
|
const fragment = document.createDocumentFragment();
|
||||||
|
|
||||||
// Map over articles to create elements
|
articles.forEach((article: any) => {
|
||||||
response.news.articles.forEach((article: any) => {
|
|
||||||
const newsarticle = document.createElement("a");
|
const newsarticle = document.createElement("a");
|
||||||
newsarticle.classList.add("NewsArticle");
|
newsarticle.classList.add("NewsArticle");
|
||||||
newsarticle.href = article.url;
|
newsarticle.href = article.url;
|
||||||
@@ -85,12 +96,14 @@ export async function SendNewsPage() {
|
|||||||
title.target = "_blank";
|
title.target = "_blank";
|
||||||
|
|
||||||
const description = document.createElement("p");
|
const description = document.createElement("p");
|
||||||
|
const articleDescription = typeof article.description === "string"
|
||||||
|
? article.description
|
||||||
|
: "No description available.";
|
||||||
|
|
||||||
article.description =
|
description.innerHTML =
|
||||||
article.description.length > 400
|
articleDescription.length > 400
|
||||||
? article.description.substring(0, 400) + "..."
|
? articleDescription.substring(0, 400) + "..."
|
||||||
: article.description;
|
: articleDescription;
|
||||||
description.innerHTML = article.description;
|
|
||||||
|
|
||||||
articletext.append(title, description);
|
articletext.append(title, description);
|
||||||
newsarticle.append(articleimage, articletext);
|
newsarticle.append(articleimage, articletext);
|
||||||
@@ -102,10 +115,10 @@ export async function SendNewsPage() {
|
|||||||
|
|
||||||
if (!settingsState.animations) return;
|
if (!settingsState.animations) return;
|
||||||
|
|
||||||
const articles = Array.from(document.querySelectorAll(".NewsArticle"));
|
const animatedArticles = Array.from(document.querySelectorAll(".NewsArticle"));
|
||||||
|
|
||||||
animate(
|
animate(
|
||||||
articles.slice(0, 20),
|
animatedArticles.slice(0, 20),
|
||||||
{ opacity: [0, 1], y: [10, 0], scale: [0.99, 1] },
|
{ opacity: [0, 1], y: [10, 0], scale: [0.99, 1] },
|
||||||
{
|
{
|
||||||
delay: stagger(0.1),
|
delay: stagger(0.1),
|
||||||
|
|||||||
Reference in New Issue
Block a user