-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpopup.js
62 lines (56 loc) · 2.18 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
// Handle search icon click
const defaultBaseUrl = 'https://chatgpt.com';
document.getElementById('searchIcon').addEventListener('click', function () {
const searchInput = document.getElementById('searchInput').value.trim();
if (searchInput) {
chrome.storage.sync.get(['endpoint'], function (result) {
const baseUrl = result.endpoint || defaultBaseUrl; // Default fallback
const queryParam = encodeURIComponent(searchInput);
const searchUrl = `${baseUrl}?q=${queryParam}`;
chrome.tabs.create({ url: searchUrl });
});
}
});
// Handle Enter key in search input
document.getElementById('searchInput').addEventListener('keydown', function (e) {
if (e.key === 'Enter') {
const searchInput = this.value.trim();
if (searchInput) {
chrome.storage.sync.get(['endpoint'], function (result) {
const baseUrl = result.endpoint || defaultBaseUrl; // Default fallback
const queryParam = encodeURIComponent(searchInput);
const searchUrl = `${baseUrl}?q=${queryParam}`;
chrome.tabs.create({ url: searchUrl });
});
}
}
});
// Settings Toggle
const settingsButton = document.getElementById('settingsButton');
const settingsContainer = document.querySelector('.settings-container');
const body = document.body;
settingsButton.addEventListener('click', function () {
settingsContainer.classList.toggle('hidden');
body.classList.toggle('settings-active'); // Toggle body height
});
// Save Endpoint
const saveButton = document.getElementById('saveEndpoint');
saveButton.addEventListener('click', function () {
const endpoint = document.getElementById('endpointInput').value.trim();
if (endpoint) {
chrome.storage.sync.set({ endpoint: endpoint }, function () {
console.log('Endpoint saved:', endpoint);
// Hide settings view and return to search bar
settingsContainer.classList.add('hidden');
body.classList.remove('settings-active');
});
}
});
// Load saved endpoint
chrome.storage.sync.get(['endpoint'], function (result) {
if (result.endpoint) {
document.getElementById('endpointInput').value = result.endpoint;
} else {
document.getElementById('endpointInput').value = defaultBaseUrl;
}
});