Skip to content

Commit

Permalink
Merge pull request #26 from StarfishC/main
Browse files Browse the repository at this point in the history
upgrade plugin-sync-singbox-configuration-gists
  • Loading branch information
Ayideyia authored Jan 26, 2025
2 parents 17d91f2 + 01de4ff commit ef5cf0d
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 40 deletions.
45 changes: 16 additions & 29 deletions plugins/GFS/plugin-sync-singbox-configuration-gists.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,43 +4,30 @@ const onRun = async () => {

const onTask = async () => {
await updateGist();
return '更新 Gist.'
return '更新配置config.json到Gist.'
};

const updateGist = async () => {
if (!Plugin.GistId) throw '未配置GIST ID'
const store = Plugins.useProfilesStore()
let profile = null
if (!Plugin.ProfileName) {
profile = await Plugins.picker.single(
'请选择要同步的配置',
store.profiles.map((v) => ({
label: v.name,
value: v
})),
[]
)
} else {
profile = store.profiles.find(item => item.name === Plugin.ProfileName)
if (!profile)
throw "未找到配置:" + Plugin.ProfileName
}
const configJsonContent = await Plugins.generateConfig(profile)
const { id: messageId } = Plugins.message.info('正在更新 Gist...', 60 * 1000)

try {
const updatedGist = await updateGistFile(Plugin.GistId, JSON.stringify(configJsonContent, null, 4))
Plugins.message.update(messageId, `Gist 更新成功: ${updatedGist}`, 'success')
} catch (error) {
Plugins.message.update(messageId, `Gist 更新失败: ${error}`, 'error')
} finally {
await Plugins.sleep(1500).then(() => Plugins.message.destroy(messageId))
for (const profile of store.profiles)
{
const configJsonContent = await Plugins.generateConfig(profile)
const { id: messageId } = Plugins.message.info(`正在更新 [ ${profile.name} ]`, 60 * 1000)
try {
const updatedGist = await updateGistFile(profile.name, Plugin.GistId, JSON.stringify(configJsonContent, null, 4))
Plugins.message.update(messageId, `更新 [ ${profile.name} ] ${updatedGist}`, 'success')
} catch (error) {
Plugins.message.update(messageId, `更新 [ ${profile.name} ] ${error}`, 'error')
} finally {
await Plugins.sleep(1500).then(() => Plugins.message.destroy(messageId))
}
}
}

async function updateGistFile(gistId, configJsonContent) {
async function updateGistFile(name, gistId, configJsonContent) {
if (!Plugin.Authorization) throw '未配置TOKEN'

let jsonName = name + '.json'
const { body } = await Plugins.HttpPatch(
`https://api.github.com/gists/${gistId}`,
{
Expand All @@ -53,7 +40,7 @@ async function updateGistFile(gistId, configJsonContent) {
},
{
files: {
'config.json': {
[jsonName]: {
content: configJsonContent
}
}
Expand Down
13 changes: 2 additions & 11 deletions plugins/gfs.json
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,8 @@
},
{
"id": "plugin-sync-singbox-configuration-gists",
"name": "同步config.json",
"description": "将指定的配置生成config.json后同步到Gists。Supported by: StarfishC",
"name": "一键同步config.json",
"description": "将所有配置生成的config.json一键同步到Gists。Supported by: StarfishC",
"type": "Http",
"url": "https://raw.githubusercontent.com/GUI-for-Cores/Plugin-Hub/main/plugins/GFS/plugin-sync-singbox-configuration-gists.js",
"path": "data/plugins/plugin-sync-singbox-configuration-gists.js",
Expand All @@ -158,15 +158,6 @@
"component": "Input",
"value": "",
"options": []
},
{
"id": "ID_ya7dggls",
"title": "PROFILE NAME",
"description": "[选填] 配置名。填写时不弹出配置选择框,否则弹出配置选择框",
"key": "ProfileName",
"component": "Input",
"value": "",
"options": []
}
],
"disabled": false,
Expand Down

0 comments on commit ef5cf0d

Please sign in to comment.