[英]How to find text-boxes from a Chrome extension
I'm making a Chrome extension and I need it to find any form text-boxes on the webpage. 我正在制作一个Chrome扩展程序,我需要它来在网页上找到任何表单文本框。
I first tried doing it in my content script like: 我首先尝试在我的内容脚本中执行此操作,例如:
document.getElementsByTag("text");
But it always returns undefined
so I'm assuming it's searching only my_app.html
. 但是它总是返回undefined
因此我假设它仅在搜索my_app.html
。
My first question is what function should I use: GetElmentsByTag, by name, by className, or with some other function? 我的第一个问题是我应该使用哪个函数:GetElmentsByTag,按名称,按className或与其他函数一起使用?
My second question is which JavaScript file do I need to search? 我的第二个问题是我需要搜索哪个JavaScript文件? Other forums with similar problems said you need to access the DOM in another file like background or popup and then send it to the content script with a message and a listener. 其他存在类似问题的论坛表示,您需要访问另一个文件(如背景或弹出窗口)中的DOM,然后将其与消息和侦听器一起发送到内容脚本。
First, you are calling a function that doesn't exist. 首先,您要调用一个不存在的函数。 You probably mean Document.getElementsByTagName()
or Document.querySelectorAll()
, as you can see here: 您可能是指Document.getElementsByTagName()
或Document.querySelectorAll()
,如您在此处看到的:
console.log(document.getElementsByTagName); console.log(document.querySelectorAll); console.log(document.getElementsByTag);
Also, your selector is wrong: There's no <text>
HTML element. 另外,您的选择器是错误的:没有<text>
HTML元素。
If you want all <input>
fields, with any type
attribute ( text
, password
, search
, radio
, checkbox
, ...) then you could use any of these: 如果要所有具有任何type
属性( text
, password
, search
, radio
, checkbox
,...)的<input>
字段,则可以使用以下任意一种:
document.getElementsByTagName('input');
document.querySelectorAll('input');
If you just want <input>
s of a specific type, let's say text
, you could do: 如果只需要特定类型的<input>
,例如text
,则可以执行以下操作:
document.querySelectorAll('input[type="text"]')
Lastly, you can concatenate multiple selectors with ,
if you want <input>
s of different types: 最后,您可以连接多个选择有,
如果你想要<input>
不同类型的S:
document.querySelectorAll('input[type="text"], input[type="password"]')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.