0

I have code to autofill form fields

(function( window ) {
    "use strict";
    var document = window.document,
        fieldValueMap = {
              "author"       : "author"
            , "name"        : "name"
            , "email"       : "email@gmail.com"
            , "url"         : "url.link"
            , "website"     : "website.com"
            , "comment"     : "comment"
            , "commentName"    : "commentName"
            , "commentEmail"  : "commentEmail@gmail.com"
            , "commentWebsite"   : "commentWebsite.com"
            , "commentComment"         : "commentComment"
        };
  
      Object.keys( fieldValueMap ).forEach(function( name ){
  
          var input = document.querySelector( "form input[id='" + name + "']" )
                          || document.querySelector( "form select[id='" + name + "']" )
              || document.querySelector( "form textarea[id='" + name + "']" );
  
          input && input.type !== "hidden" && ( input.value = fieldValueMap[ name ] );
      });
  
  })(window);

But it doesn't work properly when there is iframe on the website page. It works only when I'm manually choosing "evaluation context" and put the code in console. like that result

So I wonder if there is a way to automatize the process of filling forms in iframe and make the whole script work together. Perfectly I'd like to use final script as a bookmarklet. My first coding experience, found the code on the internet and customized it for my needs. I'm sorry if the question is too stupid. Any ideas?

  • Does this answer your question? [Invoking JavaScript code in an iframe from the parent page](https://stackoverflow.com/questions/251420/invoking-javascript-code-in-an-iframe-from-the-parent-page) – evolutionxbox Jan 23 '23 at 12:41
  • To get element by ID use `document.getElementById()`. To access iframe's content follow above comment. Code will be something like this: `iframeElement.contentDocument.getElementById()` – Jan Pfeifer Jan 23 '23 at 12:47

0 Answers0