Skip to content

Commit

Permalink
feat(core and app): 新增软件服务端,使脚本可读取软件信息。
Browse files Browse the repository at this point in the history
  • Loading branch information
enncy committed Jun 4, 2022
1 parent 526dbd6 commit 6f4ff54
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 1 deletion.
21 changes: 21 additions & 0 deletions packages/app/src/tasks/startup.server.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@

import express from 'express';
import Store from 'electron-store';
import getPort from 'get-port';
import { Logger } from '../logger';
const logger = Logger('server');

export async function startupServer() {
const app = express();
const store = new Store();

app.get('/setting', (req, res) => {
res.json(store.store.setting);
});

const port = await getPort({ port: [15319, 153120, 15321] });

app.listen(port, () => {
logger.info('服务启动成功,端口:', port);
});
}
19 changes: 19 additions & 0 deletions packages/core/src/core/utils/common.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { useContext } from '../../store';
import { DefineScript, GlobPattern, ScriptPanel, ScriptRoute } from '../define.script';
import { request } from './request';

export async function sleep(period: number): Promise<void> {
return new Promise((resolve) => {
Expand Down Expand Up @@ -114,3 +115,21 @@ export function useUnsafeWindow() {
// eslint-disable-next-line no-undef
return typeof unsafeWindow !== 'undefined' ? unsafeWindow : undefined;
}

/**
* 获取可用设置
*/
export async function getRemoteSetting(port: number) {
let count = 3;
while (count > 0) {
try {
const res = await request(`http://localhost:${port}/setting`, {
type: 'GM_xmlhttpRequest',
method: 'get',
contentType: 'json'
});
return res;
} catch { }
count--;
}
}
16 changes: 15 additions & 1 deletion packages/core/src/script/common/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { createNote } from '../../components';
import { defineScript } from '../../core/define.script';
import { onComplete, onInteractive, useUnsafeWindow } from '../../core/utils';
import { getRemoteSetting, onComplete, onInteractive, useUnsafeWindow } from '../../core/utils';
import { useSettings } from '../../store';

const supports = ['*'];

Expand Down Expand Up @@ -58,6 +59,19 @@ export const CommonScript = defineScript({
setTimeout(() => enableCopy(), 3000);
});
}
}, {
name: '获取软件题库配置脚本',
url: supports,
async onload() {
const { common } = useSettings();
if (common.answererWrappers.length) {
const setting = await getRemoteSetting(15319);
if (setting?.answererWrappers) {
const { common } = useSettings();
common.answererWrappers = setting.answererWrappers;
}
}
}
}
],
panels: [
Expand Down

0 comments on commit 6f4ff54

Please sign in to comment.