H2P: 通用脚本

通用脚本

目前為 2020-09-13 提交的版本,檢視 最新版本

此腳本不應該直接安裝,它是一個供其他腳本使用的函式庫。欲使用本函式庫,請在腳本 metadata 寫上: // @require https://update.cn-greasyfork.org/scripts/411278/847384/H2P%3A%20%E9%80%9A%E7%94%A8%E8%84%9A%E6%9C%AC.js

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

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

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

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

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

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

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name        H2P: 通用脚本
// @namespace   http://tampermonkey.net/
// @version     0.0.1
// @icon        http://www.douyutv.com/favicon.ico
// @description 通用脚本
// @author      H2P
// @compatible  chrome
// ==/UserScript==

(() => {
  'use strict';

  // 在字符串前(后)添加 0
  function add0(s = '', len = 0, isAddFront = true) {
    s = s.toString();
    while (s.length < len) { s = isAddFront ? '0' + s : s + '0'; }
    return s;
  }
  // 返回毫秒
  function timeMS(num = 0) {
    num = Number.parseInt(num);
    return num < 946684800000 ? num * 1000 : num;
  }

  const $H2P  = (xpath = 'body', queryOneElement = true) => queryOneElement ? document.querySelector(xpath) : Array.from(document.querySelectorAll(xpath));
  const $LS   = {
    init: (itemKey = '', itemPre = {}) => {
      let item = Object.assign({}, itemPre, $LS.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;
    },
    set: (itemKey = '', item = {}) => { localStorage.setItem(itemKey, JSON.stringify(item)); },
    get: (itemKey = '') => JSON.parse(localStorage.getItem(itemKey)) || {},
    remove: (itemKey = '') => { localStorage.removeItem(itemKey); }
  }
  const $INVL = {
    clear: (INVLID) => { clearInterval(INVLID); INVLID = null; }
  }
  
  const $TIME   = {
    hms: (time = 0) => {
      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 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)}`; } 
})();