-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
41 lines (35 loc) · 1.25 KB
/
script.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
document.addEventListener("DOMContentLoaded", () => {
const searchInput = document.getElementById("search-input");
const searchIcon = document.getElementById("search-icon");
const engines = {
baidu: "https://www.baidu.com/s?wd=",
bing: "https://www.bing.com/search?q=",
google: "https://www.google.com/search?q="
};
let selectedEngine = engines.google;
const buttons = document.querySelectorAll(".option");
buttons.forEach(button => {
button.addEventListener("click", () => {
buttons.forEach(btn => btn.classList.remove("selected"));
button.classList.add("selected");
selectedEngine = engines[button.id];
});
});
function performSearch() {
const query = searchInput.value;
if (query.trim() !== "") {
window.open(selectedEngine + encodeURIComponent(query), '_blank');
}
}
searchInput.addEventListener("keydown", (event) => {
if (event.key === "Enter") {
performSearch();
}
});
searchIcon.addEventListener("click", () => {
performSearch();
});
// 默认选择Baidu
document.getElementById("baidu").classList.add("selected");
selectedEngine = engines.baidu;
});