-
Notifications
You must be signed in to change notification settings - Fork 1
/
newtab.js
81 lines (68 loc) · 3.04 KB
/
newtab.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
document.addEventListener('DOMContentLoaded', function () {
const searchButton = document.getElementById('searchButton');
const searchInput = document.getElementById('searchInput');
const searchEngineSelect = document.getElementById('searchEngine');
const darkModeToggle = document.getElementById('darkModeToggle');
// Load the last selected search engine from localStorage or set the default value
searchEngineSelect.value = localStorage.getItem('selectedSearchEngine') || 'google';
searchButton.addEventListener('click', function () {
var paragraph = document.getElementById("p");
var text = document.createTextNode("Searching...");
paragraph.appendChild(text);
performSearch();
});
searchInput.addEventListener('keyup', function (event) {
if (event.key === 'Enter') {
var paragraph = document.getElementById("p");
var text = document.createTextNode("Searching...");
paragraph.appendChild(text);
performSearch();
}
});
// DARK MODE SAVER
darkModeToggle.addEventListener('click', function () {
const isDarkMode = document.body.classList.toggle('dark-mode');
chrome.storage.sync.set({ darkMode: isDarkMode });
});
chrome.storage.sync.get('darkMode', function (result) {
const isDarkMode = result.darkMode;
if (isDarkMode !== undefined) {
document.body.classList.toggle('dark-mode', isDarkMode);
}
});
function performSearch() {
const searchValue = searchInput.value.trim();
const searchEngine = searchEngineSelect.value;
if (searchValue === '') {
window.location.href = 'https://bedantadey.me/';
return;
}
let url;
if (searchEngine === 'google') {
url = `https://www.google.com/search?q=${encodeURIComponent(searchValue)}`;
} else if (searchEngine === 'bing') {
url = `https://www.bing.com/search?q=${encodeURIComponent(searchValue)}`;
} else if (searchEngine === 'ddg') {
url = `https://duckduckgo.com/?q=${encodeURIComponent(searchValue)}`;
} else if (searchEngine === 'startpage') {
url = `https://www.startpage.com/do/dsearch?query=${encodeURIComponent(searchValue)}`;
} else if (searchEngine === 'ecosia') {
url = `https://www.ecosia.org/search?q=${encodeURIComponent(searchValue)}`;
} else if (searchEngine === 'wikipedia') {
url = `https://en.wikipedia.org/wiki/${encodeURIComponent(searchValue)}`;
} else if (searchEngine === 'youtube') {
url = `https://www.youtube.com/results?search_query=${encodeURIComponent(searchValue)}`;
} else if (searchEngine === 'britannica') {
url = `https://www.britannica.com/search?query=${encodeURIComponent(searchValue)}`;
} else if (searchEngine === 'pubchem') {
url = `https://pubchem.ncbi.nlm.nih.gov/#query=${encodeURIComponent(searchValue)}`;
} else if (searchEngine === 'brave') {
url = `https://search.brave.com/search?q=${encodeURIComponent(searchValue)}`;
}
if (url) {
window.location.href = url;
}
// SEARCH SAVER
localStorage.setItem('selectedSearchEngine', searchEngine);
}
});