-
Notifications
You must be signed in to change notification settings - Fork 1
/
popup.js
54 lines (47 loc) · 2.01 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
const btn = document.querySelector('#convert-btn');
const select = document.querySelector('select');
btn.addEventListener('click', () => {
const value = select.value;
// convert chats to markdown
if (value === 'md') {
// send message to content.js
chrome.tabs.query({ active: true, currentWindow: true }, (tabs) => {
chrome.tabs.sendMessage(tabs[0].id, { type: 'CONVERT-MD' }, (response) => {
const textarea = document.querySelector('textarea');
const turndownService = new TurndownService();
const markdown = turndownService.turndown(response.join('<br/><br/>'));
textarea.value = markdown;
});
});
} else if (value === 'html') {
chrome.tabs.query({ active: true, currentWindow: true }, (tabs) => {
chrome.tabs.sendMessage(tabs[0].id, { type: 'CONVERT-HTML' }, (response) => {
const textarea = document.querySelector('textarea');
textarea.value = response;
});
});
} else if (value === 'json') {
chrome.tabs.query({ active: true, currentWindow: true }, (tabs) => {
chrome.tabs.sendMessage(tabs[0].id, { type: 'CONVERT-JSON' }, (response) => {
const textarea = document.querySelector('textarea');
textarea.value = response;
});
});
}
})
const copyBtn = document.querySelector('#copy-btn');
copyBtn.addEventListener('click', () => {
const textarea = document.querySelector('textarea');
textarea.select();
document.execCommand('copy');
})
const downloadBtn = document.querySelector('#download-btn');
downloadBtn.addEventListener('click', () => {
const textarea = document.querySelector('textarea');
const value = select.value;
const element = document.createElement('a');
const file = new Blob([textarea.value], { type: 'text/plain' });
element.href = URL.createObjectURL(file);
element.download = 'chats.' + value;
element.click();
})