Feature: query string override for API config (#71)

close #24
This commit is contained in:
Zisu Zhang 2021-06-08 21:46:32 +08:00 committed by GitHub
parent 47be9c2469
commit 15e97f0200
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 9 additions and 5 deletions

View File

@ -12,6 +12,10 @@ export function removeLocalStorageItem (key: string) {
export function noop () {} export function noop () {}
export function getSearchParam(key: string) {
return new URLSearchParams(window.location.search).get(key)
}
/** /**
* to return Promise<[T, Error]> * to return Promise<[T, Error]>
* @param {Promise<T>} promise * @param {Promise<T>} promise

View File

@ -1,5 +1,5 @@
import axios from 'axios' import axios from 'axios'
import { getLocalStorageItem, to } from '@lib/helper' import { getLocalStorageItem, getSearchParam, to } from '@lib/helper'
import { isClashX, jsBridge } from '@lib/jsBridge' import { isClashX, jsBridge } from '@lib/jsBridge'
import { createAsyncSingleton } from '@lib/asyncSingleton' import { createAsyncSingleton } from '@lib/asyncSingleton'
import { Log } from '@models/Log' import { Log } from '@models/Log'
@ -119,10 +119,10 @@ export async function getExternalControllerConfig () {
} }
} }
const hostname = getLocalStorageItem('externalControllerAddr', url?.hostname ?? '127.0.0.1') const hostname = getSearchParam('host') ?? getLocalStorageItem('externalControllerAddr', url?.hostname ?? '127.0.0.1')
const port = getLocalStorageItem('externalControllerPort', url?.port ?? '9090') const port = getSearchParam('port') ?? getLocalStorageItem('externalControllerPort', url?.port ?? '9090')
const secret = getLocalStorageItem('secret', url?.username ?? '') const secret = getSearchParam('secret') ?? getLocalStorageItem('secret', url?.username ?? '')
const protocol = hostname === '127.0.0.1' ? 'http:' : (url?.protocol ?? window.location.protocol) const protocol = getSearchParam('protocol') ?? hostname === '127.0.0.1' ? 'http:' : (url?.protocol ?? window.location.protocol)
if (!hostname || !port) { if (!hostname || !port) {
throw new Error('can\'t get hostname or port') throw new Error('can\'t get hostname or port')