diff options
| author | Larvan2 <[email protected]> | 2025-12-14 14:11:43 +0800 |
|---|---|---|
| committer | GitHub <[email protected]> | 2025-12-14 14:11:43 +0800 |
| commit | 4abe041d6d21dbd51b3093645573e8590fdcb4bb (patch) | |
| tree | bb8f2d7fccdf671d68917ae0dfddfccdb78e49ba /src | |
| parent | 02c5187cc003363a48a0c3dfa9c9101112d198c7 (diff) | |
| parent | 314e68812f7890e84503b1593902e8624e6b6824 (diff) | |
Merge pull request #98 from pupboss/master
fix: avoid errors in browser console
Diffstat (limited to 'src')
| -rw-r--r-- | src/api/connections.ts | 10 | ||||
| -rw-r--r-- | src/components/APIConfig.tsx | 12 | ||||
| -rw-r--r-- | src/components/Connections.tsx | 7 | ||||
| -rw-r--r-- | src/components/TrafficNow.tsx | 2 | ||||
| -rw-r--r-- | src/store/app.ts | 2 |
5 files changed, 18 insertions, 15 deletions
diff --git a/src/api/connections.ts b/src/api/connections.ts index fd0901b..772ec4a 100644 --- a/src/api/connections.ts +++ b/src/api/connections.ts @@ -51,11 +51,11 @@ function appendData(s: string) { let o: ConnectionsData; try { o = JSON.parse(s); - o.connections.forEach(conn => { - let m = conn.metadata; + o.connections?.forEach((conn) => { + const m = conn.metadata; if (m.process == null) { if (m.processPath != null) { - m.process = m.processPath.replace(/^.*[/\\](.*)$/, "$1"); + m.process = m.processPath.replace(/^.*[/\\](.*)$/, '$1'); } } }); @@ -86,12 +86,12 @@ export function fetchData( const ws = new WebSocket(url); ws.addEventListener('error', () => { wsState = 3; - subscribers.forEach((s) => s.onClose()); + subscribers.forEach((s) => s.onClose?.()); subscribers.length = 0; }); ws.addEventListener('close', () => { wsState = 3; - subscribers.forEach((s) => s.onClose()); + subscribers.forEach((s) => s.onClose?.()); subscribers.length = 0; }); ws.addEventListener('message', (event) => appendData(event.data)); diff --git a/src/components/APIConfig.tsx b/src/components/APIConfig.tsx index 9491d2f..c67a157 100644 --- a/src/components/APIConfig.tsx +++ b/src/components/APIConfig.tsx @@ -86,11 +86,13 @@ function APIConfig({ dispatch }) { const detectApiServer = async () => { // if there is already a clash API server at `/`, just use it as default value const res = await fetch('/'); - res.json().then((data) => { - if (data['hello'] === 'clash') { - setBaseURL(window.location.origin); - } - }); + if (res.headers.get('content-type')?.includes('application/json')) { + res.json().then((data) => { + if (data['hello'] === 'clash') { + setBaseURL(window.location.origin); + } + }); + } }; useEffect(() => { detectApiServer(); diff --git a/src/components/Connections.tsx b/src/components/Connections.tsx index cff9a26..b76af79 100644 --- a/src/components/Connections.tsx +++ b/src/components/Connections.tsx @@ -313,9 +313,10 @@ function Conn({ apiConfig }) { ({ connections }) => { const prevConnsKv = arrayToIdKv(prevConnsRef.current); const now = Date.now(); - const x = connections.map((c: ConnectionItem) => - formatConnectionDataItem(c, prevConnsKv, now, sourceMap) - ); + const x = + connections?.map((c: ConnectionItem) => + formatConnectionDataItem(c, prevConnsKv, now, sourceMap) + ) ?? []; const closed = []; for (const c of prevConnsRef.current) { const idx = x.findIndex((conn: ConnectionItem) => conn.id === c.id); diff --git a/src/components/TrafficNow.tsx b/src/components/TrafficNow.tsx index ae69910..ad7d910 100644 --- a/src/components/TrafficNow.tsx +++ b/src/components/TrafficNow.tsx @@ -74,7 +74,7 @@ function useConnection(apiConfig) { setState({ upTotal: prettyBytes(uploadTotal), dlTotal: prettyBytes(downloadTotal), - connNumber: connections.length, + connNumber: connections ? connections.length : 0, mUsage: prettyBytes(memory), }); }, diff --git a/src/store/app.ts b/src/store/app.ts index 26f311f..fb5fc2d 100644 --- a/src/store/app.ts +++ b/src/store/app.ts @@ -179,7 +179,7 @@ function parseConfigQueryString() { const qs = search.replace(/^\?/, '').split('&'); for (let i = 0; i < qs.length; i++) { const [k, v] = qs[i].split('='); - collector[k] = encodeURIComponent(v); + collector[k] = decodeURIComponent(v); } return collector; } |
