googleform

2020/8/20 下午2:06:11

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey,才能安装此脚本。

您需要先安装用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name        googleform
// @namespace   Violentmonkey Scripts
// @match       *://docs.google.com/*
// @grant       none
// @version     1.2
// @author      -
// @require    https://code.jquery.com/jquery-1.12.4.min.js
// @description 2020/8/20 下午2:06:11
// ==/UserScript==


(function () {
  'use strict';

  // 邮件地址
  const email = '[email protected]'

  // 设置的插入规则
  /**
  * key 指想配置的栏目的关键字,或者正则
  * value 指匹配到栏目需要自动填写到内容
  */
  const fillList = [
    {
      key: 'email',
      value: email,
    },
    {
      key: 'adress',
      value: '0x000000000',
    },
    {
      key: 'id',
      value: 'test',
    },
  ]

  // 查找input的关联标题
  const findInputHeaderText = inputObj => {
    let obj = $(inputObj)
    for(let i=0;obj=obj.parent();i++) {
      if (obj.prev().attr('class') === 'freebirdFormviewerComponentsQuestionBaseHeader') {
        return obj.prev().text().toLowerCase()
      }
      if (i >= 10) return '' // 防止卡死
    }
  }

  // 使页面识别输入
  const fireEvents = element => {
    ['input', 'click', 'change', 'blur'].forEach((event) => {
       const changeEvent = new Event(event, { bubbles: true, cancelable: true })
       element.dispatchEvent(changeEvent);
     })
  }

  $(document).ready(() => {
    $('input').each((index,element) => {
      // 填充邮箱
      if (element.type === 'email') {
        $(element).val(email)
        fireEvents(element)
      }
      // 填充text
      if (element.type === 'text') {
        fillList.forEach(item => {
          if (findInputHeaderText(element).indexOf(item['key']) !== -1) {
            // console.log(item['key'])
            $(element).val(item['value'])
            fireEvents(element)
          }
        })
      }
    })
  })

})();