自动填充表格没有ID TAMPERMONKEY

发布于 2025-01-21 13:42:00 字数 621 浏览 1 评论 0原文

我是Tampermonkey的新手,我想自动填写表格,但没有ID ...我已经尝试了一些代码,但没有工作

<input type="text" name="user_email" class="form-control-input" value="" placeholder="Enter your Email" onselectstart="return false" onpaste="return false;" oncopy="return false" oncut="return false" ondrag="return false" ondrop="return false" autocomplete="off">

代码已经测试过

  var mail = "[email protected]";
  document.getElementsByClassName('form-control-input').value = mail;

i'm a newbie with tampermonkey and i want to auto fill a form but has no id ...i already tried some codes but doesnt work

source

<input type="text" name="user_email" class="form-control-input" value="" placeholder="Enter your Email" onselectstart="return false" onpaste="return false;" oncopy="return false" oncut="return false" ondrag="return false" ondrop="return false" autocomplete="off">

code already tested

  var mail = "[email protected]";
  document.getElementsByClassName('form-control-input').value = mail;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

错爱 2025-01-28 13:42:00

我个人更喜欢在TampermonKey中使用纯JavaScript而不是使用jQuery,因为它可以使我更有控制力,例如在DOM准备就绪时加载脚本。 IE将函数包装在$(文档)中。dready(function()...

您可以尝试一下(确保更改@match)

I personally prefer to use jQuery over pure javascript in tampermonkey because it gives me more control such as loading the script when the DOM is ready. i.e. wrap the function in a $(document).ready(function() ...

you can try this out (make sure to change the @match)????

// ==UserScript==
// @name         New Userscript
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  try to take over the world!
// @author       You
// @match        <your site here>
// @icon         https://www.google.com/s2/favicons?sz=64&domain=undefined.
// @grant        none
// @require http://code.jquery.com/jquery-3.4.1.min.js
// ==/UserScript==

(function() {
    'use strict';

    $(document).ready(function() {
        var mail = "[email protected]";
        $('[name=user_email]').val(mail);
    });
})();
栖竹 2025-01-28 13:42:00

更改class <代码> ID 或queryselector。使用getElementsByClassName()将返回Nodelist的集合。因此,在这种情况下考虑一个数组。如果您使用getElementsByClassName()您告诉dom “嘿,我想处理许多事情” ,所以要添加string 对“这些事物的数量” ,您需要一个一个一个一个,并给他们value即,您需要循环浏览此节点符号并执行您想要的“这些数量”

在您的情况下,何时使用getElementsByClassName

,由于您仅处理一个输入,因此更轻松地使用id 进行选择。如果您说的是10个不同的输入您需要使用prefl的字段,则使用getelementsbyclassName(),但是如果您只有2个或2个或3个字段,我建议只给每个字段一个唯一的id,然后使用

  var mail = "[email protected]";
  document.getElementById('form-control-input').value = mail

 
<input type="text" name="user_email" id="form-control-input" value="" placeholder="Enter your Email" onselectstart="return false" onpaste="return false;" oncopy="return false" oncut="return false" ondrag="return false" ondrop="return false" autocomplete="off">

Change class to id or querySelector. Using getElementsByClassName()will return a collection of NodeList. So think of an array in this instance. If you use getElementsByClassName() you're telling the DOM "Hey I want to work on a number of things" so in order to add that string value to " these number of things", you need to go through them one by one and give them the value i.e you will need to loop through this NodeList and do what you want on "these number of things".

When to use getElementsByClassName

In your case, since you're only working on one input, it's easier to select using id and go with that. If you have say 10 different input fields that you need to use a prefill for then use getElementsByClassName() but if you only have like 2 or 3 fields i recommend just giving each one a unique id and using that

  var mail = "[email protected]";
  document.getElementById('form-control-input').value = mail

 
<input type="text" name="user_email" id="form-control-input" value="" placeholder="Enter your Email" onselectstart="return false" onpaste="return false;" oncopy="return false" oncut="return false" ondrag="return false" ondrop="return false" autocomplete="off">

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文