H2P: utils

utils

目前为 2020-09-13 提交的版本。查看 最新版本

此脚本不应直接安装,它是一个供其他脚本使用的外部库。如果您需要使用该库,请在脚本元属性加入:// @require https://update.cn-greasyfork.org/scripts/411278/847392/H2P%3A%20utils.js

// ==UserScript==
// @name        H2P: utils
// @namespace   http://tampermonkey.net/
// @version     0.0.6
// @icon        http://www.douyutv.com/favicon.ico
// @description utils
// @author      H2P
// @compatible  chrome
// ==/UserScript==

((w) => {
  'use strict';

  /**
   * 在字符串前(后)添加 0
   * @param {String} s
   * @param {Number} len 
   * @param {Boolean} isAddFront 
   */
  function add0(s = '', len = 0, isAddFront = true) {
    s = s.toString();
    while (s.length < len) { s = isAddFront ? '0' + s : s + '0'; }
    return s;
  }

  w.$util = (() => {
    function Util() {
      /**
       * 返回毫秒
       * @param {Number} num 
       */
      this.timeMS = (num = 0) => {
        num = Number.parseInt(num);
        return num < 946684800000 ? num * 1000 : num;
      }
  
      /**
       * localStorage 相关操作
       */
      function LS() {
        this.init = (itemKey = '', itemPre = {}) => {
          let item = Object.assign({}, itemPre, this.get(itemKey));
          for (let key in item) { if (!(key in itemPre)) { delete item[key]; } }
          localStorage.removeItem(itemKey);
          localStorage.setItem(itemKey, JSON.stringify(item));
          return item;
        }
        this.set = (itemKey = '', item = {}) => { localStorage.setItem(itemKey, JSON.stringify(item)); },
        this.get = (itemKey = '') => JSON.parse(localStorage.getItem(itemKey)) || {},
        this.remove = (itemKey = '') => { localStorage.removeItem(itemKey); }
      }
      this.LS = new LS();
  
      this.HMS = (time = 0) => {
        time = this.timeMS(time);
        let h = Number.parseInt(time / 3600000);
        let m = Number.parseInt(time % 3600000 / 60000);
        let s = Number.parseInt(time % 3600000 % 60000 / 1000);
        return {
          h: add0(h, 2),
          m: add0(m, 2),
          s: add0(s, 2),
        }
      }
    }
    return new Util();
  })();

  // return millisecond
  Date.prototype.$timems = Date.prototype.getTime;
  // return second
  Date.prototype.$times = function() { return Number.parseInt(this.getTime() / 1000); }
  // format time: yyyy-MM-dd hh-mm-ss
  Date.prototype.$formatTime = function() { return `${this.getFullYear()}-${add0(this.getMonth() + 1, 2)}-${add0(this.getDate(), 2)} ${add0(this.getHours(), 2)}:${add0(this.getMinutes(), 2)}:${add0(this.getSeconds(), 2)}`; }
  // format date: yyyy-MM-dd
  Date.prototype.$formatDate = function() { return `${this.getFullYear()}-${add0(this.getMonth() + 1, 2)}-${add0(this.getDate(), 2)}`; } 

  /**
   * 根据 xpath 查询元素
   * @param {String} xpath 
   * @param {Boolean} queryOneElement 
   */
  w.$H2P = (xpath = 'body', queryOneElement = true) => queryOneElement ? document.querySelector(xpath) : Array.from(document.querySelectorAll(xpath));







  
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ //
// 
//                                                              通知栏
// 
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ //

  const notifyType = {
    default: {
      bgColor: '#e6ffff',
      bdColor: '#23bdd9'
    },
    success: {
      bgColor: '#f6ffec',
      bdColor: '#53752d'
    },
    warn: {
      bgColor: '#fefbe6',
      bdColor: '#fdc446'
    },
    error: {
      bgColor: '#fff0ef',
      bdColor: '#e75252'
    }
  }

  w.$notifyMgr = (() => {
    const style_notify = document.createElement('style');
    style_notify.id = 'h2p-style-notify';
    style_notify.innerHTML = `
      #h2p-div-notify-container {
        position: fixed;
        width: 280px;
        bottom: 20px;
        left: 20px;
        overflow: hidden;
        z-index: 9999;
      }

      .h2p-div-notify-item {
        position: relative;
        width: 250px;
        height: 25px;
        right: -280px;
        padding: 9px 13px;
        margin: 6px 0;
        border: 1px solid;
        border-radius: 5px;
        display: flex;
        align-items: center;
        transition: left 1.5s, right 1.5s;
      }

      .h2p-div-notify-item-in {
        right: 0;
      }

      .h2p-div-notify-close {
        position: absolute;
        top: 15px;
        right: 20px;
        cursor: pointer;
      }

      .h2p-div-notify-close::before, .h2p-div-notify-close::after {
        position: absolute;
        content: '';
        width: 12px;
        height: 2px;
        background: chocolate;
      }

      .h2p-div-notify-close::before {
        transform: rotate(45deg);
      }
      .h2p-div-notify-close::after {
        transform: rotate(-45deg);
      }
    `;
    document.body.appendChild(style_notify);

    const div_notify = document.createElement('div');
    div_notify.id = 'h2p-div-notify-container';
    document.body.appendChild(div_notify);

    const Notify = function() {
      this.createNotify = ({ msg = '', type = notifyType.default, autoClose = true }) => {
        const ran = Math.floor(Math.random() * 100000000);
        const div_notify_item = document.createElement('div');
        div_notify_item.id = `h2p-div-notify-${ran}`;
        div_notify_item.classList.add('h2p-div-notify-item');
        div_notify_item.style.backgroundColor = type.bgColor;
        div_notify_item.style.borderColor = type.bdColor;
        div_notify_item.innerHTML = msg;
        $H2P('div#h2p-div-notify-container').appendChild(div_notify_item);

        const div_notify_item_close = document.createElement('div');
        div_notify_item_close.id = `h2p-div-notify-close-${ran}`;
        div_notify_item_close.classList.add('h2p-div-notify-close');
        div_notify_item_close.addEventListener('click', (e) => { this.closeNotify(`h2p-div-notify-${e.target.id.match(/[a-zA-Z\-]*(\d+)[a-zA-Z\-]*/g)[1]}`); })
        $H2P('div#h2p-div-notify-container').appendChild(div_notify_item_close);

        setTimeout((id) => {
          // 显示通知栏
          $H2P(`#${id}`).classList.add('h2p-div-notify-item-in');
          autoClose && setTimeout(this.closeNotify, 4000, id);
        }, 100, div_notify_item.id);
      }

      this.closeNotify = (id = '') => {
        $H2P(`#${id}`).classList.remove('h2p-div-notify-item-in');
        setTimeout(() => {
          $H2P('div#h2p-div-notify-container').removeChild($H2P(`#${id}`));
        }, 1500);
      }
    }
    return new Notify();
  })();
})(window);