How to autofill Google form with random answears using JS

只愿长相守 提交于 2021-01-01 08:42:34

问题


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.


回答1:


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.net/
// @version      0.1
// @description  Spam a Google Form
// @author       You
// @match        https://docs.google.com/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.com/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.net/
// @version      0.1
// @description  Spam a Google Form
// @author       You
// @match        https://docs.google.com/forms/*
// @grant        none
// ==/UserScript==

var formUrl = 'https://docs.google.com/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();
    }
  });
})();


来源:https://stackoverflow.com/questions/60803718/how-to-autofill-google-form-with-random-answears-using-js

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!