Skip to content

一分钟搭建影视站,支持Docker等部署方式

feat: import from URL Signed-off-by: JohnsonRan <[email protected]>

JohnsonRanef2948549b2b
diff --git a/js/app.js b/js/app.js
index 52a75b1..75a90ed 100644
--- a/js/app.js
+++ b/js/app.js
@@ -1109,6 +1109,118 @@ function toggleEpisodeOrder(sourceCode) {
}
}
+// 从URL导入配置
+async function importConfigFromUrl() {
+ // 创建模态框元素
+ let modal = document.getElementById('importUrlModal');
+ if (modal) {
+ document.body.removeChild(modal);
+ }
+
+ modal = document.createElement('div');
+ modal.id = 'importUrlModal';
+ modal.className = 'fixed inset-0 bg-black bg-opacity-75 flex items-center justify-center z-40';
+
+ modal.innerHTML = `
+ <div class="bg-[#191919] rounded-lg p-6 max-w-md w-full max-h-[90vh] overflow-y-auto relative">
+ <button id="closeUrlModal" class="absolute top-4 right-4 text-gray-400 hover:text-white text-xl">&times;</button>
+
+ <h3 class="text-xl font-bold mb-4">从URL导入配置</h3>
+
+ <div class="mb-4">
+ <input type="text" id="configUrl" placeholder="输入配置文件URL"
+ class="w-full px-3 py-2 bg-[#222] border border-[#333] rounded-lg text-white focus:outline-none focus:ring-1 focus:ring-blue-500">
+ </div>
+
+ <div class="flex justify-end space-x-2">
+ <button id="confirmUrlImport" class="bg-blue-600 hover:bg-blue-700 text-white px-4 py-2 rounded">导入</button>
+ <button id="cancelUrlImport" class="bg-[#444] hover:bg-[#555] text-white px-4 py-2 rounded">取消</button>
+ </div>
+ </div>`;
+
+ document.body.appendChild(modal);
+
+ // 关闭按钮事件
+ document.getElementById('closeUrlModal').addEventListener('click', () => {
+ document.body.removeChild(modal);
+ });
+
+ // 取消按钮事件
+ document.getElementById('cancelUrlImport').addEventListener('click', () => {
+ document.body.removeChild(modal);
+ });
+
+ // 确认导入按钮事件
+ document.getElementById('confirmUrlImport').addEventListener('click', async () => {
+ const url = document.getElementById('configUrl').value.trim();
+ if (!url) {
+ showToast('请输入配置文件URL', 'warning');
+ return;
+ }
+
+ // 验证URL格式
+ try {
+ const urlObj = new URL(url);
+ if (urlObj.protocol !== 'http:' && urlObj.protocol !== 'https:') {
+ showToast('URL必须以http://或https://开头', 'warning');
+ return;
+ }
+ } catch (e) {
+ showToast('URL格式不正确', 'warning');
+ return;
+ }
+
+ showLoading('正在从URL导入配置...');
+
+ try {
+ // 获取配置文件 - 直接请求URL
+ const response = await fetch(url, {
+ mode: 'cors',
+ headers: {
+ 'Accept': 'application/json'
+ }
+ });
+ if (!response.ok) throw '获取配置文件失败';
+
+ // 验证响应内容类型
+ const contentType = response.headers.get('content-type');
+ if (!contentType || !contentType.includes('application/json')) {
+ throw '响应不是有效的JSON格式';
+ }
+
+ const config = await response.json();
+ if (config.name !== 'LibreTV-Settings') throw '配置文件格式不正确';
+
+ // 验证哈希
+ const dataHash = await sha256(JSON.stringify(config.data));
+ if (dataHash !== config.hash) throw '配置文件哈希值不匹配';
+
+ // 导入配置
+ for (let item in config.data) {
+ localStorage.setItem(item, config.data[item]);
+ }
+
+ showToast('配置文件导入成功,3 秒后自动刷新本页面。', 'success');
+ setTimeout(() => {
+ window.location.reload();
+ }, 3000);
+ } catch (error) {
+ const message = typeof error === 'string' ? error : '导入配置失败';
+ showToast(`从URL导入配置出错 (${message})`, 'error');
+ } finally {
+ hideLoading();
+ document.body.removeChild(modal);
+ }
+ });
+
+ // 点击模态框外部关闭
+ modal.addEventListener('click', (e) => {
+ if (e.target === modal) {
+ document.body.removeChild(modal);
+ }
+ });
+}
+
// 配置文件导入功能
async function importConfig() {
showImportBox(async (file) => {
@@ -1151,6 +1263,100 @@ async function importConfig() {
});
}
+// 从URL导入配置
+async function importConfigFromUrl() {
+ // 创建模态框元素
+ let modal = document.getElementById('importUrlModal');
+ if (modal) {
+ document.body.removeChild(modal);
+ }
+
+ modal = document.createElement('div');
+ modal.id = 'importUrlModal';
+ modal.className = 'fixed inset-0 bg-black bg-opacity-75 flex items-center justify-center z-40';
+
+ modal.innerHTML = `
+ <div class="bg-[#191919] rounded-lg p-6 max-w-md w-full max-h-[90vh] overflow-y-auto relative">
+ <button id="closeUrlModal" class="absolute top-4 right-4 text-gray-400 hover:text-white text-xl">&times;</button>
+
+ <h3 class="text-xl font-bold mb-4">从URL导入配置</h3>
+
+ <div class="mb-4">
+ <input type="text" id="configUrl" placeholder="输入配置文件URL"
+ class="w-full px-3 py-2 bg-[#222] border border-[#333] rounded-lg text-white focus:outline-none focus:ring-1 focus:ring-blue-500">
+ </div>
+
+ <div class="flex justify-end space-x-2">
+ <button id="confirmUrlImport" class="bg-blue-600 hover:bg-blue-700 text-white px-4 py-2 rounded">导入</button>
+ <button id="cancelUrlImport" class="bg-[#444] hover:bg-[#555] text-white px-4 py-2 rounded">取消</button>
+ </div>
+ </div>`;
+
+ document.body.appendChild(modal);
+
+ // 关闭按钮事件
+ document.getElementById('closeUrlModal').addEventListener('click', () => {
+ document.body.removeChild(modal);
+ });
+
+ // 取消按钮事件
+ document.getElementById('cancelUrlImport').addEventListener('click', () => {
+ document.body.removeChild(modal);
+ });
+
+ // 确认导入按钮事件
+ document.getElementById('confirmUrlImport').addEventListener('click', async () => {
+ const url = document.getElementById('configUrl').value.trim();
+ if (!url) {
+ showToast('请输入配置文件URL', 'warning');
+ return;
+ }
+
+ if (!url.startsWith('http://') && !url.startsWith('https://')) {
+ showToast('URL必须以http://或https://开头', 'warning');
+ return;
+ }
+
+ showLoading('正在从URL导入配置...');
+
+ try {
+ // 获取配置文件
+ const response = await fetch(PROXY_URL + encodeURIComponent(url));
+ if (!response.ok) throw '获取配置文件失败';
+
+ const config = await response.json();
+ if (config.name !== 'LibreTV-Settings') throw '配置文件格式不正确';
+
+ // 验证哈希
+ const dataHash = await sha256(JSON.stringify(config.data));
+ if (dataHash !== config.hash) throw '配置文件哈希值不匹配';
+
+ // 导入配置
+ for (let item in config.data) {
+ localStorage.setItem(item, config.data[item]);
+ }
+
+ showToast('配置文件导入成功,3 秒后自动刷新本页面。', 'success');
+ setTimeout(() => {
+ window.location.reload();
+ }, 3000);
+ } catch (error) {
+ const message = typeof error === 'string' ? error : '导入配置失败';
+ showToast(`从URL导入配置出错 (${message})`, 'error');
+ } finally {
+ hideLoading();
+ document.body.removeChild(modal);
+ }
+ });
+
+ // 点击模态框外部关闭
+ modal.addEventListener('click', (e) => {
+ if (e.target === modal) {
+ document.body.removeChild(modal);
+ }
+ });
+}
+
// 配置文件导出功能
async function exportConfig() {
// 存储配置数据
diff --git a/js/ui.js b/js/ui.js
index dc3727f..020abd1 100644
--- a/js/ui.js
+++ b/js/ui.js
@@ -744,12 +744,13 @@ function showImportBox(fun) {
</div>
<div class="grid gap-2">
<h4 class="text-center text-white-900 text-sm font-medium leading-snug">将配置文件拖到此处,或手动选择文件</h4>
- <div class="flex items-center justify-center">
- <label>
- <input type="file" id="ChooseFile" hidden />
- <div class="flex w-28 h-9 px-2 flex-col bg-pink-600 rounded-full shadow text-white text-xs font-semibold leading-4 items-center justify-center cursor-pointer focus:outline-none">选择文件</div>
- </label>
- </div>
+ <div class="flex items-center justify-center gap-2">
+ <label>
+ <input type="file" id="ChooseFile" hidden />
+ <div class="flex w-28 h-9 px-2 flex-col bg-pink-600 rounded-full shadow text-white text-xs font-semibold leading-4 items-center justify-center cursor-pointer focus:outline-none">选择文件</div>
+ </label>
+ <button onclick="importConfigFromUrl()" class="flex w-28 h-9 px-2 flex-col bg-blue-600 rounded-full shadow text-white text-xs font-semibold leading-4 items-center justify-center cursor-pointer focus:outline-none">从URL导入</button>
+ </div>
</div>
</div>
</div>