以下是我试图在网页上找到单词的代码。但是,我无法获得所需的功能。当单击浏览器中的扩展图标时,警报消息会出现" Not Found",当警报窗口关闭时,文本框和按钮出现,当单击按钮时没有任何反应。
所需功能:用户输入文本并单击按钮,如果未找到该单词,则应显示警告消息。
需要建议才能让它发挥作用。感谢
的index.html
var n = 0;
function findInPage(str) {
console.log("We are here");
var txt, i, found;
if (str == "") {
return false;
}
// Find next occurance of the given string on the page, wrap around to the
// start of the page if necessary.
if (window.find) {
// Look for match starting at the current point. If not found, rewind
// back to the first match.
if (!window.find(str)) {
while (window.find(str, false, true)) {
n++;
}
} else {
n++;
}
// If not found in either direction, give message.
if (n == 0) {
alert("Not found.");
}
} else if (window.document.body.createTextRange) {
txt = window.document.body.createTextRange();
// Find the nth match from the top of the page.
found = true;
i = 0;
while (found === true && i <= n) {
found = txt.findText(str);
if (found) {
txt.moveStart("character", 1);
txt.moveEnd("textedit");
}
i += 1;
}
// If found, mark it and scroll it into view.
if (found) {
txt.moveStart("character", -1);
txt.findText(str);
txt.select();
txt.scrollIntoView();
n++;
} else {
// Otherwise, start over at the top of the page and find first match.
if (n > 0) {
n = 0;
findInPage(str);
}
// Not found anywhere, give message. else
alert("Not found.");
}
}
return false;
}
window.onload=function(){
console.log("On load function");
var bt = document.getElementsByClassName('btn');
bt.addEventListerner('click',findInPage());
console.log("After function call");
};
content.js
{{1}}