【问题标题】:How to autofill Google form with random answears using JS如何使用 JS 使用随机答案自动填充 Google 表单
【发布时间】:2020-07-03 08:02:50
【问题描述】:

我想使用 JS 随机填写 100 个 google 表单。有什么办法吗? 有example google form。 我在 stackoverflow 或 web 上找不到任何东西,只有 python 或 java 解决方案。但是如果可能的话,我当然想用javascript来做。

【问题讨论】:

  • 您可以使用TamperMonkeyGreaseMonkey 等浏览器扩展程序在页面上自动运行您的脚本。然后,您只需要编写一个脚本来填写表单、提交它并一遍又一遍地执行此操作。但是......也许谷歌会阻止你,因为你不是人类并且正在发送垃圾邮件,这很棒
  • @blex 但我该怎么做,谷歌在任何领域都没有任何 id,我只有 jscontroller 这似乎是某种 ID,但它是真实的 id 吗?当我没有任何 id 时,我不知道如何调用 getDocumentById
  • 另一个问题是,当我有超过 1 个无线电字段或超过 1 个输入等时,我该如何选择哪个是什么。因为我只能通过 id 或 tag 来引用,但是有很多 div。老实说,只有 div,仅此而已
  • 你当然不能在这里使用getElementById。但是querySelector 会帮助你。我成功地编写了一个脏脚本来填写您作为示例提供的表单(您可能会看到一些答案)。我会尝试清理它并写一个答案
  • @blex 是的,我知道,你做了 40 个回答。如果你好心,给我那个脏脚本:D 我的表格比这个更复杂,但我认为当我看到它是如何工作的时,我可以使用你的模板自己编写类似的东西

标签: javascript forms google-forms autofill


【解决方案1】:

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

只要您打开表单,它就会填写、提交、返回、提交,一遍又一遍。

使用它:

  • 在 Google Chrome 中安装 TamperMonkey 扩展程序
  • 点击浏览器中出现的图标,选择“仪表板”
  • 新建脚本,将所有内容替换为以下代码
  • 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、表单字段,其中一些是一个函数,该函数将根据您的需要返回一个随机值。

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

【讨论】:

  • 它工作得很好,现在我必须将它缩放到更大的形式,我有很多收音机和复选框等它应该很容易,对吧?
  • 用肮脏的方式做这件事很容易。不过,让它更易于维护/编辑以使用任何形式会很好。试一试
  • @svennnx3 我添加了第二个示例,这将更容易适应 any 表单(它只支持某些字段类型,因为我无法让它工作还带有下拉菜单)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-10-18
  • 1970-01-01
  • 1970-01-01
  • 2013-05-23
  • 2015-04-14
  • 2020-05-02
  • 1970-01-01
相关资源
最近更新 更多