From bbc9d662a655d3ab80b71164c94eb4807e4ac432 Mon Sep 17 00:00:00 2001 From: nonameShijian <2954700422@qq.com> Date: Tue, 16 Apr 2024 22:11:34 +0800 Subject: [PATCH] =?UTF-8?q?=E5=B0=BD=E9=87=8F=E5=87=8F=E5=B0=91gainAuthori?= =?UTF-8?q?zation=E7=9A=84=E8=B0=83=E7=94=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- noname/library/update.js | 43 ++++++++++++++---------- noname/ui/create/index.js | 5 +-- noname/ui/create/menu/pages/otherMenu.js | 35 ++++++++++--------- 3 files changed, 47 insertions(+), 36 deletions(-) diff --git a/noname/library/update.js b/noname/library/update.js index b8497c199..bba8b98d3 100644 --- a/noname/library/update.js +++ b/noname/library/update.js @@ -26,7 +26,7 @@ if (localStorage.getItem("noname_authorization")) { */ export async function gainAuthorization() { if (!localStorage.getItem("noname_authorization") && !sessionStorage.getItem("noname_authorization")) { - const result = await game.promises.prompt("请输入您github的token以解除访问每小时60次的限制"); + const result = await game.promises.prompt("请输入您github的token以解除访问每小时60次的限制(可不输入)"); if (typeof result == "string") { localStorage.setItem("noname_authorization", result); defaultHeaders["Authorization"] = `token ${localStorage.getItem("noname_authorization")}`; @@ -45,11 +45,17 @@ const defaultResponse = async (/** @type {Response} */ response) => { // @ts-ignore console.log(`限制重置时间`, new Date(reset * 1000).toLocaleString()); if ( - Number(remaining) === 0 && - !sessionStorage.getItem("noname_authorization") && - confirm(`您达到了每小时${limit}次的访问限制,是否输入您github的token以获取更高的请求总量限制`) + ( + Number(remaining) === 0 && + !sessionStorage.getItem("noname_authorization") && + confirm(`您达到了每小时${limit}次的访问限制,是否输入您github账号的token以获取更高的请求总量限制`) + ) || ( + response.status === 401 && + (localStorage.removeItem("noname_authorization"), true) && + (alert(`身份验证凭证错误,是否重新输入您github账号的token以获取更高的请求总量限制`), true) + ) ) { - await gainAuthorization(); + return gainAuthorization(); } }; @@ -168,9 +174,9 @@ export function checkVersion(ver1, ver2) { * ``` */ export async function getRepoTags(options = { username: "libccy", repository: "noname" }) { - if (!localStorage.getItem("noname_authorization")) { - await gainAuthorization(); - } + // if (!localStorage.getItem("noname_authorization")) { + // await gainAuthorization(); + // } const { username = "libccy", repository = "noname", accessToken } = options; const headers = Object.assign({}, defaultHeaders); if (accessToken) { @@ -203,9 +209,9 @@ export async function getRepoTags(options = { username: "libccy", repository: "n */ export async function getRepoTagDescription(tagName, options = { username: "libccy", repository: "noname" }) { - if (!localStorage.getItem("noname_authorization")) { - await gainAuthorization(); - } + // if (!localStorage.getItem("noname_authorization")) { + // await gainAuthorization(); + // } const { username = "libccy", repository = "noname", accessToken } = options; const headers = Object.assign({}, defaultHeaders); if (accessToken) { @@ -267,9 +273,9 @@ export async function getRepoFilesList( branch, options = { username: "libccy", repository: "noname" } ) { - if (!localStorage.getItem("noname_authorization")) { - await gainAuthorization(); - } + // if (!localStorage.getItem("noname_authorization")) { + // await gainAuthorization(); + // } const { username = "libccy", repository = "noname", accessToken } = options; const headers = Object.assign({}, defaultHeaders); if (accessToken) { @@ -327,6 +333,9 @@ export async function flattenRepositoryFiles( branch, options = { username: "libccy", repository: "noname" } ) { + if (!localStorage.getItem("noname_authorization")) { + await gainAuthorization(); + } /** * @type { { download_url: string, name: string, path: string, sha: string, size: number, type: 'file' }[] } */ @@ -536,8 +545,6 @@ export function createProgress(title, max, fileName, value) { * @throws {Error} 如果获取操作失败或找不到有效tag,将抛出错误。 */ export async function getLatestVersionFromGitHub(owner = "libccy", repo = "noname") { - if (!localStorage.getItem("noname_authorization")) await gainAuthorization(); - const tags = await getRepoTags({ username: owner, repository: repo, @@ -574,7 +581,7 @@ export async function getLatestVersionFromGitHub(owner = "libccy", repo = "nonam * @throws {Error} Will throw an error if unable to fetch the repository tree from GitHub. */ export async function getTreesFromGithub(directories, version, owner = "libccy", repo = "noname") { - if (!localStorage.getItem("noname_authorization")) await gainAuthorization(); + // if (!localStorage.getItem("noname_authorization")) await gainAuthorization(); const treesResponse = await fetch( `https://api.github.com/repos/${owner}/${repo}/git/trees/${version}?recursive=1`, @@ -582,7 +589,7 @@ export async function getTreesFromGithub(directories, version, owner = "libccy", headers: defaultHeaders, } ); - + await defaultResponse(treesResponse); if (!treesResponse.ok) throw new Error(`Failed to fetch the GitHub repository tree: HTTP status ${treesResponse.status}`); /** diff --git a/noname/ui/create/index.js b/noname/ui/create/index.js index 1cd3e635d..3aa7f81b9 100644 --- a/noname/ui/create/index.js +++ b/noname/ui/create/index.js @@ -2562,8 +2562,9 @@ export class Create { lib.arenaReady.shift()(); } delete lib.arenaReady; - if (lib.config.auto_check_update) { - setTimeout(function () { + if (lib.config.auto_check_update && !sessionStorage.getItem("auto_check_update")) { + setTimeout(() => { + sessionStorage.setItem("auto_check_update", '1'); game.checkForUpdate(false); }, 3000); } diff --git a/noname/ui/create/menu/pages/otherMenu.js b/noname/ui/create/menu/pages/otherMenu.js index 7452f198b..f0a15487e 100644 --- a/noname/ui/create/menu/pages/otherMenu.js +++ b/noname/ui/create/menu/pages/otherMenu.js @@ -14,12 +14,9 @@ import { ui, game, get, ai, lib, _status } from "../../../../../noname.js"; import { parseSize, checkVersion, - getRepoTags, getRepoTagDescription, - flattenRepositoryFiles, request, createProgress, - gainAuthorization, getLatestVersionFromGitHub, getTreesFromGithub, } from "../../../../library/update.js"; @@ -396,17 +393,17 @@ export const otherMenu = function (/** @type { boolean | undefined } */ connectM if (checkAssetButton.disabled) { return; } else if (game.download) { - if ( - !localStorage.getItem("noname_authorization") && - !sessionStorage.getItem("noname_authorization") - ) { - if ( - confirm( - "素材更新或许会直接超过每小时的访问限制,是否输入您github的token以解除访问每小时60次的限制?" - ) - ) - await gainAuthorization(); - } + // if ( + // !localStorage.getItem("noname_authorization") && + // !sessionStorage.getItem("noname_authorization") + // ) { + // if ( + // confirm( + // "素材更新或许会直接超过每小时的访问限制,是否输入您github的token以解除访问每小时60次的限制?" + // ) + // ) + // await gainAuthorization(); + // } checkAssetButton.innerHTML = "正在检查更新"; checkAssetButton.disabled = true; @@ -419,8 +416,14 @@ export const otherMenu = function (/** @type { boolean | undefined } */ connectM if (lib.config.asset_font) assetDirectories.push("font"); if (lib.config.asset_audio) assetDirectories.push("audio"); if (lib.config.asset_image) assetDirectories.push("image"); - const version = await getLatestVersionFromGitHub(); - const files = await getTreesFromGithub(assetDirectories, version); + const version = await getLatestVersionFromGitHub().catch(e => { + refresh(); + throw e; + }); + const files = await getTreesFromGithub(assetDirectories, version).catch(e => { + refresh(); + throw e; + }); assetDirectories.forEach((assetDirectory, index) => { const arr = files[index];