CommonsUtil

utility methods

As of 2020-01-08. See the latest version.

This script should not be not be installed directly. It is a library for other scripts to include with the meta directive // @require https://update.greasyfork.org/scripts/393085/763929/CommonsUtil.js

You will need to install an extension such as Tampermonkey, Greasemonkey or Violentmonkey to install this script.

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

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

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

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

You will need to install a user script manager extension to install this script.

(I already have a user script manager, let me install it!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(I already have a user style manager, let me install it!)

// ==UserScript==
// @author gaojr
// @namespace https://github.com/gaojr/tampermonkey-scripts
// @name:CN-zh_cn 工具类
// @name CommonsUtil
// @version 1.0
// @description utility methods
// @grant none
// ==/UserScript==

/**
 * 类似jquery的选择器
 * @param {string} selector 选择器
 * @param {Element} ele 元素
 * @return {Element} 元素
 */
const _$ = (selector, ele) => {
  return (ele || document).querySelector(selector);
}

/**
 * 类似jquery的选择器
 * @param {string} selector 选择器
 * @param {Element} ele 元素
 * @return {NodeListOf<Element>} 元素
 */
const _$$ = (selector, ele) => {
  return [...(ele || document).querySelectorAll(selector)];
}

/**
 * 输出错误
 * @param {string} functionName 方法名
 * @param {Error} error 错误
 */
const ce = (functionName, error) => {
  let msg = '';
  if (!!functionName) {
    msg = 'function name: ' + functionName + "\nerror: ";
  }
  msg += error;
  console.error(msg);
};

/**
 * 循环移除元素
 * @param {Element} ele 元素
 */
const removeRecursively = (ele) => {
  try {
    let parent = ele.parentNode;
    ele.remove();
    if (!!parent && !parent.innerHTML) {
      // 父元素存在且父元素内容为空
      removeRecursively(parent);
    }
  } catch (e) {
    ce('removeRecursively', e);
  }
};

/**
 * 移除选择器对象
 * @param {string} selector 选择器
 * @param {boolean} only 是否仅移除该对象
 */
const removeIt = (selector, only) => {
  try {
    if (only === false) {
      removeRecursively(_$(selector));
    } else {
      _$(selector).remove();
    }
  } catch (e) {
    ce('removeIt', e);
  }
};

/**
 * 移除选择器所有对象
 * @param {string} selector 选择器
 */
const removeAll = (selector) => {
  try {
    _$$(selector).forEach((ele) => {
      removeRecursively(ele);
    });
  } catch (e) {
    ce('removeAll', e);
  }
};

/**
 * 点击选择器对象
 * @param {string} selector 选择器
 */
const clickIt = (selector) => {
  try {
    _$(selector).click();
  } catch (e) {
    ce('clickIt', e);
  }
};

/**
 * 要在document加载完成后运行的函数
 */
const funcMap = new Map();

/**
 * 加入方法map
 * @param {string} name 名字
 * @param {Function} func 方法
 */
const addToFuncMap = (name, func) => {
  if (!funcMap.has(name)) {
    funcMap.set(name, func);
  }
}

let tempFunc = document.onreadystatechange;
document.onreadystatechange = () => {
  if (typeof (tempFunc) === 'function') {
    tempFunc();
  }

  if (document.readyState === 'interactive' || document.readyState === 'complete') {
    funcMap.forEach((value, key) => {
      try {
        console.log('TMscript start: ' + key);
        value();
        console.log('TMscript end: ' + key);
      } catch (e) {
        console.log('TMscript error: ' + key);
        ce('', e);
      }
    });
  }
};