opt: partially move update operation to JS

- move fetch update.json operation to JS
- move changelog download to JS
This commit is contained in:
KOWX712
2025-02-22 18:34:34 +08:00
parent e5dcc62fdf
commit f1128cd55b
3 changed files with 116 additions and 46 deletions

View File

@@ -65,9 +65,7 @@ get_unnecessary() {
check_update() { check_update() {
[ -f "$MODDIR/disable" ] && rm -f "$MODDIR/disable" [ -f "$MODDIR/disable" ] && rm -f "$MODDIR/disable"
JSON=$(download --fetch "https://raw.githubusercontent.com/KOWX712/Tricky-Addon-Update-Target-List/main/update.json") || exit 1 LOCAL_VERSION=$(grep '^versionCode=' "$MODPATH/update/module.prop" | awk -F= '{print $2}')
REMOTE_VERSION=$(echo "$JSON" | grep -o '"versionCode": *[0-9]*' | awk -F: '{print $2}' | tr -d ' ')
LOCAL_VERSION=$(grep -o 'versionCode=[0-9]*' "$MODPATH/update/module.prop" | awk -F= '{print $2}')
if [ "$REMOTE_VERSION" -gt "$LOCAL_VERSION" ] && [ ! -f "/data/adb/modules/TA_utl/update" ]; then if [ "$REMOTE_VERSION" -gt "$LOCAL_VERSION" ] && [ ! -f "/data/adb/modules/TA_utl/update" ]; then
if [ "$MAGISK" = "true" ]; then if [ "$MAGISK" = "true" ]; then
[ -d "/data/adb/modules/TA_utl" ] && rm -rf "/data/adb/modules/TA_utl" [ -d "/data/adb/modules/TA_utl" ] && rm -rf "/data/adb/modules/TA_utl"
@@ -89,12 +87,8 @@ uninstall() {
} }
get_update() { get_update() {
JSON=$(download --fetch "https://raw.githubusercontent.com/KOWX712/Tricky-Addon-Update-Target-List/main/update.json") || exit 1 download --output "$ZIP_URL" "$MODPATH/tmp/module.zip"
ZIP_URL=$(echo "$JSON" | grep -o '"zipUrl": "[^"]*"' | cut -d '"' -f 4) || exit 1 [ -s "$MODPATH/tmp/module.zip" ] || exit 1
CHANGELOG_URL=$(echo "$JSON" | grep -o '"changelog": "[^"]*"' | cut -d '"' -f 4) || exit 1
echo "$JSON" | grep '"version"' | sed 's/.*: "//; s/".*//' > "$MODPATH/tmp/version" || exit 1
download --output "$ZIP_URL" "$MODPATH/tmp/module.zip" || exit 1
download --output "$CHANGELOG_URL" "$MODPATH/tmp/changelog.md" || exit 1
} }
install_update() { install_update() {
@@ -114,7 +108,6 @@ install_update() {
} }
release_note() { release_note() {
VERSION=$(grep 'v' "$MODPATH/tmp/version")
awk -v header="### $VERSION" ' awk -v header="### $VERSION" '
$0 == header { $0 == header {
print; print;
@@ -171,7 +164,8 @@ case "$1" in
get_xposed get_xposed
exit exit
;; ;;
--update) --check-update)
REMOTE_VERSION="$2"
check_update check_update
exit exit
;; ;;
@@ -180,6 +174,7 @@ case "$1" in
exit exit
;; ;;
--get-update) --get-update)
ZIP_URL="$2"
get_update get_update
exit exit
;; ;;
@@ -188,6 +183,7 @@ case "$1" in
exit exit
;; ;;
--release-note) --release-note)
VERSION="$2"
release_note release_note
exit exit
;; ;;

View File

@@ -8,27 +8,86 @@ const releaseNotes = document.querySelector('.changelog');
const installButton = document.querySelector('.install'); const installButton = document.querySelector('.install');
const rebootButton = document.querySelector('.reboot'); const rebootButton = document.querySelector('.reboot');
// Function to run the update check let remoteVersionCode, remoteVersion, zipURL, changelogURL, downloading = false;
// Function to download file
function downloadFile(targetURL, fileName) {
return new Promise((resolve, reject) => {
fetch(targetURL)
.then(response => {
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
return response.blob();
})
.then(blob => {
const file = new File([blob], fileName, { type: blob.type });
const reader = new FileReader();
reader.onload = async function() {
const base64Data = reader.result.split(',')[1];
try {
await execCommand(`echo ${base64Data} | base64 -d > ${basePath}common/tmp/${fileName}`);
resolve();
} catch (error) {
reject(error);
}
};
reader.readAsDataURL(file);
})
.catch(reject);
});
}
// Function to check for updates
export async function updateCheck() { export async function updateCheck() {
try { try {
const output = await execCommand(`sh ${basePath}common/get_extra.sh --update`); const response = await fetch("https://raw.githubusercontent.com/KOWX712/Tricky-Addon-Update-Target-List/main/update.json");
console.log("update script executed successfully."); if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
noConnection.style.display = "none"; noConnection.style.display = "none";
if (output.includes("update")) { const data = await response.json();
console.log("Update detected from extra script."); remoteVersionCode = data.versionCode;
showPrompt("prompt.new_update", true, 2000); remoteVersion = data.version;
zipURL = data.zipUrl;
changelogURL = data.changelog;
const updateAvailable = await execCommand(`sh ${basePath}common/get_extra.sh --check-update ${remoteVersionCode}`);
if (updateAvailable.includes("update")) {
showPrompt("prompt.new_update", true, 1500);
updateCard.style.display = "flex"; updateCard.style.display = "flex";
setupUpdateMenu(); setupUpdateMenu();
} else {
console.log("No update detected from extra script.");
} }
} catch (error) { } catch (error) {
console.error("Failed to execute update script:", error); console.error("Error fetching JSON or executing command:", error);
showPrompt("prompt.no_internet", false); showPrompt("prompt.no_internet", false);
noConnection.style.display = "flex"; noConnection.style.display = "flex";
} }
} }
// Function to render changelog
async function renderChangelog() {
const changelog = await execCommand(`sh ${basePath}common/get_extra.sh --release-note ${remoteVersion}`);
window.linkRedirect = linkRedirect;
marked.setOptions({
sanitize: true,
walkTokens(token) {
if (token.type === 'link') {
const href = token.href;
token.href = "javascript:void(0);";
token.type = "html";
token.text = `<a href="javascript:void(0);" onclick="linkRedirect('${href}')">${token.text}</a>`;
}
}
});
const cleanedChangelog = changelog
.split('\n')
.filter(line => line.trim() !== '')
.join('\n');
const formattedChangelog = marked.parse(cleanedChangelog);
releaseNotes.innerHTML = formattedChangelog;
}
// Function to setup update menu // Function to setup update menu
function setupUpdateMenu() { function setupUpdateMenu() {
function openUpdateMenu() { function openUpdateMenu() {
@@ -45,46 +104,59 @@ function setupUpdateMenu() {
UpdateMenu.style.display = "none"; UpdateMenu.style.display = "none";
}, 200); }, 200);
} }
// Update card
updateCard.addEventListener('click', async () => { updateCard.addEventListener('click', async () => {
try { try {
const module = await execCommand(`[ -f ${basePath}common/tmp/module.zip ] || echo "false"`); const module = await execCommand(`
if (module.trim() === "false") { [ -f ${basePath}common/tmp/module.zip ] || echo "noModule"
[ -f ${basePath}common/tmp/changelog.md ] || echo "noChangelog"
[ ! -f /data/adb/modules/TA_utl/update ] || echo "updated"
`);
if (module.trim().includes("updated")) {
installButton.style.display = "none";
rebootButton.style.display = "flex";
openUpdateMenu();
} else if (module.trim().includes("noChangelog")) {
showPrompt("prompt.downloading"); showPrompt("prompt.downloading");
await new Promise(resolve => setTimeout(resolve, 200)); await downloadFile(changelogURL, "changelog.md");
await execCommand(`sh ${basePath}common/get_extra.sh --get-update`); await renderChangelog();
showPrompt("prompt.downloaded"); openUpdateMenu();
} setTimeout(() => {
const changelog = await execCommand(`sh ${basePath}common/get_extra.sh --release-note`); updateCard.click();
window.linkRedirect = linkRedirect; }, 200);
marked.setOptions({ } else if (module.trim().includes("noModule")) {
sanitize: true, if (downloading) return;
walkTokens(token) { downloading = true;
if (token.type === 'link') { try {
const href = token.href; await execCommand(`sh ${basePath}common/get_extra.sh --get-update ${zipURL}`);
token.href = "javascript:void(0);"; showPrompt("prompt.downloaded");
token.type = "html"; installButton.style.display = "flex";
token.text = `<a href="javascript:void(0);" onclick="linkRedirect('${href}')">${token.text}</a>`; downloading = false;
} } catch (error) {
showPrompt("prompt.download_fail", false);
downloading = false;
} }
}); } else {
const cleanedChangelog = changelog installButton.style.display = "flex";
.split('\n') await renderChangelog();
.filter(line => line.trim() !== '') openUpdateMenu();
.join('\n'); }
const formattedChangelog = marked.parse(cleanedChangelog);
releaseNotes.innerHTML = formattedChangelog;
openUpdateMenu();
} catch (error) { } catch (error) {
showPrompt("prompt.download_fail", false); showPrompt("prompt.download_fail", false);
console.error('Error download module update:', error); console.error('Error download module update:', error);
} }
}); });
// Close update menu
closeUpdate.addEventListener("click", closeUpdateMenu); closeUpdate.addEventListener("click", closeUpdateMenu);
UpdateMenu.addEventListener("click", (event) => { UpdateMenu.addEventListener("click", (event) => {
if (event.target === UpdateMenu) { if (event.target === UpdateMenu) {
closeUpdateMenu(); closeUpdateMenu();
} }
}); });
// Install button
installButton.addEventListener('click', async () => { installButton.addEventListener('click', async () => {
try { try {
showPrompt("prompt.installing"); showPrompt("prompt.installing");
@@ -99,6 +171,8 @@ function setupUpdateMenu() {
console.error('Fail to execute installation script:', error); console.error('Fail to execute installation script:', error);
} }
}); });
// Reboot button
rebootButton.addEventListener('click', async () => { rebootButton.addEventListener('click', async () => {
try { try {
showPrompt("prompt.rebooting"); showPrompt("prompt.rebooting");

View File

@@ -122,6 +122,7 @@
.install, .install,
.reboot { .reboot {
display: none;
margin-top: 10px; margin-top: 10px;
margin-bottom: 15px; margin-bottom: 15px;
font-weight: bold; font-weight: bold;
@@ -138,7 +139,6 @@
} }
.reboot { .reboot {
display: none;
color: #fff; color: #fff;
background-color: #007bff; background-color: #007bff;
border: 1px solid #007bff; border: 1px solid #007bff;