Skip to content

Commit e86b6af

Browse files
committedFeb 28, 2020
fix missing db file
1 parent db8d566 commit e86b6af

File tree

5 files changed

+81
-16
lines changed

5 files changed

+81
-16
lines changed
 

‎CHANGELOG.md

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

3-
## upcoming
3+
## 1.2.8
44
* 修复标签介绍显示异常
55
* 添加 tooltip 显示翻译前标签
6+
* 同时支持压缩和未压缩的数据
67

78
## 1.2.7
89
* 修复标签图标问题

‎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.2.7",
4+
"version": "1.2.8",
55
"description": "E 站注射器,将中文翻译注入到 E 站体内。",
66
"author": "EhTagTranslation",
77
"main": "syringe.js",

‎src/background/tag-database.ts

+5-3
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,14 @@ class TagDatabase {
5757
const dbUrl = chrome.runtime.getURL('assets/tag.db');
5858
const r = await fetch(dbUrl);
5959
const buf = await r.arrayBuffer();
60-
this.update(buf, defaultReleaseLink, new Date(0));
60+
this.update(buf, true, defaultReleaseLink, new Date(0));
6161
}
6262

63-
update(data: ArrayBuffer, releaseLink: string, updateTime: Date = new Date()): void {
63+
update(data: ArrayBuffer, isGziped: boolean, releaseLink: string, updateTime: Date = new Date()): void {
6464
const timer = logger.time('构建数据');
65-
const tagDB: EHTDatabase = JSON.parse(pako.ungzip(new Uint8Array(data), { to: 'string' }));
65+
const tagDB: EHTDatabase = JSON.parse(isGziped
66+
? (pako.ungzip(new Uint8Array(data), { to: 'string' }))
67+
: (new TextDecoder('utf-8').decode(data)));
6668
const sha = tagDB.head.sha;
6769
const tagReplace: TagReplace = {};
6870
const tagList: TagList = [];

‎src/background/updater.ts

+14-9
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { BehaviorSubject } from 'rxjs';
22
import { browser } from 'webextension-polyfill-ts';
33

4-
import { DownloadStatus, ReleaseCheckData } from '../interface';
4+
import { DownloadStatus, ReleaseCheckData, GithubRelease } from '../interface';
55
import { badgeLoading } from '../tool/badge-loading';
66
import { chromeMessage } from '../tool/chrome-message';
77
import { logger } from '../tool/log';
@@ -46,7 +46,7 @@ class Updater {
4646
this.initDownloadStatus();
4747
try {
4848
const data = await this.download();
49-
tagDatabase.update(data.db, data.release.html_url);
49+
tagDatabase.update(data.content, data.filename.endsWith('.gz'), data.release.html_url);
5050

5151
badgeLoading.set('OK', '#00C801');
5252
this.pushDownloadStatus({ run: true, info: '更新完成', progress: 100, complete: true });
@@ -90,9 +90,9 @@ class Updater {
9090

9191
const { sha, releaseLink } = await browser.storage.local.get(['sha', 'releaseLink']);
9292
const githubDownloadUrl = 'https://api.github.com/repos/ehtagtranslation/Database/releases/latest';
93-
const info = await (await fetch(githubDownloadUrl)).json();
93+
const info = await (await fetch(githubDownloadUrl)).json() as GithubRelease;
9494

95-
if (!(info && info.target_commitish)) {
95+
if (!info?.target_commitish) {
9696
return null;
9797
}
9898
this.lastCheckData.next({
@@ -106,7 +106,7 @@ class Updater {
106106
return this.lastCheckData.value;
107107
}
108108

109-
private download(): Promise<{ release: any, db: ArrayBuffer }> {
109+
private download(): Promise<{ release: GithubRelease, filename: string, content: ArrayBuffer }> {
110110
return new Promise(async (resolve, reject) => {
111111
if (this.loadLock) {
112112
reject('已经正在下载');
@@ -116,38 +116,43 @@ class Updater {
116116
badgeLoading.set('', '#4A90E2', 2);
117117
this.pushDownloadStatus({ run: true, info: '加载中' });
118118
const checkData = await this.checkVersion();
119-
if (!(checkData && checkData.githubRelease && checkData.githubRelease.assets)) {
119+
if (!checkData?.githubRelease?.assets) {
120+
logger.debug('checkData', checkData);
120121
reject(new Error('无法获取版本信息'));
121122
this.loadLock = false;
122123
return;
123124
}
124125
const info = checkData.githubRelease;
125-
const asset = info.assets.find((i: any) => i.name === 'db.html.json.gz');
126-
const url = asset && asset.browser_download_url || '';
126+
const asset = info.assets.find(i => i.name === 'db.html.json.gz') ?? info.assets.find(i => i.name === 'db.html.json');
127+
const url = asset?.browser_download_url;
127128
if (!url) {
129+
logger.debug('assets', info.assets);
128130
reject(new Error('无法获取下载地址'));
129131
this.loadLock = false;
130132
return;
131133
}
132134

135+
const timer = logger.time(`开始下载 ${url}`);
133136
const xhr = new XMLHttpRequest();
134137
xhr.open('GET', url);
135138
xhr.responseType = 'arraybuffer';
136139
xhr.onload = () => {
137140
try {
138-
resolve({ release: info, db: xhr.response as ArrayBuffer });
141+
resolve({ release: info, filename: asset.name, content: xhr.response as ArrayBuffer });
139142
this.pushDownloadStatus({ info: '下载完成', progress: 100 });
140143
badgeLoading.set('100', '#4A90E2', 1);
141144
} catch (e) {
142145
reject(new Error('数据无法解析'));
143146
badgeLoading.set('ERR', '#C80000');
144147
} finally {
148+
timer.end();
145149
this.loadLock = false;
146150
}
147151
};
148152
xhr.onerror = (e) => {
149153
this.loadLock = false;
150154
badgeLoading.set('ERR', '#C80000');
155+
timer.end();
151156
reject(e);
152157
};
153158
xhr.onprogress = (event) => {

‎src/interface.ts

+59-2
Original file line numberDiff line numberDiff line change
@@ -64,14 +64,71 @@ export interface Suggestion {
6464
};
6565
}
6666

67+
export interface GithubRelease {
68+
url: string;
69+
assets_url: string;
70+
upload_url: string;
71+
html_url: string;
72+
id: number;
73+
node_id: string;
74+
tag_name: string;
75+
target_commitish: string;
76+
name: string;
77+
draft: boolean;
78+
author: Author;
79+
prerelease: boolean;
80+
created_at: string;
81+
published_at: string;
82+
assets: Asset[];
83+
tarball_url: string;
84+
zipball_url: string;
85+
body: string;
86+
}
87+
88+
interface Asset {
89+
url: string;
90+
id: number;
91+
node_id: string;
92+
name: string;
93+
label: string;
94+
uploader: Author;
95+
content_type: string;
96+
state: string;
97+
size: number;
98+
download_count: number;
99+
created_at: string;
100+
updated_at: string;
101+
browser_download_url: string;
102+
}
103+
104+
interface Author {
105+
login: string;
106+
id: number;
107+
node_id: string;
108+
avatar_url: string;
109+
gravatar_id: string;
110+
url: string;
111+
html_url: string;
112+
followers_url: string;
113+
following_url: string;
114+
gists_url: string;
115+
starred_url: string;
116+
subscriptions_url: string;
117+
organizations_url: string;
118+
repos_url: string;
119+
events_url: string;
120+
received_events_url: string;
121+
type: string;
122+
site_admin: boolean;
123+
}
124+
67125
export interface ReleaseCheckData {
68126
old: string;
69127
oldLink: string;
70128
new: string;
71129
newLink: string;
72130
timestamp: number;
73-
74-
githubRelease: any;
131+
githubRelease: GithubRelease;
75132
}
76133

77134
export interface DownloadStatus {

0 commit comments

Comments
 (0)
Please sign in to comment.