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

View File

@@ -8,27 +8,86 @@ const releaseNotes = document.querySelector('.changelog');
const installButton = document.querySelector('.install');
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() {
try {
const output = await execCommand(`sh ${basePath}common/get_extra.sh --update`);
console.log("update script executed successfully.");
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}`);
}
noConnection.style.display = "none";
if (output.includes("update")) {
console.log("Update detected from extra script.");
showPrompt("prompt.new_update", true, 2000);
const data = await response.json();
remoteVersionCode = data.versionCode;
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";
setupUpdateMenu();
} else {
console.log("No update detected from extra script.");
}
} catch (error) {
console.error("Failed to execute update script:", error);
console.error("Error fetching JSON or executing command:", error);
showPrompt("prompt.no_internet", false);
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 setupUpdateMenu() {
function openUpdateMenu() {
@@ -45,46 +104,59 @@ function setupUpdateMenu() {
UpdateMenu.style.display = "none";
}, 200);
}
// Update card
updateCard.addEventListener('click', async () => {
try {
const module = await execCommand(`[ -f ${basePath}common/tmp/module.zip ] || echo "false"`);
if (module.trim() === "false") {
const module = await execCommand(`
[ -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");
await new Promise(resolve => setTimeout(resolve, 200));
await execCommand(`sh ${basePath}common/get_extra.sh --get-update`);
showPrompt("prompt.downloaded");
}
const changelog = await execCommand(`sh ${basePath}common/get_extra.sh --release-note`);
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>`;
}
await downloadFile(changelogURL, "changelog.md");
await renderChangelog();
openUpdateMenu();
setTimeout(() => {
updateCard.click();
}, 200);
} else if (module.trim().includes("noModule")) {
if (downloading) return;
downloading = true;
try {
await execCommand(`sh ${basePath}common/get_extra.sh --get-update ${zipURL}`);
showPrompt("prompt.downloaded");
installButton.style.display = "flex";
downloading = false;
} catch (error) {
showPrompt("prompt.download_fail", false);
downloading = false;
}
});
const cleanedChangelog = changelog
.split('\n')
.filter(line => line.trim() !== '')
.join('\n');
const formattedChangelog = marked.parse(cleanedChangelog);
releaseNotes.innerHTML = formattedChangelog;
openUpdateMenu();
} else {
installButton.style.display = "flex";
await renderChangelog();
openUpdateMenu();
}
} catch (error) {
showPrompt("prompt.download_fail", false);
console.error('Error download module update:', error);
}
});
// Close update menu
closeUpdate.addEventListener("click", closeUpdateMenu);
UpdateMenu.addEventListener("click", (event) => {
if (event.target === UpdateMenu) {
closeUpdateMenu();
}
});
// Install button
installButton.addEventListener('click', async () => {
try {
showPrompt("prompt.installing");
@@ -99,6 +171,8 @@ function setupUpdateMenu() {
console.error('Fail to execute installation script:', error);
}
});
// Reboot button
rebootButton.addEventListener('click', async () => {
try {
showPrompt("prompt.rebooting");

View File

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