Chrome扩展JavaScript功能
以下是我试图在网页上查找单词的代码。但是,我无法获得所需的功能。单击浏览器中的扩展图标时,警报消息会显示“未找到”,关闭警报窗口时,文本框和按钮会出现,单击按钮时,不会发生任何情况 所需功能:用户输入文本并单击按钮,然后如果找不到该单词,则会出现警告消息 需要建议才能让它工作。谢谢 index.htmlChrome扩展JavaScript功能,javascript,google-chrome,google-chrome-extension,Javascript,Google Chrome,Google Chrome Extension,以下是我试图在网页上查找单词的代码。但是,我无法获得所需的功能。单击浏览器中的扩展图标时,警报消息会显示“未找到”,关闭警报窗口时,文本框和按钮会出现,单击按钮时,不会发生任何情况 所需功能:用户输入文本并单击按钮,然后如果找不到该单词,则会出现警告消息 需要建议才能让它工作。谢谢 index.html <!doctype html> <html> <head> <title>Word Finder</title> <style&
<!doctype html>
<html>
<head>
<title>Word Finder</title>
<style>
body {
font-family: "Segoe UI", "Lucida Grande", Tahoma, sans-serif;
font-size: 100%;
}
#status {
white-space: pre;
text-overflow: ellipsis;
overflow: hidden;
max-width: 600px;
display: flex;
}
</style>
<script src="popup.js"></script>
</head>
<body>
<div id="status">
<input class="word" type="text"></input>
<button class="btn" type="button">Search</button>
</div>
</body>
</html>
查词器
身体{
字体系列:“Segoe UI”,“Lucida Grande”,Tahoma,无衬线;
字体大小:100%;
}
#地位{
空白:预处理;
文本溢出:省略号;
溢出:隐藏;
最大宽度:600px;
显示器:flex;
}
搜寻
content.js
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");
};
var n=0;
函数findInPage(str){
log(“我们在这里”);
var-txt,i,found;
如果(str==“”){
返回false;
}
//在页面上查找给定字符串的下一个实例,并环绕到
//如有必要,请从页面开始。
if(window.find){
//查找从当前点开始的匹配项。如果未找到,请倒带
//回到第一场比赛。
如果(!window.find(str)){
while(window.find(str、false、true)){
n++;
}
}否则{
n++;
}
//如果两个方向都找不到,请给出消息。
如果(n==0){
警报(“未找到”);
}
}else if(window.document.body.createTextRange){
txt=window.document.body.createTextRange();
//从页面顶部查找第n个匹配项。
发现=真;
i=0;
while(发现===true&&i 0){
n=0;
findInPage(str);
}
//到处都找不到,给我留言。还有吗
警报(“未找到”);
}
}
返回false;
}
window.onload=function(){
console.log(“加载功能”);
var bt=document.getElementsByClassName('btn');
bt.addEventListener('click',findInPage());
log(“函数调用后”);
};
请阅读chrome extensions architecture文章:您需要一个内容脚本。@wOxxOm谢谢。我已经更新了代码。现在,在这里,如果我硬编码一个字符串,给定的字符串如果在网页上找到就会突出显示。但是,我无法从扩展的UI窗格中的文本框中获取字符串。这可能是我遗漏的东西,但我不确定现在是什么。也许,我应该用别的东西,而不是窗口。加载!!让它工作起来。必须重新组织js文件中的代码。