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

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



FFIVE
浏览 141回答 1
1回答

慕码人2483693

这是一个肮脏的脚本,可能是一个起点。它仅适用于您作为示例提供的特定表单。它用于document.querySelector定位表单元素。只要您打开表单,它就会填写、提交、返回、提交,一遍又一遍。要使用它:在 Google Chrome 中安装TamperMonkey扩展单击浏览器中出现的图标,选择“仪表板”创建一个新脚本,将所有内容替换为下面的代码Ctrl + S 保存在选项卡中打开表单并观察它的工作代码:// ==UserScript==// @name&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;GoogleForm Spammer// @namespace&nbsp; &nbsp; http://tampermonkey.net/// @version&nbsp; &nbsp; &nbsp; 0.1// @description&nbsp; Spam a Google Form// @author&nbsp; &nbsp; &nbsp; &nbsp;You// @match&nbsp; &nbsp; &nbsp; &nbsp; https://docs.google.com/forms/*// @grant&nbsp; &nbsp; &nbsp; &nbsp; unsafeWindow// ==/UserScript==(function() {&nbsp; window.addEventListener('load', function() {&nbsp; &nbsp; if (window.location.pathname.indexOf('/forms/d') === 0) { // If we're on the form page&nbsp; &nbsp; &nbsp; submitRandomForm();&nbsp; &nbsp; } else if (window.location.pathname.indexOf('/forms/u') === 0) { // If we're on the "submitted" page&nbsp; &nbsp; &nbsp; goBackToForm();&nbsp; &nbsp; }&nbsp; &nbsp; function submitRandomForm() {&nbsp; &nbsp; &nbsp; // Size&nbsp; &nbsp; &nbsp; var radios&nbsp; &nbsp; &nbsp;= document.querySelectorAll(".appsMaterialWizToggleRadiogroupRadioButtonContainer"),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; radioIndex = Math.floor(Math.random() * radios.length);&nbsp; &nbsp; &nbsp; radios[radioIndex].click();&nbsp; &nbsp; &nbsp; // Print&nbsp; &nbsp; &nbsp; var checkboxes&nbsp; &nbsp; = document.querySelectorAll(".appsMaterialWizTogglePapercheckboxCheckbox"),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; checkboxIndex = Math.floor(Math.random() * checkboxes.length);&nbsp; &nbsp; &nbsp; checkboxes[checkboxIndex].click();&nbsp; &nbsp; &nbsp; // Age (between 16 and 45)&nbsp; &nbsp; &nbsp; var age = Math.floor(Math.random() * 30) + 16;&nbsp; &nbsp; &nbsp; document.querySelector(".quantumWizTextinputPaperinputInput").value = age;&nbsp; &nbsp; &nbsp; // Submit&nbsp; &nbsp; &nbsp; document.querySelector(".freebirdFormviewerViewCenteredContent .appsMaterialWizButtonPaperbuttonLabel").click();&nbsp; &nbsp; }&nbsp; &nbsp; function goBackToForm() {&nbsp; &nbsp; &nbsp; window.location.href = 'https://docs.google.com/forms/d/e/1FAIpQLSd7GueJGytOiQpkhQzo_dCU0oWwbk3L1htKblBO1m14VHSpHw/viewform';&nbsp; &nbsp; }&nbsp; });})();这是一个更清洁的方法。您在顶部声明表单 URL、表单字段,并且对于其中的一些,一个函数将根据您的需要返回一个随机值。要尝试这个,请保存该脚本,然后尝试访问此表单:// ==UserScript==// @name&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;GoogleForm Spammer// @namespace&nbsp; &nbsp; http://tampermonkey.net/// @version&nbsp; &nbsp; &nbsp; 0.1// @description&nbsp; Spam a Google Form// @author&nbsp; &nbsp; &nbsp; &nbsp;You// @match&nbsp; &nbsp; &nbsp; &nbsp; https://docs.google.com/forms/*// @grant&nbsp; &nbsp; &nbsp; &nbsp; none// ==/UserScript==var formUrl = 'https://docs.google.com/forms/d/e/1FAIpQLSdQ9iT7isDU8IIbyg-wowB-9HGzyq-xu2NyzsOeG0j8fhytmA/viewform';var formSchema = [&nbsp; &nbsp; {type: 'radio'},&nbsp; &nbsp; &nbsp; // A&nbsp; &nbsp; {type: 'radio'},&nbsp; &nbsp; &nbsp; // B&nbsp; &nbsp; {type: 'checkbox'},&nbsp; &nbsp;// C&nbsp; &nbsp; {type: 'checkbox'},&nbsp; &nbsp;// D&nbsp; &nbsp; {type: 'short_text', func: generateAnswerE },&nbsp; &nbsp;// E&nbsp; &nbsp; {type: 'paragraph', func: generateParagraph },&nbsp; // F];function generateAnswerE() {&nbsp; // Let's say we want a random number&nbsp; return Math.floor(Math.random() * 30) + 16;}function generateParagraph() {&nbsp; // Just for the example&nbsp; return "Hello world";}(function() {&nbsp; window.addEventListener('load', function() {&nbsp; &nbsp; if (window.location.pathname.indexOf('/forms/d') === 0) { // If we're on the form page&nbsp; &nbsp; &nbsp; submitRandomForm();&nbsp; &nbsp; } else if (window.location.pathname.indexOf('/forms/u') === 0) { // If we're on the "submitted" page&nbsp; &nbsp; &nbsp; window.location.href = formUrl;&nbsp; &nbsp; }&nbsp; &nbsp; function submitRandomForm() {&nbsp; &nbsp; &nbsp; var formItems = document.querySelectorAll('.freebirdFormviewerViewItemsItemItem');&nbsp; &nbsp; &nbsp; for (var i = 0; i < formSchema.length; i++) {&nbsp; &nbsp; &nbsp; &nbsp; var field = formSchema[i],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; item&nbsp; = formItems[i];&nbsp; &nbsp; &nbsp; &nbsp; switch(field.type) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case 'radio':&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var radios&nbsp; &nbsp; &nbsp;= item.querySelectorAll(".appsMaterialWizToggleRadiogroupRadioButtonContainer"),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; radioIndex = Math.floor(Math.random() * radios.length);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; radios[radioIndex].click();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case 'checkbox':&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var checkboxes&nbsp; &nbsp; = item.querySelectorAll(".appsMaterialWizTogglePapercheckboxCheckbox"),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; checkboxIndex = Math.floor(Math.random() * checkboxes.length);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; checkboxes[checkboxIndex].click();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case 'short_text':&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; item.querySelector(".quantumWizTextinputPaperinputInput").value = field.func();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case 'paragraph':&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; item.querySelector(".quantumWizTextinputPapertextareaInput").value = field.func();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; // Submit&nbsp; &nbsp; &nbsp; document.querySelector(".freebirdFormviewerViewCenteredContent .appsMaterialWizButtonPaperbuttonLabel").click();&nbsp; &nbsp; }&nbsp; });})();
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript