mirror of
https://github.com/BetterSEQTA/BetterSEQTA-Plus.git
synced 2026-06-05 19:24:39 +00:00
feat: add github fallback
This commit is contained in:
+11
-4
@@ -57,13 +57,20 @@ browser.runtime.onMessage.addListener(
|
|||||||
return true;
|
return true;
|
||||||
|
|
||||||
case "fetchThemes": {
|
case "fetchThemes": {
|
||||||
const url = `https://betterseqta.org/api/themes?type=betterseqta&limit=100&nocache=${Date.now()}`;
|
const apiUrl = `https://betterseqta.org/api/themes?type=betterseqta&limit=100&nocache=${Date.now()}`;
|
||||||
fetch(url, { cache: "no-store" })
|
const githubUrl = `https://raw.githubusercontent.com/BetterSEQTA/BetterSEQTA-Themes/main/store/themes.json?nocache=${Date.now()}`;
|
||||||
|
fetch(apiUrl, { cache: "no-store" })
|
||||||
.then((r) => r.json())
|
.then((r) => r.json())
|
||||||
.then(sendResponse)
|
.then(sendResponse)
|
||||||
.catch((err) => {
|
.catch((err) => {
|
||||||
console.error("[Background] fetchThemes error:", err);
|
console.warn("[Background] fetchThemes API failed, trying GitHub fallback:", err?.message);
|
||||||
sendResponse({ success: false, error: err?.message });
|
fetch(githubUrl, { cache: "no-store" })
|
||||||
|
.then((r) => r.json())
|
||||||
|
.then((data) => sendResponse({ success: true, data: { themes: data.themes ?? [] } }))
|
||||||
|
.catch((fallbackErr) => {
|
||||||
|
console.error("[Background] fetchThemes GitHub fallback error:", fallbackErr);
|
||||||
|
sendResponse({ success: false, error: fallbackErr?.message });
|
||||||
|
});
|
||||||
});
|
});
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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"
|
"extension_pages": "script-src 'self'; object-src 'self'; connect-src 'self' https://betterseqta.org https://raw.githubusercontent.com"
|
||||||
},
|
},
|
||||||
"content_scripts": [
|
"content_scripts": [
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -472,6 +472,7 @@ export class ThemeManager {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private readonly THEME_API_BASE = 'https://betterseqta.org/api';
|
private readonly THEME_API_BASE = 'https://betterseqta.org/api';
|
||||||
|
private readonly GITHUB_THEMES_BASE = 'https://raw.githubusercontent.com/BetterSEQTA/BetterSEQTA-Themes/main/store/themes';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Fetch JSON from a URL via background script (avoids CORS when running inside SEQTA page)
|
* Fetch JSON from a URL via background script (avoids CORS when running inside SEQTA page)
|
||||||
@@ -487,7 +488,7 @@ export class ThemeManager {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Download and install a theme from the store.
|
* Download and install a theme from the store.
|
||||||
* Always calls the download API first to increment download_count on the server.
|
* Uses API first (increments download_count), falls back to GitHub if unreachable.
|
||||||
*/
|
*/
|
||||||
public async downloadTheme(themeContent: {
|
public async downloadTheme(themeContent: {
|
||||||
id: string;
|
id: string;
|
||||||
@@ -500,16 +501,23 @@ export class ThemeManager {
|
|||||||
try {
|
try {
|
||||||
if (!themeContent.id) return;
|
if (!themeContent.id) return;
|
||||||
|
|
||||||
// Always call download endpoint to increment download_count
|
let themeData: ThemeContent;
|
||||||
const downloadData = (await this.fetchFromUrl(
|
|
||||||
`${this.THEME_API_BASE}/themes/${themeContent.id}/download`
|
|
||||||
)) as { success?: boolean; data?: { theme_json_url: string } };
|
|
||||||
if (!downloadData?.success || !downloadData?.data?.theme_json_url) {
|
|
||||||
throw new Error("Failed to get theme download URL");
|
|
||||||
}
|
|
||||||
const themeJsonUrl = downloadData.data.theme_json_url;
|
|
||||||
|
|
||||||
const themeData = (await this.fetchFromUrl(themeJsonUrl)) as ThemeContent;
|
try {
|
||||||
|
// Try API first (increments download_count)
|
||||||
|
const downloadData = (await this.fetchFromUrl(
|
||||||
|
`${this.THEME_API_BASE}/themes/${themeContent.id}/download`
|
||||||
|
)) as { success?: boolean; data?: { theme_json_url: string } };
|
||||||
|
if (!downloadData?.success || !downloadData?.data?.theme_json_url) {
|
||||||
|
throw new Error("Failed to get theme download URL");
|
||||||
|
}
|
||||||
|
themeData = (await this.fetchFromUrl(downloadData.data.theme_json_url)) as ThemeContent;
|
||||||
|
} catch (apiError) {
|
||||||
|
// Fallback to GitHub if API is unreachable
|
||||||
|
console.warn("[ThemeManager] API failed, trying GitHub fallback:", apiError);
|
||||||
|
const githubUrl = `${this.GITHUB_THEMES_BASE}/${themeContent.id}/theme.json`;
|
||||||
|
themeData = (await this.fetchFromUrl(githubUrl)) as ThemeContent;
|
||||||
|
}
|
||||||
|
|
||||||
await this.installTheme(themeData);
|
await this.installTheme(themeData);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
|||||||
Reference in New Issue
Block a user