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() {
'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 CACHE_KEY = 'pyserve_version_cache';
const CACHE_DURATION = 3600000; // 1 hour
const FALLBACK_VERSION = 'offline';
const FALLBACK_VERSION = '0.9.10';
async function fetchLatestVersion() {
try {
const response = await fetch(RELEASES_URL, {
const response = await fetch(API_URL, {
method: 'GET',
mode: 'cors',
headers: {
'Accept': 'text/html'
'Accept': 'application/json'
}
});
if (!response.ok) {
console.warn(`Failed to fetch: ${response.status}`);
console.warn(`API request failed: ${response.status}`);
return null;
}
const html = await response.text();
const patterns = [
/<title>PyServeX\s+v([\d.]+)/i,
/releases\/tag\/v([\d.]+)/,
/aria-label="PyServeX\s+v([\d.]+)/i,
/<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];
}
const data = await response.json();
if (data.tag_name) {
const version = data.tag_name.replace(/^v/, '');
console.log(`✓ Version fetched from Gitea API: ${version}`);
return version;
}
console.warn('Version not found in HTML');
console.warn('tag_name not found in API response');
return null;
} catch (error) {
console.warn('Fetch error:', error.message);
console.warn('API fetch error:', error.message);
return null;
}
}