Skip to content

Commit b5b9492

Browse files
1.2.10
1 parent f6995a3 commit b5b9492

File tree

8 files changed

+156
-70
lines changed

8 files changed

+156
-70
lines changed

CHANGELOG.md

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

3+
## 1.2.10
4+
5+
- 修复标签翻译异常 (#144)
6+
37
## 1.2.9
48

59
- 标签自动完成支持收藏搜索和添加标签

package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "ehsyringe",
33
"displayName": "EhSyringe",
4-
"version": "1.2.9",
4+
"version": "1.2.10",
55
"description": "E 站注射器,将中文翻译注入到 E 站体内。",
66
"author": "EhTagTranslation",
77
"main": "syringe.js",
@@ -34,7 +34,7 @@
3434
"copy-webpack-plugin": "^5.1.1",
3535
"css-loader": "^3.5.3",
3636
"cssnano": "^4.1.10",
37-
"eslint": "^6.8.0",
37+
"eslint": "^7.0.0",
3838
"eslint-config-prettier": "^6.11.0",
3939
"eslint-plugin-prettier": "^3.1.3",
4040
"file-loader": "^6.0.0",

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { dateDiff } from '../../tool/tool';
88
// 1 day
99
const autoCheckInterval = 1000 * 60 * 60 * 24;
1010

11-
export const autoUpdateInit = async () => {
11+
export const autoUpdateInit = async (): Promise<void> => {
1212
const conf = await config.get();
1313
if (!conf.autoUpdate) return;
1414
logger.log('自动更新');

src/plugin/syringe/syringe.ts

+7-5
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { logger } from '../../tool/log';
77
import './syringe.less';
88

99
(function (): void {
10-
const tagClear = () => {
10+
const tagClear = (): void => {
1111
window.localStorage.removeItem('tag-list');
1212
window.localStorage.removeItem('tag-replace-data');
1313
window.localStorage.removeItem('tag-update-time');
@@ -35,7 +35,7 @@ function isText(node: Node): node is Text {
3535
}
3636

3737
class Syringe {
38-
tagReplace: TagReplace = {};
38+
tagReplace?: TagReplace;
3939
pendingTags: Node[] = [];
4040
documentEnd = false;
4141
readonly skipNode: Set<string> = new Set(['TITLE', 'LINK', 'META', 'HEAD', 'SCRIPT', 'BR', 'HR', 'STYLE', 'MARK']);
@@ -55,7 +55,8 @@ class Syringe {
5555
window.document.addEventListener('DOMContentLoaded', (e) => {
5656
this.documentEnd = true;
5757
});
58-
this.setBodyClass(document.querySelector('body')!);
58+
const body = document.querySelector('body');
59+
if (body) this.setBodyClass(body);
5960
this.observer = new MutationObserver((mutations) =>
6061
mutations.forEach((mutation) =>
6162
mutation.addedNodes.forEach((node1) => {
@@ -84,14 +85,15 @@ class Syringe {
8485
.then((data) => {
8586
this.tagReplace = data as TagReplace;
8687
timer.end();
87-
this.pendingTags.forEach((t) => this.translateTagImpl(t));
88+
const pendingTags = this.pendingTags;
8889
this.pendingTags = [];
90+
pendingTags.forEach((t) => this.translateTagImpl(t));
8991
})
9092
.catch(logger.error);
9193
}
9294
}
9395

94-
setBodyClass(node: HTMLBodyElement) {
96+
setBodyClass(node: HTMLBodyElement): void {
9597
if (!node) return;
9698
node.classList.add(!location.host.includes('exhentai') ? 'eh' : 'ex');
9799
if (!this.conf.showIcon) {

src/plugin/tag-tip/tag-tip.ts

+17-15
Original file line numberDiff line numberDiff line change
@@ -28,20 +28,22 @@ class TagTip {
2828
map(() => this.inputElement.value),
2929
// distinctUntilChanged()
3030
)
31-
.subscribe(this.search);
31+
.subscribe((s) => {
32+
this.search(s).catch(logger.error);
33+
});
3234

33-
fromEvent<KeyboardEvent>(this.inputElement, 'keydown').subscribe(this.keydown);
35+
fromEvent<KeyboardEvent>(this.inputElement, 'keydown').subscribe((e) => this.keydown(e));
3436

3537
fromEvent<MouseEvent>(this.autoCompleteList, 'click').subscribe((e) => {
3638
this.inputElement.focus();
3739
e.preventDefault();
3840
e.stopPropagation();
3941
});
4042

41-
fromEvent(this.inputElement, 'focus').subscribe(this.setListPosition);
43+
fromEvent(this.inputElement, 'focus').subscribe(() => this.setListPosition());
4244

43-
fromEvent(window, 'resize').subscribe(this.setListPosition);
44-
fromEvent(window, 'scroll').subscribe(this.setListPosition);
45+
fromEvent(window, 'resize').subscribe(() => this.setListPosition());
46+
fromEvent(window, 'scroll').subscribe(() => this.setListPosition());
4547

4648
fromEvent(document, 'click').subscribe(() => {
4749
this.autoCompleteList.innerHTML = '';
@@ -50,7 +52,7 @@ class TagTip {
5052
document.body.insertBefore(this.autoCompleteList, null);
5153
}
5254

53-
readonly search = async (value: string) => {
55+
async search(value: string): Promise<void> {
5456
// todo: 增加自定义分隔符
5557
value = this.inputElement.value = value.replace(/ +/gm, ' ');
5658
const values = value.match(/(\S+:".+?"|".+?"|\S+:\S+|\S+)/gim) ?? [];
@@ -81,9 +83,9 @@ class TagTip {
8183
this.autoCompleteList.insertBefore(this.tagElementItem(tag), null);
8284
});
8385
this.selectedIndex = -1;
84-
};
86+
}
8587

86-
readonly keydown = (e: KeyboardEvent) => {
88+
keydown(e: KeyboardEvent): void {
8789
if (e.code === 'ArrowUp' || e.code === 'ArrowDown') {
8890
if (e.code === 'ArrowUp') {
8991
this.selectedIndex--;
@@ -114,16 +116,16 @@ class TagTip {
114116
e.stopPropagation();
115117
}
116118
}
117-
};
119+
}
118120

119-
readonly setListPosition = () => {
121+
setListPosition(): void {
120122
const rect = this.inputElement.getBoundingClientRect();
121123
this.autoCompleteList.style.left = `${rect.left}px`;
122124
this.autoCompleteList.style.top = `${rect.bottom}px`;
123125
this.autoCompleteList.style.minWidth = `${rect.width}px`;
124-
};
126+
}
125127

126-
readonly tagElementItem = (suggestion: Suggestion): HTMLDivElement => {
128+
tagElementItem(suggestion: Suggestion): HTMLDivElement {
127129
const tag = suggestion.tag;
128130
const item = document.createElement('div');
129131
const cnName = document.createElement('span');
@@ -150,15 +152,15 @@ class TagTip {
150152
this.autoCompleteList.innerHTML = '';
151153
};
152154
return item;
153-
};
155+
}
154156
}
155157

156-
export const tagTipInit = async () => {
158+
export async function tagTipInit(): Promise<TagTip | undefined> {
157159
const conf = await config.get();
158160
if (!conf.tagTip) return;
159161
logger.log('标签提示');
160162

161163
const searchInput: HTMLInputElement | null = document.querySelector('#f_search, #newtagfield, [name=f_search]');
162164
if (!searchInput) return;
163165
return new TagTip(searchInput);
164-
};
166+
}

src/popup/popup.ts

+24-20
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { html, nothing, render, svg } from 'lit-html';
1+
import { html, nothing, render, svg, SVGTemplateResult, TemplateResult } from 'lit-html';
22
import { browser } from 'webextension-polyfill-ts';
33

44
import { background } from '../background';
@@ -30,9 +30,11 @@ interface PopupState {
3030

3131
class Popup {
3232
constructor() {
33-
window.addEventListener('click', (ev) => this.openLink(ev));
33+
window.addEventListener('click', (ev) => {
34+
this.openLink(ev).catch(logger.error);
35+
});
3436
this._update();
35-
this.getVersion().catch(logger.error);
37+
this.getVersion();
3638
this.checkVersion().catch(logger.error);
3739
this.loadConfig().catch(logger.error);
3840
browser.management
@@ -41,8 +43,10 @@ class Popup {
4143
this.state.extensionVersion = `${data.version}`;
4244
})
4345
.catch(logger.error);
44-
const downloadStatusSub = background.updater.downloadStatus.subscribe((data) => this.downloadStatus(data));
45-
window.addEventListener('unload', (_) => {
46+
const downloadStatusSub = background.updater.downloadStatus.subscribe((data) => {
47+
this.downloadStatus(data).catch(logger.error);
48+
});
49+
window.addEventListener('unload', () => {
4650
downloadStatusSub.unsubscribe();
4751
});
4852
}
@@ -86,12 +90,12 @@ class Popup {
8690
[0, 0],
8791
];
8892

89-
async loadConfig() {
93+
async loadConfig(): Promise<void> {
9094
this.configOriginal = await config.get();
9195
this.state.configValue = { ...this.configOriginal };
9296
}
9397

94-
testAnimation() {
98+
testAnimation(): void {
9599
const a = this.testAnimationList[this.testAnimationIndex];
96100
this.testAnimationIndex++;
97101
if (!this.testAnimationList[this.testAnimationIndex]) {
@@ -101,7 +105,7 @@ class Popup {
101105
this.state.progress = a[1];
102106
}
103107

104-
async getVersion() {
108+
getVersion(): void {
105109
const sha = background.tagDatabase.sha.value;
106110
const releaseLink = background.tagDatabase.releaseLink.value;
107111
const updateTime = background.tagDatabase.updateTime.value;
@@ -111,7 +115,7 @@ class Popup {
111115
this.state.updateTimeFull = updateTime?.toLocaleString() ?? 'N/A';
112116
}
113117

114-
async checkVersion() {
118+
async checkVersion(): Promise<void> {
115119
this.state.versionInfo = '检查中...';
116120
const data = await background.updater.checkVersion();
117121
logger.log('Release Data', data);
@@ -150,7 +154,7 @@ class Popup {
150154
this.state.progress = 100;
151155
this.state.animationState = 2;
152156
this.state.updateButtonDisabled = false;
153-
await this.getVersion();
157+
this.getVersion();
154158
await this.checkVersion();
155159

156160
await sleep(500);
@@ -161,12 +165,12 @@ class Popup {
161165
}
162166
}
163167

164-
private updateButtonClick = async () => {
168+
private async updateButtonClick(): Promise<void> {
165169
this.state.updateButtonDisabled = true;
166170
await background.updater.update();
167-
};
171+
}
168172

169-
_logoTemplate(progress = 0) {
173+
_logoTemplate(progress = 0): SVGTemplateResult {
170174
const PushRodStyle = `transform: translate(${(progress / 400) * 70}px, 0)`;
171175
const EnemaStyle = `transform: scaleX(${progress / 100})`;
172176

@@ -233,7 +237,7 @@ class Popup {
233237
</svg>`;
234238
}
235239

236-
changeConfigValue(key: string, value: any) {
240+
changeConfigValue(key: string, value: any): void {
237241
if (key === 'introduceImageLevel') {
238242
this.state.configValue = {
239243
...this.state.configValue,
@@ -251,20 +255,20 @@ class Popup {
251255
return !keys.every((key) => (this.configOriginal as any)[key] === (this.state.configValue as any)[key]);
252256
}
253257

254-
async saveConfig() {
258+
async saveConfig(): Promise<void> {
255259
await config.set(this.state.configValue);
256260
await this.loadConfig();
257261
await sleep(200);
262+
window.close();
258263
const tabs = await browser.tabs.query({ active: true });
259264
if (tabs?.length) {
260265
const ehtabs = tabs.filter((v) => v.url && /(\/\/|\.)(e-|ex)hentai\.org/i.test(v.url));
261266
logger.log('Reload tabs', ehtabs);
262-
ehtabs.forEach((v) => browser.tabs.reload(v.id).catch(logger.error));
267+
await Promise.all(ehtabs.map((v) => browser.tabs.reload(v.id)));
263268
}
264-
window.close();
265269
}
266270

267-
_settingPanelTemplate() {
271+
_settingPanelTemplate(): TemplateResult {
268272
const state = this.state;
269273

270274
const checkboxList: Array<{ key: string; name: string }> = [
@@ -355,7 +359,7 @@ class Popup {
355359
`;
356360
}
357361

358-
_template() {
362+
_template(): TemplateResult {
359363
const state = this.state;
360364
return html` <div class="popup-root ${state.showSettingPanel ? 'hide' : ''}">
361365
<div class="head-buttons">
@@ -428,7 +432,7 @@ class Popup {
428432
${state.configValue ? this._settingPanelTemplate() : nothing}`;
429433
}
430434

431-
_update() {
435+
_update(): void {
432436
render(this._template(), document.body);
433437
}
434438
}

src/tool/config-manage.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ export interface ConfigData {
1616
class ConfigManage {
1717
constructor() {
1818
/* 有可能会有性能问题, 开的页面多了不知道会是什么效果*/
19-
chrome.storage.onChanged.addListener(async (changes) => {
19+
chrome.storage.onChanged.addListener((changes) => {
2020
logger.log('插件存储改变', changes);
2121
if ('config' in changes && changes.config.newValue) {
2222
save('config', changes.config.newValue);
@@ -71,7 +71,6 @@ class ConfigManage {
7171
const defaultValue = this.defaultValue as any;
7272
const input = { ...data } as any;
7373
for (const key in defaultValue) {
74-
if (!defaultValue.hasOwnProperty(key)) continue;
7574
if (typeof input[key] === 'undefined') {
7675
input[key] = defaultValue[key];
7776
}

0 commit comments

Comments
 (0)