是否可以从网页内部以编程方式打开网络浏览器的 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/