-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.ts
More file actions
75 lines (66 loc) · 2.16 KB
/
Copy pathindex.ts
File metadata and controls
75 lines (66 loc) · 2.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import Crypto from './lib/Crypto';
import * as File from './lib/File';
function getFilename(path: string): string {
return path.substring(path.lastIndexOf('/') + 1);
}
function tips(tip: string) {
const ele = document.getElementById('tips');
ele && (ele.innerText = tip);
}
async function handler(content: string, type: 'encode' | 'decode') {
const bookmarkFile = document.querySelector<HTMLInputElement>('#bookmark-file');
const passphrase = document.querySelector<HTMLInputElement>('#passphrase');
if (bookmarkFile && passphrase) {
if (!bookmarkFile.value) {
tips('没有输入书签路径!');
return;
}
if (!passphrase.value) {
tips('没有输入密码短语!');
return;
}
if (type === 'encode') {
const encodeText = await Crypto.encryptAES(passphrase.value, content, bookmarkFile.value);
File.downloadTextFile(encodeText, getFilename(bookmarkFile.value), () => {
tips('编码完成,已导出文件。');
});
} else {
try {
const decodeText = await Crypto.decryptAES(passphrase.value, content, bookmarkFile.value);
File.downloadTextFile(decodeText, getFilename(bookmarkFile.value), () => {
tips('解码完成,已导出文件。');
});
} catch (err) {
console.error(err);
tips('解码过程中出现错误!');
}
}
} else {
tips('内部错误!');
}
}
window.onload = () => {
let fileContent = '';
const encodeBtn = document.getElementById('encode');
const decodeBtn = document.getElementById('decode');
const selectFile = document.querySelector<HTMLInputElement>('#select-file');
selectFile?.addEventListener('change', () => {
File.openTextFile.call(selectFile, (flag, content) => {
if (flag.state && content) {
fileContent = content;
} else {
tips('文件打开出错!');
}
});
});
encodeBtn?.addEventListener('click', ev => {
ev.stopPropagation();
console.log('encode!');
handler(fileContent, 'encode');
});
decodeBtn?.addEventListener('click', ev => {
ev.stopPropagation();
console.log('decode!');
handler(fileContent, 'decode');
});
};