自定义源列显示源版本号、作者名字,优化自定义源脚本数据存储
parent
ff98e06afa
commit
3ba51fd6fe
|
@ -15,6 +15,7 @@
|
||||||
### 优化
|
### 优化
|
||||||
|
|
||||||
- 更新zh-tw翻译
|
- 更新zh-tw翻译
|
||||||
|
- 自定义源列显示源版本号、作者名字
|
||||||
|
|
||||||
### 修复
|
### 修复
|
||||||
|
|
||||||
|
|
|
@ -13,7 +13,7 @@ declare namespace LX {
|
||||||
type UserApiSources = Record<LX.Source, UserApiSourceInfo>
|
type UserApiSources = Record<LX.Source, UserApiSourceInfo>
|
||||||
|
|
||||||
|
|
||||||
interface UserApiInfo {
|
interface UserApiInfoFull {
|
||||||
id: string
|
id: string
|
||||||
name: string
|
name: string
|
||||||
description: string
|
description: string
|
||||||
|
@ -25,6 +25,8 @@ declare namespace LX {
|
||||||
sources?: UserApiSources
|
sources?: UserApiSources
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type UserApiInfo = Omit<UserApiInfoFull, 'script'>
|
||||||
|
|
||||||
interface UserApiStatus {
|
interface UserApiStatus {
|
||||||
status: boolean
|
status: boolean
|
||||||
message?: string
|
message?: string
|
||||||
|
|
|
@ -1,2 +1,2 @@
|
||||||
|
|
||||||
export const userApis: LX.UserApi.UserApiInfo[] = []
|
export const userApis: LX.UserApi.UserApiInfoFull[] = []
|
||||||
|
|
|
@ -6,9 +6,9 @@ let userApiId: string | null
|
||||||
|
|
||||||
export const getApiList = getUserApis
|
export const getApiList = getUserApis
|
||||||
|
|
||||||
export const importApi = (script: string): LX.UserApi.ImportUserApi => {
|
export const importApi = async(script: string): Promise<LX.UserApi.ImportUserApi> => {
|
||||||
return {
|
return {
|
||||||
apiInfo: handleImportApi(script),
|
apiInfo: await handleImportApi(script),
|
||||||
apiList: getUserApis(),
|
apiList: getUserApis(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,6 +5,7 @@ import path from 'node:path'
|
||||||
import { openDevTools as handleOpenDevTools } from '@main/utils'
|
import { openDevTools as handleOpenDevTools } from '@main/utils'
|
||||||
import { encodePath } from '@common/utils/electron'
|
import { encodePath } from '@common/utils/electron'
|
||||||
import USER_API_RENDERER_EVENT_NAME from './rendererEvent/name'
|
import USER_API_RENDERER_EVENT_NAME from './rendererEvent/name'
|
||||||
|
import { getScript } from './utils'
|
||||||
|
|
||||||
let browserWindow: Electron.BrowserWindow | null = null
|
let browserWindow: Electron.BrowserWindow | null = null
|
||||||
|
|
||||||
|
@ -91,8 +92,8 @@ export const createWindow = async(userApi: LX.UserApi.UserApiInfo) => {
|
||||||
// const randomNum = Math.random().toString().substring(2, 10)
|
// const randomNum = Math.random().toString().substring(2, 10)
|
||||||
await browserWindow.loadURL('data:text/html;charset=UTF-8,' + encodeURIComponent(html))
|
await browserWindow.loadURL('data:text/html;charset=UTF-8,' + encodeURIComponent(html))
|
||||||
|
|
||||||
browserWindow.on('ready-to-show', () => {
|
browserWindow.on('ready-to-show', async() => {
|
||||||
sendEvent(USER_API_RENDERER_EVENT_NAME.initEnv, userApi)
|
sendEvent(USER_API_RENDERER_EVENT_NAME.initEnv, { ...userApi, script: await getScript(userApi.id) })
|
||||||
})
|
})
|
||||||
|
|
||||||
// global.modules.userApiWindow.loadFile(join(dir, 'renderer/user-api.html'))
|
// global.modules.userApiWindow.loadFile(join(dir, 'renderer/user-api.html'))
|
||||||
|
@ -106,7 +107,7 @@ export const closeWindow = async() => {
|
||||||
browserWindow.webContents.session.clearStorageData(),
|
browserWindow.webContents.session.clearStorageData(),
|
||||||
browserWindow.webContents.session.clearCache(),
|
browserWindow.webContents.session.clearCache(),
|
||||||
])
|
])
|
||||||
browserWindow.destroy()
|
browserWindow?.destroy()
|
||||||
browserWindow = null
|
browserWindow = null
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,20 +1,52 @@
|
||||||
import { userApis as defaultUserApis } from './config'
|
import { userApis as defaultUserApis } from './config'
|
||||||
import { STORE_NAMES } from '@common/constants'
|
import { STORE_NAMES } from '@common/constants'
|
||||||
import getStore from '@main/utils/store'
|
import getStore from '@main/utils/store'
|
||||||
|
import zlib from 'node:zlib'
|
||||||
|
|
||||||
let userApis: LX.UserApi.UserApiInfo[] | null
|
let userApis: LX.UserApi.UserApiInfo[] | null
|
||||||
|
let scripts = new Map<string, string>()
|
||||||
|
|
||||||
|
const saveData = () => {
|
||||||
|
getStore(STORE_NAMES.USER_API).set('userApis', userApis!.map(api => {
|
||||||
|
return {
|
||||||
|
...api,
|
||||||
|
script: scripts.get(api.id),
|
||||||
|
}
|
||||||
|
}))
|
||||||
|
}
|
||||||
|
|
||||||
export const getUserApis = (): LX.UserApi.UserApiInfo[] => {
|
export const getUserApis = (): LX.UserApi.UserApiInfo[] => {
|
||||||
const electronStore_userApi = getStore(STORE_NAMES.USER_API)
|
|
||||||
if (userApis) return userApis
|
if (userApis) return userApis
|
||||||
userApis = electronStore_userApi.get('userApis') as LX.UserApi.UserApiInfo[]
|
|
||||||
if (!userApis) {
|
const electronStore_userApi = getStore(STORE_NAMES.USER_API)
|
||||||
userApis = defaultUserApis
|
let infoFull = electronStore_userApi.get('userApis') as LX.UserApi.UserApiInfoFull[]
|
||||||
|
let requiredUpdate = false
|
||||||
|
if (infoFull) {
|
||||||
|
for (let i = 0; i < infoFull.length; i++) {
|
||||||
|
const api = infoFull[i]
|
||||||
|
if (api.version != null) continue
|
||||||
|
requiredUpdate ||= true
|
||||||
|
try {
|
||||||
|
infoFull.splice(i, 1, {
|
||||||
|
...parseScriptInfo(api.script),
|
||||||
|
...api,
|
||||||
|
})
|
||||||
|
} catch (e) {
|
||||||
|
infoFull.splice(i, 1)
|
||||||
|
i--
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
infoFull = defaultUserApis
|
||||||
electronStore_userApi.set('userApis', userApis)
|
electronStore_userApi.set('userApis', userApis)
|
||||||
}
|
}
|
||||||
for (const api of userApis) {
|
userApis = infoFull.map(api => {
|
||||||
if (api.allowShowUpdateAlert == null) api.allowShowUpdateAlert = false
|
if (api.allowShowUpdateAlert == null) api.allowShowUpdateAlert = false
|
||||||
}
|
const { script, ...info } = api
|
||||||
|
scripts.set(api.id, script)
|
||||||
|
return info
|
||||||
|
})
|
||||||
|
if (requiredUpdate) saveData()
|
||||||
return userApis
|
return userApis
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -46,22 +78,47 @@ const matchInfo = (scriptInfo: string) => {
|
||||||
|
|
||||||
return infos as Record<keyof typeof INFO_NAMES, string>
|
return infos as Record<keyof typeof INFO_NAMES, string>
|
||||||
}
|
}
|
||||||
export const importApi = (script: string): LX.UserApi.UserApiInfo => {
|
const parseScriptInfo = (script: string) => {
|
||||||
const result = /^\/\*[\S|\s]+?\*\//.exec(script)
|
const result = /^\/\*[\S|\s]+?\*\//.exec(script)
|
||||||
if (!result) throw new Error('无效的自定义源文件')
|
if (!result) throw new Error('无效的自定义源文件')
|
||||||
|
|
||||||
let scriptInfo = matchInfo(result[0])
|
let scriptInfo = matchInfo(result[0])
|
||||||
|
|
||||||
scriptInfo.name ||= `user_api_${new Date().toLocaleString()}`
|
scriptInfo.name ||= `user_api_${new Date().toLocaleString()}`
|
||||||
|
return scriptInfo
|
||||||
|
}
|
||||||
|
const deflateScript = async(script: string) => new Promise<string>((resolve, reject) => {
|
||||||
|
zlib.deflate(Buffer.from(script, 'utf8'), (err, buf) => {
|
||||||
|
if (err) {
|
||||||
|
reject(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
resolve('gz_' + buf.toString('base64'))
|
||||||
|
})
|
||||||
|
})
|
||||||
|
const inflateScript = async(script: string) => new Promise<string>((resolve, reject) => {
|
||||||
|
if (script.startsWith('gz_')) {
|
||||||
|
zlib.inflate(Buffer.from(script.substring(3), 'base64'), (err, buf) => {
|
||||||
|
if (err) {
|
||||||
|
reject(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
resolve(buf.toString('utf8'))
|
||||||
|
})
|
||||||
|
} else resolve(script)
|
||||||
|
})
|
||||||
|
export const importApi = async(scriptRaw: string): Promise<LX.UserApi.UserApiInfo> => {
|
||||||
|
let scriptInfo = parseScriptInfo(scriptRaw)
|
||||||
const apiInfo = {
|
const apiInfo = {
|
||||||
id: `user_api_${Math.random().toString().substring(2, 5)}_${Date.now()}`,
|
id: `user_api_${Math.random().toString().substring(2, 5)}_${Date.now()}`,
|
||||||
...scriptInfo,
|
...scriptInfo,
|
||||||
script,
|
|
||||||
allowShowUpdateAlert: true,
|
allowShowUpdateAlert: true,
|
||||||
}
|
}
|
||||||
userApis ??= []
|
userApis ??= []
|
||||||
userApis.push(apiInfo)
|
userApis.push(apiInfo)
|
||||||
getStore(STORE_NAMES.USER_API).set('userApis', userApis)
|
const script = await deflateScript(scriptRaw)
|
||||||
|
scripts.set(apiInfo.id, script)
|
||||||
|
saveData()
|
||||||
return apiInfo
|
return apiInfo
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -69,16 +126,21 @@ export const removeApi = (ids: string[]) => {
|
||||||
if (!userApis) return
|
if (!userApis) return
|
||||||
for (let index = userApis.length - 1; index > -1; index--) {
|
for (let index = userApis.length - 1; index > -1; index--) {
|
||||||
if (ids.includes(userApis[index].id)) {
|
if (ids.includes(userApis[index].id)) {
|
||||||
|
scripts.delete(userApis[index].id)
|
||||||
userApis.splice(index, 1)
|
userApis.splice(index, 1)
|
||||||
ids.splice(index, 1)
|
ids.splice(index, 1)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
getStore(STORE_NAMES.USER_API).set('userApis', userApis)
|
saveData()
|
||||||
}
|
}
|
||||||
|
|
||||||
export const setAllowShowUpdateAlert = (id: string, enable: boolean) => {
|
export const setAllowShowUpdateAlert = (id: string, enable: boolean) => {
|
||||||
const targetApi = userApis?.find(api => api.id == id)
|
const targetApi = userApis?.find(api => api.id == id)
|
||||||
if (!targetApi) return
|
if (!targetApi) return
|
||||||
targetApi.allowShowUpdateAlert = enable
|
targetApi.allowShowUpdateAlert = enable
|
||||||
getStore(STORE_NAMES.USER_API).set('userApis', userApis)
|
saveData()
|
||||||
|
}
|
||||||
|
|
||||||
|
export const getScript = async(id: string) => {
|
||||||
|
return inflateScript(scripts.get(id) ?? '')
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,7 +5,10 @@ material-modal(:show="modelValue" bg-close teleport="#view" @close="handleClose"
|
||||||
ul.scroll(v-if="apiList.length" :class="$style.content")
|
ul.scroll(v-if="apiList.length" :class="$style.content")
|
||||||
li(v-for="(api, index) in apiList" :key="api.id" :class="[$style.listItem, {[$style.active]: appSetting['common.apiSource'] == api.id}]")
|
li(v-for="(api, index) in apiList" :key="api.id" :class="[$style.listItem, {[$style.active]: appSetting['common.apiSource'] == api.id}]")
|
||||||
div(:class="$style.listLeft")
|
div(:class="$style.listLeft")
|
||||||
h3 {{ api.name }}
|
h3
|
||||||
|
| {{ api.name }}
|
||||||
|
span(v-if="api.version") {{ /^\d/.test(api.version) ? `v${api.version}` : api.version }}
|
||||||
|
span(v-if="api.author") {{ api.author }}
|
||||||
p {{ api.description }}
|
p {{ api.description }}
|
||||||
div
|
div
|
||||||
base-checkbox(:id="`user_api_${api.id}`" v-model="api.allowShowUpdateAlert" :class="$style.checkbox" :label="$t('user_api__allow_show_update_alert')" @change="handleChangeAllowUpdateAlert(api, $event)")
|
base-checkbox(:id="`user_api_${api.id}`" v-model="api.allowShowUpdateAlert" :class="$style.checkbox" :label="$t('user_api__allow_show_update_alert')" @change="handleChangeAllowUpdateAlert(api, $event)")
|
||||||
|
@ -145,7 +148,7 @@ export default {
|
||||||
flex-flow: row nowrap;
|
flex-flow: row nowrap;
|
||||||
align-items: center;
|
align-items: center;
|
||||||
transition: background-color 0.2s ease;
|
transition: background-color 0.2s ease;
|
||||||
padding: 10px;
|
padding: 15px 10px;
|
||||||
border-radius: @radius-border;
|
border-radius: @radius-border;
|
||||||
&:hover {
|
&:hover {
|
||||||
background-color: var(--color-primary-background-hover);
|
background-color: var(--color-primary-background-hover);
|
||||||
|
@ -157,6 +160,11 @@ export default {
|
||||||
font-size: 15px;
|
font-size: 15px;
|
||||||
color: var(--color-font);
|
color: var(--color-font);
|
||||||
word-break: break-all;
|
word-break: break-all;
|
||||||
|
span {
|
||||||
|
font-size: 12px;
|
||||||
|
color: var(--color-font-label);
|
||||||
|
margin-left: 6px;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
p {
|
p {
|
||||||
margin-top: 5px;
|
margin-top: 5px;
|
||||||
|
|
Loading…
Reference in New Issue