From 9568e050f1be6151fc9fd51c195fd58a5a3d3d4f Mon Sep 17 00:00:00 2001 From: Comzyh Date: Mon, 14 Oct 2019 21:42:41 +0800 Subject: [PATCH] Detect clash API server at `/` when log in. (#258) --- src/components/APIConfig.js | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/components/APIConfig.js b/src/components/APIConfig.js index 236bd1d..42753da 100644 --- a/src/components/APIConfig.js +++ b/src/components/APIConfig.js @@ -21,8 +21,21 @@ function APIConfig2() { const actions = useActions({ updateClashAPIConfig }); const contentEl = useRef(null); + + 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') { + setHostname(window.location.hostname); + setPort(window.location.port); + } + }); + }; + useEffect(() => { contentEl.current.focus(); + detectApiServer(); }, []); const handleInputOnChange = e => {