自用价值观 - 点击时刻提醒(优化点击性能)

价值观 在你每次点击时飘出并提示!

// ==UserScript==
// @name         自用价值观 - 点击时刻提醒(优化点击性能)
// @namespace    http://tampermonkey.net/
// @version      1.1.5
// @description  价值观 在你每次点击时飘出并提示!
// @author       Cody
// @include      http://*
// @include      https://*
// @exclude      *mail.qq.com*
// @grant        none
// @run-at       document-idle
// ==/UserScript==

(function () {
  'use strict';
  var animateInterval = 2;
  var coreSocialistValues = ["文明", "自由", "民主", "平等", "公正", "法制", "和谐", "敬业", "友善", '狗头保命', '爱学习', '埋头苦干', '加油干', '别走神', '去学习', '去工作', '别划水', '别吵我','别逼逼','思考中'],
    index;
  window.addEventListener('click', function (e) {
    index = Math.floor(Math.random() * coreSocialistValues.length)
    //if (e.target.tagName == 'A') {
    //return;
    //}
    var x = e.pageX,
      y = e.pageY,
      el;
    var bubble = document.querySelectorAll('.bubble')
    try {
      [].forEach.call(bubble, ((element, index) => {
        if (element.style.content == 'none') {
          el = element;
          throw "优化点击,DOM复用中";
        }
      }));
    }
     catch(error){
      //console.log(error)
      }
      finally {
      if (!el) {
        el = document.createElement('span');
        el.classList.add('bubble');
        document.body.appendChild(el);
      }
      el.style.cssText = ['z-index: 9999; position: absolute; font-size: 14px; font-weight: bold; color: #dd2222;opacity:1;content:"animateing"; top: ', y - 20, 'px; left: ', x, 'px;'].join('');
      el.textContent = coreSocialistValues[index];
      setTimeout(function () {
        animate(el);
      })
    }
  });

  function animate(el) {
    var top = parseInt(el.style.top);
    el.style.transition = 'all ' + animateInterval + 's'
    el.style.top = top - 200 + 'px';
    el.style.opacity = 0;
    setTimeout(function () {
      el.style.content = 'none'
      /*setTimeout(function () {
        if (el.style.content = 'none') {
          el.parentNode.removeChild(el);
        }
      }, 1000 * animateInterval)*/
    }, 1000 * animateInterval)
  }
})();