-
Notifications
You must be signed in to change notification settings - Fork 1
/
popup.js
199 lines (170 loc) · 6.43 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
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
document.addEventListener('DOMContentLoaded', () => {
const status = document.getElementById('status');
const warning = document.getElementById('warning');
const topicSelect = document.getElementById('topic-select');
const allStorageItems = ['topics', 'apiUrl', 'accessToken', 'prefillEnabled'];
let config = {
topics: [],
apiUrl: '',
accessToken: '',
prefillEnabled: true,
pageUrl: ''
};
// Initialize the extension on load
init();
// Initializes the extension by loading the configuration and adding event listeners.
function init() {
loadConfig();
addEventListeners();
}
// Loads the configuration from Chrome storage and applies settings to the UI.
async function loadConfig() {
try {
const items = await getConfigs(allStorageItems);
config.pageUrl = await getPageUrl();
config = {
...config,
topics: items.topics ? items.topics.split(',') : [],
apiUrl: items.apiUrl || '',
accessToken: items.accessToken || '',
prefillEnabled: !!items.prefillEnabled
};
updateUI();
} catch (error) {
console.error('Error retrieving settings:', error);
}
}
// Adds necessary event listeners for UI interactions.
function addEventListeners() {
document.querySelector('.settings').addEventListener('click', () => toggleVisibility('settings'));
document.getElementById('save').addEventListener('click', saveConfig);
document.querySelector('.open-url').addEventListener('click', openNtfyUrl);
document.getElementById('send').addEventListener('click', sendMessage);
}
// Configuration Management
// Retrieves configuration items from Chrome storage.
async function getConfigs(storageItems) {
return new Promise((resolve, reject) => {
chrome.storage.sync.get(storageItems, items => chrome.runtime.lastError ? reject(chrome.runtime.lastError) : resolve(items));
});
}
// Saves the updated configuration to Chrome storage and updates the UI.
async function saveConfig() {
const updatedConfig = {
topics: document.getElementById('topics').value,
apiUrl: document.getElementById('url').value,
accessToken: document.getElementById('token').value,
prefillEnabled: document.getElementById('prefill').checked,
};
try {
await new Promise((resolve, reject) => {
chrome.storage.sync.set(updatedConfig, () => {
chrome.runtime.lastError ? reject(chrome.runtime.lastError) : resolve();
});
});
config = { ...config, ...updatedConfig, topics: updatedConfig.topics.split(',') };
showStatus('Configuration saved.', 'green');
updateTopicDropdown();
prefillMessage();
} catch (error) {
showStatus('Error saving configuration.', 'red');
}
}
// Styling and UI Functions
// Updates UI elements with loaded configuration values.
function updateUI() {
setElementValue('url', config.apiUrl);
setElementValue('token', config.accessToken);
setElementValue('topics', config.topics.join(','));
setElementChecked('prefill', config.prefillEnabled);
updateTopicDropdown();
prefillMessage();
}
// Shows a temporary status message to the user.
function showStatus(message, color) {
status.textContent = message;
status.style.color = color;
setTimeout(() => (status.textContent = ''), 3000);
}
// Shows a temporary warning message.
function showWarning(message) {
warning.textContent = message;
warning.style.color = 'red';
warning.classList.remove('hidden');
setTimeout(() => warning.classList.add('hidden'), 3000);
}
// Sets a text input field's value by element ID.
function setElementValue(id, value) {
document.getElementById(id).value = value || '';
}
// Sets a checkbox's checked state by element ID.
function setElementChecked(id, value) {
document.getElementById(id).checked = !!value;
}
// Updates the dropdown menu with the topics from the configuration.
function updateTopicDropdown() {
topicSelect.innerHTML = '';
config.topics.forEach(topic => {
const option = document.createElement('option');
option.value = topic.trim();
option.textContent = topic.trim();
topicSelect.appendChild(option);
});
}
// Prefills the message input with the current page URL if enabled.
function prefillMessage() {
const message = document.getElementById('message');
const urlMessage = `${config.pageUrl}\n`;
message.value = config.prefillEnabled
? urlMessage + message.value.replace(urlMessage, '')
: message.value.replace(urlMessage, '');
}
// Toggles visibility of an element by ID.
function toggleVisibility(id) {
document.getElementById(id).classList.toggle('hidden');
}
// URL and Messaging Functions
// Gets the URL of the current active tab.
async function getPageUrl() {
return new Promise((resolve, reject) => {
chrome.tabs.query({ active: true, currentWindow: true }, tabs =>
chrome.runtime.lastError
? reject(chrome.runtime.lastError)
: resolve(tabs[0].url));
});
}
// Opens the configured API URL in a new tab, or shows a warning if not set.
function openNtfyUrl() {
if (config.apiUrl) {
chrome.tabs.create({ url: config.apiUrl });
} else {
showWarning('Please configure ntfy URL first.');
}
}
// Sends a message to the configured API endpoint.
function sendMessage() {
const { fullUrl, headers, message } = createRequest();
if (!fullUrl) return;
fetch(fullUrl, { method: 'POST', headers, body: message })
.then(response => response.ok
? showStatus('Message sent successfully.', 'green')
: showStatus('Failed to send message.', 'red'))
.catch(error => showStatus(`Error: ${error}`, 'red'));
}
// Prepares the request URL, headers, and body for sending a message.
function createRequest() {
if (!topicSelect.value || !config.apiUrl) {
showWarning('Please configure topics and API URL.');
return {};
}
const urlObj = new URL(config.apiUrl);
const headers = new Headers();
if (urlObj.username && urlObj.password) {
headers.set('Authorization', `Basic ${btoa(`${urlObj.username}:${urlObj.password}`)}`);
}
if (config.accessToken) {
headers.set('Authorization', `Bearer ${config.accessToken}`);
}
return { fullUrl: `${urlObj.origin}/${topicSelect.value}`, headers, message: document.getElementById('message').value };
}
});