googleform

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

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

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

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 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)
          }
        })
      }
    })
  })

})();