pxkw.hatenadiary.com

めもめも

Bookmarklet to fill forms matched to a regex

We can not use a regex in getElementsByXXX("/^like this*/").
So we have to get superset of what we need and then evaluate each element whether its name (or tagname or something) matches to the regex.

Example

  <form>
    <input type="text" name="target0" value="target0" />
    <input type="text" name="target1" value="target1" />
    <input type="text" name="dummy0"  value="dummy0"  />
    <input type="text" name="target2" value="target2" />
    <input type="text" name="target3" value="target3" />
    <input type="text" name="dummy1"  value="dummy1"  />
  </form>
  
  <button id="changebtn">change</button>

  <script>
    document.getElementById("changebtn").onclick = function(){
        var es = document.getElementsByTagName("input");
          for( var i=0; i<es.length; i++ ){
              var e = es[i];
              if( e.name.match(/^target.*/) ) {
                  e.value = "changed!";

                  /*somehow necessary for scriptlet on my environment*/
                  console.log(); 
              }
          }
    }
  </script>












How to register the script as a bookmarklet

  1. Show bookmark-toolbar (Ctrl-Shift-b)
  2. Rclick -> Add page...
    • URL : javascript:{ [PASTE THE SCRIPT IN A LINE] }