简体   繁体   English

书签来替换随机名称输入字段的输入值

[英]Bookmarklet to replace input values of random name input fields

I would like to create a bookmarklet that replaces all values of every input field on a webpage. 我想创建一个小书签来替换网页上每个输入字段的所有 There are a lot ot them but they can be split into two categories by their name . 它们有很多,但可以按名称分为两类。

Example: 例:

<input class="Class1" name="ABC_randomnumbers" value="randomnumber" type="text">
<input class="Class1" name="DEF_randomnumbers" value="randomnumber" type="text">

Input fields with its names starting with ABC would receive the value X, input fields with its names starting with DEF would receive the value Y. I tried 名称以ABC开头的输入字段将接收值X,名称以DEF开头的输入字段将接收值Y。

javascript:document.body.innerHTML=document.body.innerHTML.replace(/input class="Class1" name="ABC_(\d+)" value="\d+"/g,'input class="Class1" name="ABC_$1" value="X"'); document.body.innerHTML=document.body.innerHTML.replace(/input class="Class1 name="DEF_(\d+)" value="\d+"/g,'input class="Class1" name="DEF_$1" value="Y"');

to replace all occurences of these strings in the HTML but it messes up the whole page, for example it deletes the contents of the tag. 替换HTML中这些字符串的所有出现,但会弄乱整个页面,例如,它删除了标记的内容。 I read something about getElementsByName , but they don't have the same name and I can't get regexp work with getElementsByName . 我读了一些有关getElementsByName的内容 ,但是它们的名称不同,我无法使用getElementsByName进行正则表达式工作。

I would refrain from editing the base HTML in such a way. 我将避免以这种方式编辑基本HTML。 A better option is to edit the page with Javascript code. 更好的选择是使用Javascript代码编辑页面。 I provided an example below: 我在下面提供了一个示例:

var allInputs = document.getElementsByTagName("input");

for (var i = 0; i < allInputs.length; i++) {
    if(allInputs[i].name.startsWith("ABC_")) {
        //Do Logic here
    }

    //Other cases here
}

Shorthand version provided below: 以下是简写版本:

Array.from(document.getElementsByTagName("input")).forEach(function(input) {
    if(input.name.startsWith("ABC_")) {
        //Do Logic here
    }

    //Other cases here
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM