如何使用 JS 使用随机答案自动填充 Google 表单

How to autofill Google form with random answears using JS

我想用 JS 随机填写 100 google 个表格。有什么办法吗? 有example google form。 我在 Whosebug 或网络上找不到任何东西,只有 python 或 java 解决方案。但如果可能的话,我当然想在 java 脚本中完成。

这是一个脚本,可以作为起点。它 适用于您作为示例提供的特定表格。它使用 document.querySelector 来定位表单元素。

一旦您打开表格,它就会填写、提交、返回、提交,一遍又一遍。

要使用它:

  • 在 Google Chrome
  • 中安装 TamperMonkey 扩展
  • 单击浏览器中显示的图标,select“仪表板”
  • 新建脚本,将所有内容替换为以下代码
  • Ctrl + S 保存
  • 在选项卡中打开表单并观察它的工作

代码:

// ==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';
    }
  });
})();

这里有一个更简洁的方法。您在顶部声明表单 URL,表单字段,对于其中一些,一个函数将根据您的需要 return 一个随机值。

要试一试,请保存该脚本,然后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();
    }
  });
})();