订餐问卷一键填写

订餐问卷一键填写, 不要忘记订餐😂

目前為 2020-04-22 提交的版本,檢視 最新版本

// ==UserScript==
// @name         订餐问卷一键填写
// @version      0.0.1
// @namespace    ly525
// @description  订餐问卷一键填写, 不要忘记订餐😂
// @author       ly525
// @match        *://*.www.wenjuan.com/*
// @run-at       document-end
// @license      MIT
// ==/UserScript==
(function(){

  var config = {
    name: '刘岩',
    breakfast: '公司用餐',
    lunch: '公司盒饭',
    dinner: '公司盒饭',
    location: '舜天',
    department: 'TaurusX Group'
  }

  var nameLabel = '姓名'
  var locationLabel = '办公地点'
  var breakfastLabel = '早餐'
  var lunchLabel = '午餐'
  var dinnerLabel = '晚餐'
  var departmentLabel = '部门'

  function fillName(el) {
    el.querySelector('input[type=text]').value = config.name
  }

  function fillOption(el, labelKey) {
    // el.querySelectorAll('input[type=radio]')[1].checked = true
    var cells = [].slice.apply(el.querySelectorAll('.option_cell'))
    cells.forEach(function(cell) {
      if (cell.innerHTML.includes(config[labelKey])) {
        cell.querySelector('input[type=radio]').checked = true
      }
    })
  }

  function autoFill() {
    var question_boxs = [].slice.apply(document.querySelectorAll('.wjques.maxtop.question'))
    question_boxs.forEach(function(el) {
      var content = el.innerHTML
      if (content.includes(nameLabel)) {
        fillName(el)
      } else if (content.includes(locationLabel)) {
        fillOption(el, 'location')
      } else if (content.includes(breakfastLabel)) {
        fillOption(el, 'breakfast')
      } else if (content.includes(lunchLabel)) {
        fillOption(el, 'lunch')
      } else if (content.includes(dinnerLabel)) {
        fillOption(el, 'dinner')
      } else if (content.includes(departmentLabel)) {
        fillOption(el, 'department')
      }
    })
  }

  var btn = document.createElement('button');
  btn.class = 'WJButton wj_colo';
  btn.style = "position: absolute; right: 50px;top: 50px";
  document.body.appendChild(btn);

  btn.addEventListener('click', autoFill) 
})()