-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.js
92 lines (69 loc) · 2.46 KB
/
main.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
const paragraph = document.querySelector('.speech');
const list = document.querySelector('ul');
const createListItem = (text, bookmarked = false) => `
<span>${text}</span>
<div>
<button class="copy" title="Copy to Clipboard">
<img src="assets/copy.svg" alt="copy" />
</button>
<button class="bookmark ${
bookmarked ? 'bookmarked' : ''
}" title="Bookmark for Later">
<img src="assets/bookmark.svg" alt="bookmark" />
</button>
<button class="delete" title="Delete from List">
<img src="assets/trash.svg" alt="delete" />
</button>
</div>
`;
window.SpeechRecognition =
window.SpeechRecognition || window.webkitSpeechRecognition;
const recognition = new SpeechRecognition();
recognition.lang = 'bn-BD';
recognition.interimResults = false;
recognition.addEventListener('result', (e) => {
paragraph.innerText = e.results[0][0].transcript;
const li = document.createElement('li');
li.setAttribute('data-id', Date.now());
li.innerHTML = createListItem(paragraph.innerText);
list.appendChild(li);
});
recognition.addEventListener('end', recognition.start);
recognition.start();
window.addEventListener('click', (e) => {
if (e.target.classList.contains('copy')) {
const text =
e.target.parentElement.parentElement.querySelector('span').innerText;
if (!navigator.clipboard) {
return;
}
navigator.clipboard.writeText(text);
} else if (e.target.classList.contains('delete')) {
e.target.parentElement.parentElement.remove();
} else if (e.target.classList.contains('bookmark')) {
const { id } = e.target.parentElement.parentElement.dataset;
const text =
e.target.parentElement.parentElement.querySelector('span').innerText;
const bookmarks = JSON.parse(localStorage.getItem('bookmarks')) || [];
const bookmarkIndex = bookmarks.findIndex((bookmark) => bookmark.id === id);
if (bookmarkIndex === -1) {
bookmarks.push({
id,
text,
});
} else {
bookmarks.splice(bookmarkIndex, 1);
}
e.target.classList.toggle('bookmarked');
localStorage.setItem('bookmarks', JSON.stringify(bookmarks));
}
});
window.addEventListener('DOMContentLoaded', () => {
const bookmarks = JSON.parse(localStorage.getItem('bookmarks')) || [];
bookmarks.forEach((bookmark) => {
const li = document.createElement('li');
li.setAttribute('data-id', bookmark.id);
li.innerHTML = createListItem(bookmark.text, true);
list.appendChild(li);
});
});