-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoptions.js
More file actions
206 lines (182 loc) · 7.27 KB
/
options.js
File metadata and controls
206 lines (182 loc) · 7.27 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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
// options.js — Agent CRUD: add, edit, delete
let editingId = null;
document.addEventListener('DOMContentLoaded', async () => {
const { agents } = await getAgents();
renderAgentList(agents);
// Color preset clicks
document.querySelectorAll('.color-dot').forEach(dot => {
dot.addEventListener('click', () => {
document.querySelectorAll('.color-dot').forEach(d => d.classList.remove('selected'));
dot.classList.add('selected');
document.getElementById('color').value = dot.dataset.color;
});
});
document.getElementById('agentForm').addEventListener('submit', handleSubmit);
document.getElementById('cancelBtn').addEventListener('click', resetForm);
document.getElementById('testBtn').addEventListener('click', testWebhook);
// Show test button when URL has value
document.getElementById('webhookUrl').addEventListener('input', (e) => {
document.getElementById('testBtn').style.display = e.target.value.trim() ? '' : 'none';
});
});
async function handleSubmit(e) {
e.preventDefault();
const status = document.getElementById('status');
const name = document.getElementById('name').value.trim();
const emoji = document.getElementById('emoji').value.trim() || '🤖';
const color = document.getElementById('color').value;
const webhookUrl = document.getElementById('webhookUrl').value.trim();
// Validate name
if (!name) {
status.textContent = 'Please enter a name.';
status.className = 'error';
return;
}
// Validate webhook URL
if (!webhookUrl) {
status.textContent = 'Please enter a webhook URL.';
status.className = 'error';
return;
}
try {
const parsed = new URL(webhookUrl);
if (parsed.protocol !== 'http:' && parsed.protocol !== 'https:') {
status.textContent = 'URL must start with http:// or https://';
status.className = 'error';
return;
}
} catch {
status.textContent = 'Invalid URL format.';
status.className = 'error';
return;
}
const { agents } = await getAgents();
if (editingId) {
const idx = agents.findIndex(a => a.id === editingId);
if (idx !== -1) {
agents[idx] = { ...agents[idx], name, emoji, color, webhookUrl };
}
} else {
agents.push({ id: generateId(), name, emoji, color, webhookUrl });
}
try {
await saveAgents(agents);
status.textContent = editingId ? 'Saved!' : 'Added!';
status.className = 'success';
renderAgentList(agents);
resetForm();
} catch {
status.textContent = 'Save failed. Storage may be full.';
status.className = 'error';
}
}
function renderAgentList(agents) {
const container = document.getElementById('agentList');
if (agents.length === 0) {
container.innerHTML = '<div class="empty-list">No agents yet. Add one above.</div>';
return;
}
container.innerHTML = agents.map(agent => `
<div class="agent-item" data-id="${agent.id}">
<div class="agent-avatar" style="background:${escapeHtml(agent.color)}">${escapeHtml(agent.emoji)}</div>
<div class="agent-info">
<div class="agent-info-name">${escapeHtml(agent.name)}</div>
<div class="agent-info-url" title="${escapeHtml(agent.webhookUrl)}">${escapeHtml(shortenUrl(agent.webhookUrl))}</div>
</div>
<div class="agent-actions">
<button class="btn-edit" title="Edit">✎</button>
<button class="btn-delete" title="Delete">🗑</button>
</div>
</div>
`).join('');
// Bind edit/delete buttons
container.querySelectorAll('.btn-edit').forEach(btn => {
btn.addEventListener('click', () => {
const id = btn.closest('.agent-item').dataset.id;
const agent = agents.find(a => a.id === id);
if (agent) editAgent(agent);
});
});
container.querySelectorAll('.btn-delete').forEach(btn => {
btn.addEventListener('click', () => {
const item = btn.closest('.agent-item');
const id = item.dataset.id;
const name = item.querySelector('.agent-info-name')?.textContent || 'this agent';
// Inline confirm
const actions = item.querySelector('.agent-actions');
actions.innerHTML = `<span style="font-size:12px;color:#c62828">Delete?</span>
<button class="btn-confirm-yes" style="font-size:12px;color:#c62828;font-weight:600;background:none;border:none;cursor:pointer">Yes</button>
<button class="btn-confirm-no" style="font-size:12px;color:#767676;background:none;border:none;cursor:pointer">No</button>`;
item.querySelector('.btn-confirm-no').addEventListener('click', () => renderAgentList(agents));
item.querySelector('.btn-confirm-yes').addEventListener('click', async () => {
const { agents: current } = await getAgents();
const updated = current.filter(a => a.id !== id);
try {
await saveAgents(updated);
renderAgentList(updated);
if (editingId === id) resetForm();
} catch {
const status = document.getElementById('status');
status.textContent = 'Delete failed. Storage error.';
status.className = 'error';
}
});
});
});
}
function editAgent(agent) {
document.getElementById('name').value = agent.name;
document.getElementById('emoji').value = agent.emoji;
document.getElementById('color').value = agent.color;
document.querySelectorAll('.color-dot').forEach(d => {
d.classList.toggle('selected', d.dataset.color === agent.color);
});
document.getElementById('webhookUrl').value = agent.webhookUrl;
editingId = agent.id;
document.getElementById('submitBtn').textContent = 'Save';
document.getElementById('cancelBtn').style.display = '';
document.getElementById('status').textContent = '';
}
function resetForm() {
document.getElementById('agentForm').reset();
document.getElementById('color').value = '#3578b9';
document.querySelectorAll('.color-dot').forEach(d => {
d.classList.toggle('selected', d.dataset.color === '#3578b9');
});
editingId = null;
document.getElementById('submitBtn').textContent = 'Add Agent';
document.getElementById('cancelBtn').style.display = 'none';
document.getElementById('status').textContent = '';
}
async function testWebhook() {
const url = document.getElementById('webhookUrl').value.trim();
const status = document.getElementById('status');
if (!url) { status.textContent = 'Enter a URL first.'; status.className = 'error'; return; }
status.textContent = 'Testing...';
status.className = '';
try {
const res = await fetch(url, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ schema: 'share2agent/v1', test: true, timestamp: new Date().toISOString() }),
signal: AbortSignal.timeout(5000)
});
status.textContent = res.ok ? '✓ Connection successful' : 'Webhook returned ' + res.status;
status.className = res.ok ? 'success' : 'error';
} catch {
status.textContent = 'Could not reach webhook';
status.className = 'error';
}
}
function shortenUrl(url) {
try {
const u = new URL(url);
const host = u.hostname.length > 25 ? u.hostname.slice(0, 12) + '...' + u.hostname.slice(-10) : u.hostname;
return host + (u.pathname !== '/' ? u.pathname : '') + (u.port ? ':' + u.port : '');
} catch { return url; }
}
function escapeHtml(str) {
const div = document.createElement('div');
div.textContent = str;
return div.innerHTML;
}