feat: add version check (#367)

Signed-off-by: JohnsonRan <me@ihtw.moe>
This commit is contained in:
旋律已经死了。
2025-05-12 17:02:08 +08:00
committed by GitHub
parent 916cd54741
commit e868282080
11 changed files with 214 additions and 45 deletions

View File

@@ -45,8 +45,8 @@ jobs:
upstream_sync_branch: main
target_sync_branch: main
target_repo_token: ${{ secrets.GITHUB_TOKEN }}
commit_user_name: github-actions[bot]
commit_user_email: 41898282+github-actions[bot]@users.noreply.github.com
git_config_user: github-actions[bot]
git_config_email: 41898282+github-actions[bot]@users.noreply.github.com
test_mode: false
- name: Sync check

34
.github/workflows/version.yml vendored Normal file
View File

@@ -0,0 +1,34 @@
name: Bump version
on:
push:
branches:
- main
workflow_dispatch:
jobs:
bump-version:
runs-on: ubuntu-latest
env:
TZ: 'Asia/Shanghai'
steps:
- name: Checkout repository
uses: actions/checkout@v4
- name: Bump version and commit changes
run: |
CURRENT_TIME=$(date +"%Y-%m-%d %H:%M")
git config user.name 'github-actions[bot]'
git config user.email 'github-actions[bot]@users.noreply.github.com'
echo $(date +"%Y%m%d%H%M") > VERSION.txt
git add VERSION.txt
git commit -m "Auto Update $CURRENT_TIME"
git push origin main
- name: Delete workflow runs
uses: Mattraks/delete-workflow-runs@main
with:
token: ${{ secrets.GITHUB_TOKEN }}
repository: ${{ github.repository }}
retain_days: 0
keep_minimum_runs: 2

1
VERSION.txt Normal file
View File

@@ -0,0 +1 @@
202505121430

BIN
image/retrotv_5520.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.0 KiB

View File

@@ -24,8 +24,8 @@
<meta property="twitter:image" content="https://images.icon-icons.com/38/PNG/512/retrotv_5520.png">
<!-- Favicon -->
<link rel="icon" href="https://images.icon-icons.com/38/PNG/512/retrotv_5520.png">
<link rel="apple-touch-icon" href="https://images.icon-icons.com/38/PNG/512/retrotv_5520.png">
<link rel="icon" href="./image/retrotv_5520.png">
<link rel="apple-touch-icon" href="./image/retrotv_5520.png">
<link rel="manifest" href="manifest.json">
<!-- Canonical URL -->
@@ -434,6 +434,9 @@
window.__ENV__.PASSWORD = "{{PASSWORD}}";
</script>
<!-- 版本检测脚本 -->
<script src="js/version-check.js"></script>
<!-- 弹窗显示脚本 -->
<script>
// 在页面加载完成后检查用户是否首次访问

View File

@@ -15,7 +15,7 @@ const SITE_CONFIG = {
name: 'LibreTV',
url: 'https://libretv.is-an.org',
description: '免费在线视频搜索与观看平台',
logo: 'https://images.icon-icons.com/38/PNG/512/retrotv_5520.png',
logo: './image/retrotv_5520.png',
version: '1.0.3'
};

162
js/version-check.js Normal file
View File

@@ -0,0 +1,162 @@
// 添加动画样式
(function() {
const style = document.createElement('style');
style.textContent = `
@keyframes pulse {
0%, 100% {
opacity: 1;
}
50% {
opacity: 0.6;
}
}
.animate-pulse {
animation: pulse 2s cubic-bezier(0.4, 0, 0.6, 1) infinite;
}
`;
document.head.appendChild(style);
})();
// 版本检查函数
async function checkForUpdates() {
try {
const currentVersionResponse = await fetch('/VERSION.txt', {
cache: 'no-store'
});
if (!currentVersionResponse.ok) {
throw new Error('获取当前版本失败');
}
const currentVersion = await currentVersionResponse.text();
// 获取最新版本
const latestVersionResponse = await fetch('https://raw.githubusercontent.com/JohnsonRan/LibreTV/main/VERSION.txt');
if (!latestVersionResponse.ok) {
throw new Error('获取最新版本失败');
}
const latestVersion = await latestVersionResponse.text();
console.log('当前版本:', currentVersion);
console.log('最新版本:', latestVersion);
// 清理版本字符串(移除可能的空格或换行符)
const cleanCurrentVersion = currentVersion.trim();
const cleanLatestVersion = latestVersion.trim();
// 返回版本信息
return {
current: cleanCurrentVersion,
latest: cleanLatestVersion,
hasUpdate: parseInt(cleanLatestVersion) > parseInt(cleanCurrentVersion),
currentFormatted: formatVersion(cleanCurrentVersion),
latestFormatted: formatVersion(cleanLatestVersion)
};
} catch (error) {
console.error('版本检测出错:', error);
throw error;
}
}
// 格式化版本号为可读形式 (yyyyMMddhhmm -> yyyy-MM-dd hh:mm)
function formatVersion(versionString) {
// 检测版本字符串是否有效
if (!versionString) {
return '未知版本';
}
// 清理版本字符串(移除可能的空格或换行符)
const cleanedString = versionString.trim();
// 格式化标准12位版本号
if (cleanedString.length === 12) {
const year = cleanedString.substring(0, 4);
const month = cleanedString.substring(4, 6);
const day = cleanedString.substring(6, 8);
const hour = cleanedString.substring(8, 10);
const minute = cleanedString.substring(10, 12);
return `${year}-${month}-${day} ${hour}:${minute}`;
}
return cleanedString;
}
// 创建错误版本信息元素
function createErrorVersionElement(errorMessage) {
const errorElement = document.createElement('p');
errorElement.className = 'text-gray-400 text-xs mt-1 text-center md:text-left';
errorElement.innerHTML = `版本: <span class="text-amber-500">检测失败</span>`;
errorElement.title = errorMessage || "无法连接到 Github";
return errorElement;
}
// 添加版本信息到页脚
function addVersionInfoToFooter() {
checkForUpdates().then(result => {
if (!result) {
// 如果版本检测失败,显示错误信息
const versionElement = createErrorVersionElement();
// 在页脚显示错误元素
displayVersionElement(versionElement);
return;
}
// 创建版本信息元素
const versionElement = document.createElement('p');
versionElement.className = 'text-gray-400 text-xs mt-1 text-center md:text-left';
// 添加当前版本信息
versionElement.innerHTML = `版本: ${result.currentFormatted}`;
// 如果有更新,添加更新提示
if (result.hasUpdate) {
versionElement.innerHTML += ` <span class="inline-flex items-center bg-red-600 text-white text-xs px-2 py-0.5 rounded-md ml-1 cursor-pointer animate-pulse font-medium">
<svg xmlns="http://www.w3.org/2000/svg" class="h-3 w-3 mr-1" fill="none" viewBox="0 0 24 24" stroke="currentColor">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M13 10V3L4 14h7v7l9-11h-7z" />
</svg>
发现新版
</span>`;
setTimeout(() => {
const updateBtn = versionElement.querySelector('span');
if (updateBtn) {
updateBtn.addEventListener('click', () => {
window.open('https://github.com/LibreSpark/LibreTV', '_blank');
});
}
}, 100);
} else {
// 如果没有更新,显示当前版本为最新版本
versionElement.innerHTML = `版本: ${result.currentFormatted} <span class="text-green-500">(最新版本)</span>`;
}
// 显示版本元素
displayVersionElement(versionElement);
}).catch(error => {
console.error('版本检测出错:', error);
// 创建错误版本信息元素并显示
const errorElement = createErrorVersionElement(`错误信息: ${error.message}`);
displayVersionElement(errorElement);
});
}
// 在页脚显示版本元素的辅助函数
function displayVersionElement(element) {
// 获取页脚元素
const footerElement = document.querySelector('.footer p.text-gray-500.text-sm');
if (footerElement) {
// 在原版权信息后插入版本信息
footerElement.insertAdjacentElement('afterend', element);
} else {
// 如果找不到页脚元素,尝试在页脚区域最后添加
const footer = document.querySelector('.footer .container');
if (footer) {
footer.querySelector('div').appendChild(element);
}
}
}
// 页面加载完成后添加版本信息
document.addEventListener('DOMContentLoaded', addVersionInfoToFooter);

View File

@@ -10,7 +10,7 @@
"apple-mobile-web-app-status-bar-style": "black",
"icons": [
{
"src": "https://images.icon-icons.com/38/PNG/512/retrotv_5520.png",
"src": "./image/retrotv_5520.png",
"sizes": "512x512",
"type": "image/png"
}

View File

@@ -6,12 +6,13 @@
<title>LibreTV 播放器</title>
<!-- Favicon -->
<link rel="icon" href="https://images.icon-icons.com/38/PNG/512/retrotv_5520.png">
<link rel="apple-touch-icon" href="https://images.icon-icons.com/38/PNG/512/retrotv_5520.png">
<link rel="icon" href="./image/retrotv_5520.png">
<link rel="apple-touch-icon" href="./image/retrotv_5520.png">
<link rel="manifest" href="manifest.json">
<script src="libs/tailwindcss.min.js"></script>
<script src="js/wakelock.js"></script>
<script src="js/version-check.js"></script>
<link rel="stylesheet" href="css/styles.css">
<style>
body, html {

View File

@@ -1,7 +1,7 @@
# LibreTV - 免费在线视频搜索与观看平台
<div align="center">
<img src="https://images.icon-icons.com/38/PNG/512/retrotv_5520.png" alt="LibreTV Logo" width="120">
<img src="./image/retrotv_5520.png" alt="LibreTV Logo" width="120">
<br>
<p><strong>自由观影,畅享精彩</strong></p>
</div>

View File

@@ -1,40 +1,8 @@
const CACHE_NAME = 'libre-tv-cache-v1';
const urlsToCache = [
'/',
'/css/styles.css',
'/js/app.js',
'/js/config.js',
'/js/ui.js',
'/js/api.js',
'/js/password.js',
'/libs/tailwindcss.min.js',
'/libs/DPlayer.min.js',
'/libs/hls.min.js',
'/libs/sha256.min.js',
'/https://images.icon-icons.com/38/PNG/512/retrotv_5520.png',
'/manifest.json'
];
// 不使用缓存,直接通过网络获取资源
self.addEventListener('install', event => {
event.waitUntil(
caches.open(CACHE_NAME)
.then(cache => {
console.log('Opened cache');
return cache.addAll(urlsToCache);
})
);
self.skipWaiting();
});
self.addEventListener('fetch', event => {
event.respondWith(
caches.match(event.request)
.then(response => {
// Cache hit - return response
if (response) {
return response;
}
return fetch(event.request);
}
)
);
self.addEventListener('activate', event => {
event.waitUntil(self.clients.claim());
});