-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsidepanel.js
More file actions
152 lines (122 loc) · 4.08 KB
/
sidepanel.js
File metadata and controls
152 lines (122 loc) · 4.08 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
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
(() => {
const container = document.getElementById('links-container');
const toast = document.getElementById('toast');
const addBtn = document.getElementById('add-link-btn');
const modal = document.getElementById('add-link-modal');
const form = document.getElementById('add-link-form');
const nameInput = document.getElementById('link-name');
const urlInput = document.getElementById('link-url');
const cancelBtn = document.getElementById('cancel-link-btn');
const closeBtn = document.getElementById('close-link-btn');
function getFaviconUrl(pageUrl) {
try {
const { hostname } = new URL(pageUrl);
return `https://www.google.com/s2/favicons?domain=${hostname}&sz=64`;
} catch {
return '';
}
}
function createLinkBox(label, url) {
const btn = document.createElement('button');
btn.className = 'link-box';
btn.dataset.url = url;
const faviconImg = document.createElement('img');
faviconImg.className = 'favicon';
faviconImg.src = getFaviconUrl(url);
faviconImg.alt = `${label} favicon`;
faviconImg.width = 20;
faviconImg.height = 20;
faviconImg.loading = 'lazy';
faviconImg.onerror = () => faviconImg.remove();
const textWrapper = document.createElement('div');
textWrapper.className = 'text';
const labelSpan = document.createElement('span');
labelSpan.className = 'label';
labelSpan.textContent = label;
const urlSpan = document.createElement('span');
urlSpan.className = 'url';
urlSpan.textContent = url;
textWrapper.appendChild(labelSpan);
textWrapper.appendChild(urlSpan);
btn.appendChild(faviconImg);
btn.appendChild(textWrapper);
const copiedSpan = document.createElement('span');
copiedSpan.className = 'copied-message';
copiedSpan.textContent = 'Copied to clipboard ✅';
btn.appendChild(copiedSpan);
return btn;
}
function showToast(message) {
toast.textContent = message;
toast.style.opacity = '1';
setTimeout(() => (toast.style.opacity = '0'), 1800);
}
chrome.storage.sync.get({ links: [] }, ({ links }) => {
links.forEach(({ label, url }) => {
container.insertBefore(createLinkBox(label, url), addBtn);
});
});
addBtn.addEventListener('click', () => {
modal.hidden = false;
nameInput.value = '';
urlInput.value = '';
nameInput.focus();
});
cancelBtn.addEventListener('click', () => {
modal.hidden = true;
});
closeBtn.addEventListener('click', () => {
modal.hidden = true;
});
modal.addEventListener('click', (e) => {
if (e.target === modal) {
modal.hidden = true;
}
});
form.addEventListener('submit', (e) => {
e.preventDefault();
const label = nameInput.value.trim();
const url = urlInput.value.trim();
if (!label || !url) {
showToast('Both fields are required.');
return;
}
try {
new URL(url);
} catch {
showToast('Please enter a valid URL.');
return;
}
container.insertBefore(createLinkBox(label, url), addBtn);
chrome.storage.sync.get({ links: [] }, (data) => {
chrome.storage.sync.set({ links: [...data.links, { label, url }] });
});
modal.hidden = true;
showToast('Link added!');
});
container.addEventListener('click', async (e) => {
const target = e.target.closest('.link-box');
if (!target) return;
target.classList.add('pressed');
const handleAnimationEnd = () => {
target.classList.remove('pressed');
target.removeEventListener('animationend', handleAnimationEnd);
};
target.addEventListener('animationend', handleAnimationEnd);
const url = target.dataset.url;
try {
await navigator.clipboard.writeText(url);
if (target._copiedTimeout) {
clearTimeout(target._copiedTimeout);
}
target.classList.add('show-copied');
target._copiedTimeout = setTimeout(() => {
target.classList.remove('show-copied');
target._copiedTimeout = null;
}, 900);
} catch (err) {
console.error('Failed to copy: ', err);
showToast('Could not copy. See console.');
}
});
})();