gpt4 book ai didi

javascript - 我可以通过 JavaScript 以编程方式打开浏览器的 native 搜索对话框吗?

转载 作者:行者123 更新时间:2023-11-30 06:21:24 26 4
gpt4 key购买 nike

是否可以从网页内部以编程方式打开网络浏览器的 native 搜索对话框?

此外,如果可能的话,我能否以编程方式在其中执行搜索?

最佳答案

no way to open browser's utilities例如使用 javascript 的查找、扩展、设置等。但是,您可以使用提示窗口创建自己的搜索:

const searchBtn = document.getElementById("searchBtn");
const searchElement = document.querySelector("#text"); // Any element you want to search
const backup = searchElement.innerHTML;

searchBtn.addEventListener("click", function () {
searchElement.innerHTML = backup;

const search = prompt("Search");
const text = searchElement.innerHTML.split(" ");

if (search != null) {
for (let i = 0; i < text.length; i++) {
let index = text[i];
let splitIndex = index.split("");
if (index.toLowerCase().includes(search.toLowerCase())) {
for (let si = 0; si < index.length; si++) {
if (search.toLowerCase().includes(index[si].toLowerCase())) {
splitIndex[si] = `<mark>${index[si]}</mark>`;
text[i] = splitIndex.join("");
}
}
}
}
searchElement.innerHTML = text.join(" ");
}
});
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<title>Search Menu</title>
</head>
<body>
<p id="text">The quick brown fox jumped over the lazy dog.</p>
<button id="searchBtn">Search</button>
</body>
</html>

您还可以使用 window.find() 在您的页面中查找字符串:

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<title>Search Menu</title>
</head>
<body>
<p id="text">The quick brown fox jumped over the lazy dog.</p>
<button id="searchBtn">Search</button>
</body>

<script type="text/javascript">
const searchBtn = document.getElementById("searchBtn");

searchBtn.addEventListener("click", function () {
const search = prompt("Search");
const searchResult = window.find(search);
});
</script>
</html>

关于javascript - 我可以通过 JavaScript 以编程方式打开浏览器的 native 搜索对话框吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52835530/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com