From 92e654614e9ca063a6edc82d39d5c620121956f3 Mon Sep 17 00:00:00 2001 From: KOWX712 Date: Sun, 6 Apr 2025 22:58:49 +0800 Subject: [PATCH] feat: add mirror link fallback - mostly mean for China user, possible to use all feature except module update --- module/webui/scripts/menu_option.js | 72 ++++++++++++++++------------- module/webui/scripts/update.js | 9 ++-- 2 files changed, 46 insertions(+), 35 deletions(-) diff --git a/module/webui/scripts/menu_option.js b/module/webui/scripts/menu_option.js index ccf5043..553e055 100644 --- a/module/webui/scripts/menu_option.js +++ b/module/webui/scripts/menu_option.js @@ -42,11 +42,16 @@ document.getElementById("deselect-unnecessary").addEventListener("click", async try { const excludeList = await fetch("https://raw.githubusercontent.com/KOWX712/Tricky-Addon-Update-Target-List/main/more-exclude.json") .then(response => { - if (!response.ok) { - throw new Error(`HTTP error! status: ${response.status}`); - } + if (!response.ok) throw new Error(`HTTP error! status: ${response.status}`); return response.json(); }) + .catch(async () => { + return fetch("https://raw.gitmirror.com/KOWX712/Tricky-Addon-Update-Target-List/main/more-exclude.json") + .then(response => { + if (!response.ok) throw new Error(`HTTP error! status: ${response.status}`); + return response.json(); + }); + }) .then(data => { return data.data .flatMap(category => category.apps) @@ -205,35 +210,40 @@ document.getElementById("aospkb").addEventListener("click", async () => { // Function to replace valid kb document.getElementById("validkb").addEventListener("click", async () => { fetch("https://raw.githubusercontent.com/KOWX712/Tricky-Addon-Update-Target-List/main/.extra") - .then(response => { - if (!response.ok) { - throw new Error(`HTTP error! status: ${response.status}`); - } - return response.text(); - }) - .then(async data => { - if (!data.trim()) { - await aospkb(); - showPrompt("prompt.no_valid_fallback", false); - return; - } - try { - const hexBytes = new Uint8Array(data.match(/.{1,2}/g).map(byte => parseInt(byte, 16))); - const decodedHex = new TextDecoder().decode(hexBytes); - const source = atob(decodedHex); - const result = await setKeybox(source); - if (result) { - showPrompt("prompt.valid_key_set"); - } else { - throw new Error("Failed to copy valid keybox"); + .then(response => { + if (!response.ok) throw new Error(`HTTP error! status: ${response.status}`); + return response.text(); + }) + .catch(async () => { + return fetch("https://raw.gitmirror.com/KOWX712/Tricky-Addon-Update-Target-List/main/.extra") + .then(response => { + if (!response.ok) throw new Error(`HTTP error! status: ${response.status}`); + return response.text(); + }); + }) + .then(async data => { + if (!data.trim()) { + await aospkb(); + showPrompt("prompt.no_valid_fallback", false); + return; } - } catch (error) { - throw new Error("Failed to decode keybox data"); - } - }) - .catch(async error => { - showPrompt("prompt.no_internet", false); - }); + try { + const hexBytes = new Uint8Array(data.match(/.{1,2}/g).map(byte => parseInt(byte, 16))); + const decodedHex = new TextDecoder().decode(hexBytes); + const source = atob(decodedHex); + const result = await setKeybox(source); + if (result) { + showPrompt("prompt.valid_key_set"); + } else { + throw new Error("Failed to copy valid keybox"); + } + } catch (error) { + throw new Error("Failed to decode keybox data"); + } + }) + .catch(async error => { + showPrompt("prompt.no_internet", false); + }); }); // File selector diff --git a/module/webui/scripts/update.js b/module/webui/scripts/update.js index 7929784..4dabfb8 100644 --- a/module/webui/scripts/update.js +++ b/module/webui/scripts/update.js @@ -41,10 +41,11 @@ function downloadFile(targetURL, fileName) { // Function to check for updates export async function updateCheck() { try { - const response = await fetch("https://raw.githubusercontent.com/KOWX712/Tricky-Addon-Update-Target-List/main/update.json"); - if (!response.ok) { - throw new Error(`HTTP error! status: ${response.status}`); - } + const response = await fetch("https://raw.githubusercontent.com/KOWX712/Tricky-Addon-Update-Target-List/main/update.json") + .catch(async () => { + return fetch("https://raw.gitmirror.com/KOWX712/Tricky-Addon-Update-Target-List/main/update.json"); + }); + if (!response.ok) throw new Error(`HTTP error! status: ${response.status}`); noConnection.style.display = "none"; const data = await response.json(); remoteVersionCode = data.versionCode;