Refactor version fetcher to use Gitea API for fetching latest version

This commit is contained in:
Илья Глазунов 2025-12-07 23:03:58 +03:00
parent 0dca4e8ddc
commit 898c7b6a7a

View File

@ -5,49 +5,39 @@
(function() { (function() {
'use strict'; 'use strict';
const RELEASES_URL = 'https://git.pyserve.org/Shifty/pyserveX/releases/latest'; const API_URL = 'https://git.pyserve.org/api/v1/repos/Shifty/pyserveX/releases/latest';
const VERSION_ELEMENT_ID = 'current-version'; const VERSION_ELEMENT_ID = 'current-version';
const CACHE_KEY = 'pyserve_version_cache'; const CACHE_KEY = 'pyserve_version_cache';
const CACHE_DURATION = 3600000; // 1 hour const CACHE_DURATION = 3600000; // 1 hour
const FALLBACK_VERSION = 'offline'; const FALLBACK_VERSION = '0.9.10';
async function fetchLatestVersion() { async function fetchLatestVersion() {
try { try {
const response = await fetch(RELEASES_URL, { const response = await fetch(API_URL, {
method: 'GET', method: 'GET',
mode: 'cors',
headers: { headers: {
'Accept': 'text/html' 'Accept': 'application/json'
} }
}); });
if (!response.ok) { if (!response.ok) {
console.warn(`Failed to fetch: ${response.status}`); console.warn(`API request failed: ${response.status}`);
return null; return null;
} }
const html = await response.text(); const data = await response.json();
const patterns = [ if (data.tag_name) {
/<title>PyServeX\s+v([\d.]+)/i, const version = data.tag_name.replace(/^v/, '');
/releases\/tag\/v([\d.]+)/, console.log(`✓ Version fetched from Gitea API: ${version}`);
/aria-label="PyServeX\s+v([\d.]+)/i, return version;
/<h4[^>]*>.*?v([\d.]+).*?<\/h4>/i
];
for (const pattern of patterns) {
const match = html.match(pattern);
if (match && match[1]) {
console.log(`✓ Version found: ${match[1]}`);
return match[1];
}
} }
console.warn('Version not found in HTML'); console.warn('tag_name not found in API response');
return null; return null;
} catch (error) { } catch (error) {
console.warn('Fetch error:', error.message); console.warn('API fetch error:', error.message);
return null; return null;
} }
} }