Skip to content

Commit 6b19377

Browse files
- 修改样式
- 优化更新检查逻辑
1 parent a5350c0 commit 6b19377

11 files changed

+63
-46
lines changed

.eslintrc.yml

+10-9
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ plugins:
77
- prettier
88
extends:
99
- eslint:recommended
10-
- plugin:@typescript-eslint/eslint-recommended
1110
- plugin:@typescript-eslint/recommended
1211
- plugin:@typescript-eslint/recommended-requiring-type-checking
1312
- prettier
@@ -19,24 +18,26 @@ rules:
1918
'@typescript-eslint/explicit-function-return-type':
2019
- 2
2120
- allowExpressions: true
22-
'@typescript-eslint/no-extra-non-null-assertion': [2]
2321
'@typescript-eslint/no-extraneous-class':
2422
- 1
2523
- allowWithDecorator: true
2624
allowStaticOnly: true
27-
'@typescript-eslint/no-floating-promises': [1]
2825
'@typescript-eslint/no-parameter-properties':
2926
- 1
30-
- allows:
31-
- protected readonly
32-
- private readonly
33-
- public readonly
34-
- readonly
27+
- allows: [protected readonly, private readonly, public readonly, readonly]
3528
'@typescript-eslint/no-throw-literal': [2]
3629
'no-unused-vars': [0]
30+
'@typescript-eslint/no-unused-vars':
31+
- 1
32+
- varsIgnorePattern: ^_
33+
argsIgnorePattern: ^_
3734
'@typescript-eslint/prefer-for-of': [1]
3835
'@typescript-eslint/prefer-function-type': [1]
3936
'@typescript-eslint/prefer-nullish-coalescing': [1]
4037
'@typescript-eslint/prefer-optional-chain': [1]
41-
no-void: [1]
38+
'@typescript-eslint/ban-types':
39+
- 1
40+
- types:
41+
object: false
42+
no-void: [1, { allowAsStatement: true }]
4243
eqeqeq: [1, smart]

CHANGELOG.md

+5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
# 更新日志
22

3+
## 1.3.1
4+
5+
- 修改样式
6+
- 优化更新检查逻辑
7+
38
## 1.3.0
49

510
- 启动时自动检查扩展更新

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "ehsyringe",
33
"displayName": "EhSyringe",
4-
"version": "1.3.0",
4+
"version": "1.3.1",
55
"description": "E 站注射器,将中文翻译注入到 E 站体内。",
66
"author": "EhTagTranslation",
77
"main": "syringe.js",

src/background/extension-updater.ts

+15-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,15 @@
11
import { browser } from 'webextension-polyfill-ts';
22
import { logger } from '../tool/log';
3+
import { chromeMessage } from '../tool/chrome-message';
4+
5+
let checked = false;
6+
7+
async function update(): Promise<void> {
8+
if (checked) {
9+
logger.log('检查插件更新', '跳过');
10+
return;
11+
}
312

4-
async function main(): Promise<void> {
513
const info = await browser.management.getSelf();
614
const response = await fetch('https://api.github.com/repos/EhTagTranslation/EhSyringe/releases/latest', {
715
headers: {
@@ -27,6 +35,11 @@ async function main(): Promise<void> {
2735
}
2836
});
2937
}
38+
checked = true;
39+
}
40+
41+
function init(): void {
42+
chromeMessage.listener('ext-update', () => update());
3043
}
3144

32-
export const result = main();
45+
export const result = init();

src/background/updater.ts

+10-1
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,21 @@ class Updater {
3131

3232
private loadLock = false;
3333

34+
private checked = false;
35+
3436
constructor() {
3537
chromeMessage.listener('auto-update', async (force) => {
38+
if (this.checked && !force) {
39+
logger.log('自动更新', '跳过');
40+
return false;
41+
}
3642
const version = await this.checkVersion(true);
3743
if (version?.new && (version.new !== version.old || force)) {
3844
await this.update();
45+
logger.log('自动更新', '有新版本并更新');
3946
return true;
4047
}
48+
logger.log('自动更新', '没有新版本');
4149
return false;
4250
});
4351
}
@@ -112,6 +120,7 @@ class Updater {
112120
githubRelease: info,
113121
timestamp: new Date().getTime(),
114122
});
123+
this.checked = true;
115124
return this.lastCheckData.value;
116125
}
117126

@@ -148,7 +157,7 @@ class Updater {
148157
const data = await downloadFile(url, undefined, (event) => {
149158
if (event.lengthComputable) {
150159
const percent = Math.floor((event.loaded / event.total) * 100);
151-
this.pushDownloadStatus({ info: percent + '%', progress: percent });
160+
this.pushDownloadStatus({ info: `${percent}%`, progress: percent });
152161
badgeLoading.set(percent.toFixed(0), '#4A90E2', 1);
153162
}
154163
});

src/plugin/auto-update/auto-update.ts

+3-19
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,10 @@
1-
import { browser } from 'webextension-polyfill-ts';
2-
31
import { chromeMessage } from '../../tool/chrome-message';
42
import { config } from '../../tool/config-manage';
5-
import { logger } from '../../tool/log';
6-
import { dateDiff } from '../../tool/tool';
7-
8-
// 1 day
9-
const autoCheckInterval = 1000 * 60 * 60 * 24;
103

114
export const autoUpdateInit = async (): Promise<void> => {
5+
void chromeMessage.send('ext-update', undefined);
6+
127
const conf = await config.get();
138
if (!conf.autoUpdate) return;
14-
logger.log('自动更新');
15-
16-
const { lastCheckTime } = await browser.storage.local.get('lastCheckTime');
17-
18-
const time = new Date().getTime();
19-
const needCheck = time - autoCheckInterval > (lastCheckTime || 0);
20-
logger.log('上次自动更新检查', dateDiff(lastCheckTime), new Date(lastCheckTime), needCheck ? '开始检查' : '跳过');
21-
if (needCheck) {
22-
await browser.storage.local.set({ lastCheckTime: time });
23-
const updated = await chromeMessage.send('auto-update', false);
24-
logger.log('自动更新结束', updated ? '有新版本并更新' : '没有新版本');
25-
}
9+
void chromeMessage.send('auto-update', false);
2610
};

src/plugin/introduce/introduce.less

+7
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,13 @@ div#gd5 {
8282
&::-webkit-scrollbar {
8383
width: 2px;
8484
}
85+
abbr[title] {
86+
padding: 0 1px;
87+
&::after {
88+
content: ' (' attr(title) ')';
89+
font-size: 90%;
90+
}
91+
}
8592
}
8693
.ehs-href {
8794
flex: none;

src/tool/chrome-message.ts

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ interface MessageMap {
66
'get-taglist': [string | null, TagList | TagItem | undefined];
77
'get-tagreplace': [string | null, TagReplace | string];
88
'auto-update': [boolean, boolean];
9+
'ext-update': [void, void];
910
'suggest-tag': [
1011
{
1112
term: string;

src/tool/config-manage.ts

+4-6
Original file line numberDiff line numberDiff line change
@@ -68,12 +68,10 @@ class ConfigManage {
6868
if (!data) {
6969
return { ...this.defaultValue };
7070
}
71-
const defaultValue = this.defaultValue as any;
72-
const input = { ...data } as any;
73-
for (const key in defaultValue) {
74-
if (typeof input[key] === 'undefined') {
75-
input[key] = defaultValue[key];
76-
}
71+
const input = { ...data } as ConfigData;
72+
for (const k in this.defaultValue) {
73+
const key = k as keyof ConfigData;
74+
((input as unknown) as Record<string, unknown>)[key] = input[key] ?? this.defaultValue[key];
7775
}
7876
return input;
7977
}

src/tool/storage.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
1-
const cacheRoot: { [key: string]: any } = {};
1+
const cacheRoot: Record<string, unknown> = {};
22

33
function makeStorageKey(key: string): string {
44
return `EhSyringe.${key}`;
55
}
66

77
export function load<T>(key: string): T | undefined {
88
if (cacheRoot[key]) {
9-
return cacheRoot[key];
9+
return cacheRoot[key] as T;
1010
}
1111
const storageValue = window.localStorage.getItem(makeStorageKey(key));
1212
if (typeof storageValue === 'string') {
1313
try {
14-
return (cacheRoot[key] = JSON.parse(storageValue));
14+
return (cacheRoot[key] = JSON.parse(storageValue) as T);
1515
} catch {
1616
window.localStorage.removeItem(makeStorageKey(key));
1717
}

tsconfig.json

+4-5
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,16 @@
11
{
22
"include": ["src/*", "src/**/*"],
33
"exclude": ["node_modules", "**/*.spec.ts"],
4-
"typeRoots": ["node_modules/@types", "types"],
54
"compilerOptions": {
6-
"outDir": "./dist/",
7-
"sourceMap": true,
8-
"noImplicitAny": true,
9-
"esModuleInterop": true,
105
"module": "commonjs",
116
"target": "es6",
127
"jsx": "react",
8+
"outDir": "./dist/",
9+
"sourceMap": true,
10+
"esModuleInterop": true,
1311
"allowJs": true,
1412
"strict": true,
13+
"noImplicitReturns": true,
1514
"experimentalDecorators": true,
1615
"plugins": [
1716
{

0 commit comments

Comments
 (0)