-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsetup.js
More file actions
95 lines (80 loc) · 2.78 KB
/
Copy pathsetup.js
File metadata and controls
95 lines (80 loc) · 2.78 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
// Load existing config if available
document.addEventListener('DOMContentLoaded', async () => {
const data = await chrome.storage.local.get(['notionToken', 'databaseId']);
if (data.notionToken) {
document.getElementById('notionToken').value = data.notionToken;
}
if (data.databaseId) {
document.getElementById('databaseId').value = data.databaseId;
}
});
// Handle form submission
document.getElementById('setupForm').addEventListener('submit', async (e) => {
e.preventDefault();
const saveBtn = document.getElementById('saveBtn');
const notionToken = document.getElementById('notionToken').value.trim();
const databaseId = document.getElementById('databaseId').value.trim();
// Clean UI state
showStatus('', '');
// Validate token format
if (!notionToken.startsWith('secret_') && !notionToken.startsWith('ntn_')) {
showStatus('error', 'Token must start with "ntn_" or "secret_"');
return;
}
// Validate database ID
if (databaseId.length < 32) {
showStatus('error', 'Database ID looks too short.');
return;
}
// Disable button
saveBtn.disabled = true;
saveBtn.textContent = 'Verifying...';
try {
// Test connection
const response = await fetch(`https://api.notion.com/v1/databases/${databaseId}`, {
method: 'GET',
headers: {
'Authorization': `Bearer ${notionToken}`,
'Notion-Version': '2022-06-28'
}
});
if (!response.ok) {
if (response.status === 401) {
throw new Error('Invalid token. Check integration settings.');
} else if (response.status === 404) {
throw new Error('Database not found. Did you share it with the integration?');
} else {
throw new Error(`Connection failed (${response.status}).`);
}
}
// Save credentials
await chrome.storage.local.set({
notionToken,
databaseId
});
// Success!
saveBtn.textContent = 'Connected';
showStatus('success', `
<strong>System Operational</strong><br>
<span style="opacity:0.6; font-size:12px">Configuration saved. Closing in 3s...</span>
`);
// Auto-close after 3 seconds
setTimeout(() => {
window.close();
}, 3000);
} catch (error) {
showStatus('error', error.message);
saveBtn.disabled = false;
saveBtn.textContent = 'Try Again';
}
});
function showStatus(type, message) {
const statusEl = document.getElementById('status');
if (!type) {
statusEl.style.display = 'none';
return;
}
statusEl.className = `status ${type}`;
statusEl.innerHTML = message;
statusEl.style.display = 'block';
}