Skip to content

Commit

Permalink
fix(script and app): 修复脚本题库检测BUG,优化软件更新提示
Browse files Browse the repository at this point in the history
  • Loading branch information
enncy committed Apr 5, 2023
1 parent 7cc5347 commit 7e217d1
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 76 deletions.
2 changes: 1 addition & 1 deletion packages/app/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@ocsjs/app",
"version": "2.0.6",
"version": "2.1.0",
"description": "app package of ocs",
"main": "./lib/index.js",
"types": "./lib/types.d.ts",
Expand Down
2 changes: 1 addition & 1 deletion packages/app/src/tasks/updater.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export async function updater(win: BrowserWindow) {
message: [
`检测到最新版本:${tag}`,
...(feat.length ? ['新增功能:', feat.join('\n')] : []),
...[fix.length ? ['修复BUG: ', fix.join('\n')] : []],
...(fix.length ? ['修复BUG: ', fix.join('\n')] : []),
'',
'是否更新 ?'
].join('\n'),
Expand Down
148 changes: 74 additions & 74 deletions packages/scripts/src/projects/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -241,83 +241,83 @@ export const CommonProject = Project.create({
}
},
onrender({ panel }) {
panel.body.replaceChildren(el('hr'));
const refresh = el(
'button',
{ className: 'base-style-button', disabled: this.cfg.answererWrappers.length === 0 },
'🔄️刷新题库状态'
);
refresh.onclick = () => {
updateState();
};
const tableContainer = el('div');
panel.body.append(refresh, tableContainer);

// 更新题库状态
const updateState = async () => {
// 清空元素
tableContainer.replaceChildren();
refresh.toggleAttribute('disabled');
refresh.textContent = '🚫正在加载...';

let loadedCount = 0;

if (this.cfg.answererWrappers.length) {
const table = el('table');
table.style.width = '100%';
this.cfg.answererWrappers.forEach(async (item) => {
const t = Date.now();
let success = false;
let error;
const res = await Promise.race([
(async () => {
try {
return await request(new URL(item.url).origin + '/?t=' + t, {
type: 'GM_xmlhttpRequest',
method: 'get',
responseType: 'text',
headers: {
'Content-Type': 'text/html'
}
});
} catch (err) {
error = err;
// 因为需要用到 GM_xhr 所以判断是否处于用户脚本环境
if ($gm.getInfos() !== undefined) {
panel.body.replaceChildren(el('hr'));
const refresh = el(
'button',
{ className: 'base-style-button', disabled: this.cfg.answererWrappers.length === 0 },
'🔄️刷新题库状态'
);
refresh.onclick = () => {
updateState();
};
const tableContainer = el('div');
panel.body.append(refresh, tableContainer);

// 更新题库状态
const updateState = async () => {
// 清空元素
tableContainer.replaceChildren();
refresh.toggleAttribute('disabled');
refresh.textContent = '🚫正在加载...';

let loadedCount = 0;

if (this.cfg.answererWrappers.length) {
const table = el('table');
table.style.width = '100%';
this.cfg.answererWrappers.forEach(async (item) => {
const t = Date.now();
let success = false;
let error;
const res = await Promise.race([
(async () => {
try {
return await request(new URL(item.url).origin + '/?t=' + t, {
type: 'GM_xmlhttpRequest',
method: 'get',
responseType: 'text',
headers: {
'Content-Type': 'text/html'
}
});
} catch (err) {
error = err;
return false;
}
})(),
(async () => {
await $.sleep(10 * 1000);
return false;
}
})(),
(async () => {
await $.sleep(10 * 1000);
return false;
})()
]);
if (res) {
success = true;
} else {
success = false;
}
})()
]);
if (res) {
success = true;
} else {
success = false;
}

const body = el('tbody');
body.append(el('td', item.name));
body.append(el('td', success ? '连接成功🟢' : error ? '连接失败🔴' : '连接超时🟡'));
body.append(el('td', `延迟 : ${success ? Date.now() - t : '---'}/ms`));
table.append(body);
loadedCount++;

if (loadedCount === this.cfg.answererWrappers.length) {
setTimeout(() => {
refresh.textContent = '🔄️刷新题库状态';
refresh.toggleAttribute('disabled');
}, 3000);
}
});
tableContainer.append(table);
} else {
tableContainer.append(el('div', '暂无任何题库...'));
}
};
const body = el('tbody');
body.append(el('td', item.name));
body.append(el('td', success ? '连接成功🟢' : error ? '连接失败🔴' : '连接超时🟡'));
body.append(el('td', `延迟 : ${success ? Date.now() - t : '---'}/ms`));
table.append(body);
loadedCount++;

if (loadedCount === this.cfg.answererWrappers.length) {
setTimeout(() => {
refresh.textContent = '🔄️刷新题库状态';
refresh.toggleAttribute('disabled');
}, 3000);
}
});
tableContainer.append(table);
} else {
tableContainer.append(el('div', '暂无任何题库...'));
}
};

// 因为需要用到 GM_xhr 所以判断是否处于用户脚本环境
if ($gm.getInfos() !== undefined) {
updateState();
this.onConfigChange('answererWrappers', () => {
updateState();
Expand Down

0 comments on commit 7e217d1

Please sign in to comment.