最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to autofill Google form with random answears using JS - Stack Overflow

programmeradmin4浏览0评论

I want to randomly fill a 100 google forms using JS. Is any way to do it? There is example google form. I couldn't find anything on stackoverflow or web, only python or java solutions. But I want to do it in javascript if it is possible of course.

I want to randomly fill a 100 google forms using JS. Is any way to do it? There is example google form. I couldn't find anything on stackoverflow or web, only python or java solutions. But I want to do it in javascript if it is possible of course.

Share Improve this question asked Mar 22, 2020 at 19:03 svennnx3svennnx3 832 gold badges2 silver badges9 bronze badges 5
  • You can use a browser extension like TamperMonkey or GreaseMonkey to run a script of yours automatically on the page. Then, you just need to write a script which fills the form, submits it, and does that over and over. But... maybe Google will block you, seeing you're not a human and are spamming, which is great – blex Commented Mar 22, 2020 at 19:06
  • @blex but how can i do that, google doesnt have any id in any field, i have only jscontroller which seems to be some kind of ID but is it id for real ? I don't know how to call getDocumentById when i dont have any id – svennnx3 Commented Mar 22, 2020 at 19:14
  • Another problem is that, when i have more than 1 radio field or more than 1 input etc how can i chose which is what. Because i can only refer by id or by tag, but there is a lot divs . To be honest there are only divs, nothing more – svennnx3 Commented Mar 22, 2020 at 19:34
  • You won't be able to use getElementById here, of course. But querySelector will help you. I successfully made a dirty script to fill the form you provided as an example (you may see some answers). I'll try to clean it up and write an answer – blex Commented Mar 22, 2020 at 19:37
  • @blex Yeah i can see, you made like 40 answears. If you will be so kind and give me that dirty script :D My form is more plicated than this one , but i think that when i see how it really works i can write something like this on my own using your template – svennnx3 Commented Mar 22, 2020 at 19:39
Add a ment  | 

1 Answer 1

Reset to default 3

Here is a dirty script, which could be a starting point. It only works with the specific form you provided as an example. It uses document.querySelector to target the form elements.

As soon as you'll open the form, it will fill it, submit it, go back to it, submit it, over and over.

To use it:

  • Install the TamperMonkey extension in Google Chrome
  • Click on the icon that appeared in your browser, select "Dashboard"
  • Create a new script, and replace all the content with the code below
  • Ctrl + S to save
  • Open the form in a tab and watch it do the work

Code:

// ==UserScript==
// @name         GoogleForm Spammer
// @namespace    http://tampermonkey/
// @version      0.1
// @description  Spam a Google Form
// @author       You
// @match        https://docs.google./forms/*
// @grant        unsafeWindow
// ==/UserScript==

(function() {
  window.addEventListener('load', function() {
    if (window.location.pathname.indexOf('/forms/d') === 0) { // If we're on the form page
      submitRandomForm();
    } else if (window.location.pathname.indexOf('/forms/u') === 0) { // If we're on the "submitted" page
      goBackToForm();
    }

    function submitRandomForm() {
      // Size
      var radios     = document.querySelectorAll(".appsMaterialWizToggleRadiogroupRadioButtonContainer"),
          radioIndex = Math.floor(Math.random() * radios.length);
      radios[radioIndex].click();

      // Print
      var checkboxes    = document.querySelectorAll(".appsMaterialWizTogglePapercheckboxCheckbox"),
          checkboxIndex = Math.floor(Math.random() * checkboxes.length);
      checkboxes[checkboxIndex].click();

      // Age (between 16 and 45)
      var age = Math.floor(Math.random() * 30) + 16;
      document.querySelector(".quantumWizTextinputPaperinputInput").value = age;

      // Submit
      document.querySelector(".freebirdFormviewerViewCenteredContent .appsMaterialWizButtonPaperbuttonLabel").click();
    }

    function goBackToForm() {
      window.location.href = 'https://docs.google./forms/d/e/1FAIpQLSd7GueJGytOiQpkhQzo_dCU0oWwbk3L1htKblBO1m14VHSpHw/viewform';
    }
  });
})();

And here is a little cleaner way. You declare the form URL at the top, the form fields, and for some of them, a function which will return a random value according to your needs.

To try this one out, save that script, and try accessing this form:

// ==UserScript==
// @name         GoogleForm Spammer
// @namespace    http://tampermonkey/
// @version      0.1
// @description  Spam a Google Form
// @author       You
// @match        https://docs.google./forms/*
// @grant        none
// ==/UserScript==

var formUrl = 'https://docs.google./forms/d/e/1FAIpQLSdQ9iT7isDU8IIbyg-wowB-9HGzyq-xu2NyzsOeG0j8fhytmA/viewform';
var formSchema = [
    {type: 'radio'},      // A
    {type: 'radio'},      // B
    {type: 'checkbox'},   // C
    {type: 'checkbox'},   // D
    {type: 'short_text', func: generateAnswerE },   // E
    {type: 'paragraph', func: generateParagraph },  // F
];

function generateAnswerE() {
  // Let's say we want a random number
  return Math.floor(Math.random() * 30) + 16;
}

function generateParagraph() {
  // Just for the example
  return "Hello world";
}

(function() {
  window.addEventListener('load', function() {
    if (window.location.pathname.indexOf('/forms/d') === 0) { // If we're on the form page
      submitRandomForm();
    } else if (window.location.pathname.indexOf('/forms/u') === 0) { // If we're on the "submitted" page
      window.location.href = formUrl;
    }

    function submitRandomForm() {
      var formItems = document.querySelectorAll('.freebirdFormviewerViewItemsItemItem');

      for (var i = 0; i < formSchema.length; i++) {
        var field = formSchema[i],
            item  = formItems[i];
        switch(field.type) {
            case 'radio':
                var radios     = item.querySelectorAll(".appsMaterialWizToggleRadiogroupRadioButtonContainer"),
                    radioIndex = Math.floor(Math.random() * radios.length);
                radios[radioIndex].click();
                break;
            case 'checkbox':
                var checkboxes    = item.querySelectorAll(".appsMaterialWizTogglePapercheckboxCheckbox"),
                    checkboxIndex = Math.floor(Math.random() * checkboxes.length);
                checkboxes[checkboxIndex].click();
                break;
            case 'short_text':
                item.querySelector(".quantumWizTextinputPaperinputInput").value = field.func();
                break;
            case 'paragraph':
                item.querySelector(".quantumWizTextinputPapertextareaInput").value = field.func();
                break;
        }
      }

      // Submit
      document.querySelector(".freebirdFormviewerViewCenteredContent .appsMaterialWizButtonPaperbuttonLabel").click();
    }
  });
})();
发布评论

评论列表(0)

  1. 暂无评论