-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpopup.js
88 lines (72 loc) · 3.36 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
document.addEventListener('DOMContentLoaded', function() {
const searchForm = document.getElementById('searchForm');
const searchQueryInput = document.getElementById('searchQuery');
const searchResultsDiv = document.getElementById('searchResults');
const aboutButton = document.getElementById('aboutButton'); // Get the About button
function performSearch() {
const searchQuery = searchQueryInput.value.toLowerCase();
fetch(chrome.runtime.getURL('File.json')) // was planning on using CDN for file, but security concerns
.then(response => {
if (!response.ok) {
throw new Error('Network response was not ok');
}
return response.json();
})
.then(data => {
let responses;
if (searchQuery) {
responses = data.responses.filter(response =>
response.title.toLowerCase().includes(searchQuery) ||
response.message.toLowerCase().includes(searchQuery)
);
} else {
responses = data.responses;
}
searchResultsDiv.innerHTML = '';
responses.forEach(response => {
const entryDiv = document.createElement('div');
entryDiv.className = 'entry';
const titleDiv = document.createElement('div');
titleDiv.className = 'title';
titleDiv.textContent = response.title;
const messageDiv = document.createElement('div');
messageDiv.className = 'message';
messageDiv.textContent = response.message.substring(0, 120); // Limit the displayed message to the first 90 characters
entryDiv.appendChild(titleDiv);
entryDiv.appendChild(messageDiv);
entryDiv.addEventListener('click', () => {
navigator.clipboard.writeText(response.message)
.then(() => {
console.log('Message copied to clipboard');
window.close();
})
.catch(err => {
console.error('Could not copy text: ', err);
});
});
searchResultsDiv.appendChild(entryDiv);
});
})
.catch(error => {
console.error('There has been a problem with your fetch operation:', error);
});
}
searchForm.addEventListener('submit', function(event) {
event.preventDefault();
performSearch();
});
searchQueryInput.addEventListener('input', performSearch);
// Add click event listener to the About button
document.getElementById('aboutButton').addEventListener('click', function() {
window.location.href = 'about.html';
});
var settingsButton = document.getElementById('SettingsButton');
// Check if the settingsButton element exists
if (settingsButton) {
settingsButton.addEventListener('click', function() {
window.location.href = "settings.html";
});
} else {
console.error('Settings button not found');
}
});