-
Notifications
You must be signed in to change notification settings - Fork 0
/
popup.js
93 lines (85 loc) · 3.45 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
document.addEventListener('DOMContentLoaded', function() {
const addButton = document.getElementById('addButton');
const headingList = document.getElementById('headingList');
const headingInput = document.getElementById('heading'); // Get the heading input element
const contentInput = document.getElementById('content'); // Get the content input element
addButton.addEventListener('click', function() {
const heading = headingInput.value;
const content = contentInput.value;
// Save the data to storage
chrome.storage.sync.set({ [heading]: content }, function() {
// Update the UI
const entryItem = document.createElement('li');
entryItem.className = 'entry';
entryItem.innerHTML = `
<span class="entry-heading">${heading}</span>
<div class="buttons">
<button class="copy-button">Copy</button>
<button class="remove-button">Remove</button>
</div>
`;
headingList.appendChild(entryItem);
// Clear input fields after adding entry
headingInput.value = ''; // Clear the heading input
contentInput.value = ''; // Clear the content input
});
});
// Pre-built entries
const preBuiltEntries = [
{ heading: 'Pre-built Heading 1', content: 'Pre-built Content 1' },
{ heading: 'Pre-built Heading 2', content: 'Pre-built Content 2' },
// Add more pre-built entries as needed
];
preBuiltEntries.forEach(entry => {
const { heading, content } = entry;
chrome.storage.sync.set({ [heading]: content });
const entryItem = document.createElement('li');
entryItem.className = 'entry';
entryItem.innerHTML = `
<span class="entry-heading">${heading}</span>
<div class="buttons">
<button class="copy-button">Copy</button>
<button class="remove-button">Remove</button>
</div>
`;
headingList.appendChild(entryItem);
});
// Load saved entries on popup open
chrome.storage.sync.get(null, function(result) {
Object.keys(result).forEach(function(heading) {
const entryItem = document.createElement('li');
entryItem.className = 'entry';
entryItem.innerHTML = `
<span class="entry-heading">${heading}</span>
<div class="buttons">
<button class="copy-button">Copy</button>
<button class="remove-button">Remove</button>
</div>
`;
headingList.appendChild(entryItem);
});
});
// Handle copy button click event
headingList.addEventListener('click', function(event) {
if (event.target.classList.contains('copy-button')) {
const heading = event.target.parentNode.parentNode.querySelector('.entry-heading').textContent;
chrome.storage.sync.get(heading, function(result) {
const tempInput = document.createElement('textarea');
document.body.appendChild(tempInput);
tempInput.value = result[heading];
tempInput.select();
document.execCommand('copy');
document.body.removeChild(tempInput);
});
}
});
// Handle remove button click event
headingList.addEventListener('click', function(event) {
if (event.target.classList.contains('remove-button')) {
const heading = event.target.parentNode.parentNode.querySelector('.entry-heading').textContent;
chrome.storage.sync.remove(heading, function() {
event.target.parentNode.parentNode.remove();
});
}
});
});