-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathconfig-tab.js
229 lines (203 loc) · 6.79 KB
/
config-tab.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
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
// Config tab initialization and handlers
export async function initializeConfigTab() {
const configForm = document.getElementById('configForm');
const serverUrl = document.getElementById('archivebox_server_url');
const apiKey = document.getElementById('archivebox_api_key');
const matchUrls = document.getElementById('match_urls');
// Load saved values
const savedConfig = await chrome.storage.local.get([
'archivebox_server_url',
'archivebox_api_key',
'match_urls'
]);
serverUrl.value = savedConfig.archivebox_server_url || '';
apiKey.value = savedConfig.archivebox_api_key || '';
matchUrls.value = savedConfig.match_urls || '.*';
// Server test button handler
document.getElementById('testServer').addEventListener('click', async () => {
const statusIndicator = document.getElementById('serverStatus');
const statusText = document.getElementById('serverStatusText');
const updateStatus = (success, message) => {
statusIndicator.className = success ? 'status-indicator status-success' : 'status-indicator status-error';
statusText.textContent = message;
statusText.className = success ? 'text-success' : 'text-danger';
};
try {
let response = await fetch(`${serverUrl.value}/api/`, {
method: 'GET',
mode: 'cors',
credentials: 'omit'
});
// fall back to pre-v0.8.0 endpoint for backwards compatibility
if (response.status === 404) {
response = await fetch(`${serverUrl.value}`, {
method: 'GET',
mode: 'cors',
credentials: 'omit'
});
}
if (response.ok) {
updateStatus(true, '✓ Server is reachable');
} else {
updateStatus(false, `✗ Server error: ${response.status} ${response.statusText}`);
}
} catch (err) {
updateStatus(false, `✗ Connection failed: ${err.message}`);
}
});
// API key test button handler
document.getElementById('testApiKey').addEventListener('click', async () => {
const statusIndicator = document.getElementById('apiKeyStatus');
const statusText = document.getElementById('apiKeyStatusText');
try {
const response = await fetch(`${serverUrl.value}/api/v1/auth/check_api_token`, {
method: 'POST',
mode: 'cors',
credentials: 'omit',
body: JSON.stringify({
token: apiKey.value,
})
});
const data = await response.json();
if (data.user_id) {
statusIndicator.className = 'status-indicator status-success';
statusText.textContent = `✓ API key is valid: user_id = ${data.user_id}`;
statusText.className = 'text-success';
} else {
statusIndicator.className = 'status-indicator status-error';
statusText.textContent = `✗ API key error: ${response.status} ${response.statusText} ${JSON.stringify(data)}`;
statusText.className = 'text-danger';
}
} catch (err) {
statusIndicator.className = 'status-indicator status-error';
statusText.textContent = `✗ API test failed: ${err.message}`;
statusText.className = 'text-danger';
}
});
// Generate API key button handler
document.getElementById('generateApiKey').addEventListener('click', () => {
const key = Array.from(crypto.getRandomValues(new Uint8Array(16)))
.map(b => b.toString(16).padStart(2, '0'))
.join('');
apiKey.value = key;
});
// Login server button handler
document.getElementById('loginServer').addEventListener('click', () => {
if (serverUrl.value) {
window.open(`${serverUrl.value}/admin`, '_blank');
}
});
// Save changes when inputs change
[serverUrl, apiKey, matchUrls].forEach(input => {
input.addEventListener('change', async () => {
await chrome.storage.local.set({
archivebox_server_url: serverUrl.value.replace(/\/$/, ''),
archivebox_api_key: apiKey.value,
match_urls: matchUrls.value
});
});
});
// Test URL functionality
const testUrlInput = document.getElementById('testUrl');
const testButton = document.getElementById('testAdding');
const testStatus = document.getElementById('urlStatusText');
testButton.addEventListener('click', async () => {
const url = testUrlInput.value.trim();
if (!url) {
testStatus.innerHTML = `
<span class="status-indicator status-error"></span>
Please enter a URL to test
`;
return;
}
// Show loading state
testButton.disabled = true;
testStatus.innerHTML = `
<span class="spinner-border spinner-border-sm" role="status" aria-hidden="true"></span>
Testing...
`;
try {
const testEntry = {
url,
title: 'Test Entry',
timestamp: new Date().toISOString(),
tags: ['test']
};
const result = await syncToArchiveBox(testEntry);
if (result.ok) {
testStatus.innerHTML = `
<span class="status-indicator status-success"></span>
Success! URL was added to ArchiveBox
`;
// Clear the input on success
testUrlInput.value = '';
} else {
testStatus.innerHTML = `
<span class="status-indicator status-error"></span>
Error: ${result.status}
`;
}
} catch (error) {
testStatus.innerHTML = `
<span class="status-indicator status-error"></span>
Error: ${error.message}
`;
} finally {
testButton.disabled = false;
}
});
// Add Enter key support for test URL input
testUrlInput.addEventListener('keypress', (e) => {
if (e.key === 'Enter') {
e.preventDefault();
testButton.click();
}
});
}
async function syncToArchiveBox(entry) {
const { archivebox_server_url, archivebox_api_key } = await chrome.storage.local.get([
'archivebox_server_url',
'archivebox_api_key'
]);
if (!archivebox_server_url || !archivebox_api_key) {
return {
ok: false,
status: 'Server URL and API key must be configured and saved first'
};
}
try {
const response = await fetch(`${archivebox_server_url}/api/v1/cli/add`, {
method: 'POST',
mode: 'cors',
credentials: 'omit',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
'x-archivebox-api-key': archivebox_api_key,
},
body: JSON.stringify({
urls: [entry.url],
tag: entry.tags.join(','),
depth: 0,
update: false,
update_all: false,
}),
});
if (!response.ok) {
const text = await response.text();
return {
ok: false,
status: `Server returned ${response.status}: ${text}`
};
}
return {
ok: true,
status: 'Success'
};
} catch (err) {
return {
ok: false,
status: `Connection failed: ${err.message}`
};
}
}