Skip to content

Commit c78b64b

Browse files
authored
🎨 让安装页面URL好看一点 (#993)
* [v1.3] 让安装页面URL好看一点 * Update utils.ts * Update utils.ts
1 parent d37a832 commit c78b64b

File tree

2 files changed

+37
-3
lines changed

2 files changed

+37
-3
lines changed

src/pages/install/App.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ import { intervalExecution, timeoutExecution } from "@App/pkg/utils/timer";
3131
import { useSearchParams } from "react-router-dom";
3232
import { CACHE_KEY_SCRIPT_INFO } from "@App/app/cache_key";
3333
import { cacheInstance } from "@App/app/cache";
34-
import { formatBytes } from "@App/pkg/utils/utils";
34+
import { formatBytes, prettyUrl } from "@App/pkg/utils/utils";
3535

3636
type ScriptOrSubscribe = Script | Subscribe;
3737

@@ -779,13 +779,13 @@ function App() {
779779
bold
780780
style={{
781781
overflowWrap: "break-word",
782-
wordBreak: "break-all",
782+
wordBreak: "break-word",
783783
maxHeight: "70px",
784784
display: "block",
785785
overflowY: "auto",
786786
}}
787787
>
788-
{`${t("source")}: ${scriptInfo?.url}`}
788+
{`${t("source")}: ${prettyUrl(scriptInfo?.url)}`}
789789
</Typography.Text>
790790
</div>
791791
</div>

src/pkg/utils/utils.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -439,3 +439,37 @@ export const formatBytes = (bytes: number, decimals: number = 2): string => {
439439

440440
return `${value.toFixed(decimals)} ${units[i]}`;
441441
};
442+
443+
// 把编码URL变成使用者可以阅读的格式
444+
export const prettyUrl = (s: string | undefined | null, baseUrl?: string) => {
445+
if (s?.includes("://")) {
446+
let u;
447+
try {
448+
u = baseUrl ? new URL(s, baseUrl) : new URL(s);
449+
} catch {
450+
// ignored
451+
}
452+
if (!u) return s;
453+
const pathname = u.pathname;
454+
if (pathname && pathname.includes("%")) {
455+
try {
456+
const raw = decodeURI(pathname);
457+
if (
458+
raw &&
459+
raw.length < pathname.length &&
460+
!raw.includes("?") &&
461+
!raw.includes("#") &&
462+
!raw.includes("&") &&
463+
!raw.includes("=") &&
464+
!raw.includes("%") &&
465+
!raw.includes(":")
466+
) {
467+
s = s.replace(pathname, raw);
468+
}
469+
} catch {
470+
// ignored
471+
}
472+
}
473+
}
474+
return s;
475+
};

0 commit comments

Comments
 (0)