-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpopup.js
163 lines (143 loc) · 6.6 KB
/
popup.js
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
document.addEventListener('DOMContentLoaded', function () {
const encryptionTab = document.getElementById('encryptionTab');
const decryptionTab = document.getElementById('decryptionTab');
const encryptionTabContent = document.getElementById('encryptionTabContent');
const decryptionTabContent = document.getElementById('decryptionTabContent');
const encryptSubmitButton = document.getElementById('encryptSubmitButton');
const decryptSubmitButton = document.getElementById('decryptSubmitButton');
const plaintextInput = document.getElementById('plaintextInput');
const ciphertextInput = document.getElementById('ciphertextInput');
const publicKeyFileInput = document.getElementById('publicKeyFileInput');
const privateKeyFileInput = document.getElementById('privateKeyFileInput');
const encryptionResponseOutput = document.getElementById('encryptionResponseOutput');
const decryptionResponseOutput = document.getElementById('decryptionResponseOutput');
function copyToClipboard(text) {
const textarea = document.createElement('textarea');
textarea.value = text;
document.body.appendChild(textarea);
textarea.select();
document.execCommand('copy');
document.body.removeChild(textarea);
}
encryptionResponseOutput.addEventListener('click', () => {
const message = encryptionResponseOutput.value;
if (message) {
copyToClipboard(message);
}
});
// 복호화 응답 클릭 시 복사
decryptionResponseOutput.addEventListener('click', () => {
const message = decryptionResponseOutput.value;
if (message) {
copyToClipboard(message);
}
});
// 탭 전환
encryptionTab.addEventListener('click', () => {
encryptionTab.classList.add('active');
decryptionTab.classList.remove('active');
encryptionTabContent.classList.add('active');
decryptionTabContent.classList.remove('active');
chrome.storage.session.get('publicKeyFile', (result) => {
if (result.publicKeyFile) {
// Base64 데이터를 Blob으로 변환
const byteCharacters = atob(result.publicKeyFile);
const byteNumbers = new Array(byteCharacters.length).fill().map((_, i) => byteCharacters.charCodeAt(i));
const byteArray = new Uint8Array(byteNumbers);
const blob = new Blob([byteArray]);
// Blob을 파일로 설정
const file = new File([blob], "publicKey-file", { type: "application/octet-stream" });
// File 객체를 publicKeyFileInput에 설정
const dataTransfer = new DataTransfer();
dataTransfer.items.add(file);
publicKeyFileInput.files = dataTransfer.files;
}
});
});
decryptionTab.addEventListener('click', () => {
decryptionTab.classList.add('active');
encryptionTab.classList.remove('active');
decryptionTabContent.classList.add('active');
encryptionTabContent.classList.remove('active');
chrome.storage.session.get('privateKeyFile', (result) => {
if (result.privateKeyFile) {
const byteCharacters = atob(result.privateKeyFile);
const byteNumbers = new Array(byteCharacters.length).fill().map((_, i) => byteCharacters.charCodeAt(i));
const byteArray = new Uint8Array(byteNumbers);
const blob = new Blob([byteArray]);
const file = new File([blob], "privateKey-file", { type: "application/octet-stream" });
const dataTransfer = new DataTransfer();
dataTransfer.items.add(file);
privateKeyFileInput.files = dataTransfer.files;
}
});
});
privateKeyFileInput.addEventListener('change', () => {
const file = privateKeyFileInput.files[0];
if (file) {
const reader = new FileReader();
reader.onload = function (event) {
const base64File = event.target.result.split(',')[1]; // Base64 데이터 추출
chrome.storage.session.set({ 'privateKeyFile': base64File }, () => {
console.log('파일 저장 완료');
});
};
reader.readAsDataURL(file); // 파일 읽기
}
});
publicKeyFileInput.addEventListener('change', () => {
const file = publicKeyFileInput.files[0];
if (file) {
const reader = new FileReader();
reader.onload = function (event) {
const base64File = event.target.result.split(',')[1]; // Base64 데이터 추출
chrome.storage.session.set({ 'publicKeyFile': base64File }, () => {
console.log('파일 저장 완료');
});
};
reader.readAsDataURL(file); // 파일 읽기
}
});
// 서버로 본문 및 파일 전송
encryptSubmitButton.addEventListener('click', () => {
const plaintext = plaintextInput.value;
const file = publicKeyFileInput.files[0];
const formData = new FormData();
formData.append('content', plaintext);
if (file) {
formData.append('publicKeyFile', file);
}
fetch('http://localhost:8000/crypto-service/encryption', {
method: 'POST',
body: formData
})
.then(response => response.json())
.then(data => {
encryptionResponseOutput.value = data.data.encryptedContent || '응답 메시지가 없습니다.';
})
.catch(error => {
encryptionResponseOutput.value = '암호화에 실패하였습니다.'; // 오류 메시지 표시
});
});
decryptSubmitButton.addEventListener('click', () => {
const ciphertext = ciphertextInput.value;
const file = privateKeyFileInput.files[0];
const formData = new FormData();
formData.append('encryptedMessage', ciphertext);
if (file) {
formData.append('privateKeyFile', file);
}
fetch('http://localhost:8000/crypto-service/decryption', {
method: 'POST',
body: formData
})
.then(response => response.json())
.then(data => {
decryptionResponseOutput.value = data.data.content || '응답 메시지가 없습니다.';
})
.catch(error => {
decryptionResponseOutput.value = '복호화에 실패하였습니다.'; // 오류 메시지 표시
});
});
encryptionTab.click();
});