如何使用JS自动将Google表格与随机答录一起填写

问题描述 投票:0回答:1

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

javascript forms google-form autofill
1个回答
1
投票

这里是一个dirty脚本,可能是一个起点。它only与您作为示例提供的特定表格一起使用。它使用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++) {console.log('hi');
        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();
    }
  });
})();
© www.soinside.com 2019 - 2024. All rights reserved.