简体   繁体   English

如何从Chrome扩展程序中查找文本框

[英]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属性( textpasswordsearchradiocheckbox ,...)的<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.

相关问题 如何在onblur事件中验证文本框? - How to validate text-boxes in onblur event? 如何将 JavaScript 应用于具有不同 ID 的文本框数组? - How to apply JavaScript to an array of text-boxes with different IDs? 在laravel框架中求和并乘以文本框 - Sum and multiply text-boxes in laravel framework jQuery-如果选中1个复选框,则一次启用4个文本框 - JQuery - enable 4 text-boxes at once if 1 checkbox is checked 将一些文本框中的内容邮寄到带有html的gmail - mailing content in a few text-boxes to a gmail with html 在两个文本框上进行验证或输入验证 - ASP.NET Webforms - Either or type validation on 2 text-boxes - ASP.NET Webforms JS从文本框中获取文本,传递给asp.net mvc ActionResult,但ActionResult参数显示为null - JS grabbing text from text-boxes, passing to asp.net mvc ActionResult but ActionResult parameters appear null 根据选中的复选框计算文本框中的值总和 - Calculate sum of values on text-boxes based on checkbox checked 动态添加其他文本框时无法保留旧文本框的数据 - Cannot retain the data of old Text-boxes on dynamic addition of others 当从下拉菜单中选择某些项目时,会出现不断变化的文本框/下拉菜单 - Having changing text-boxes/drop-downs appear when certain item is picked from a drop-down menu
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM