CommonsUtil

utility methods

Version vom 08.01.2020. Aktuellste Version

Dieses Skript sollte nicht direkt installiert werden. Es handelt sich hier um eine Bibliothek für andere Skripte, welche über folgenden Befehl in den Metadaten eines Skriptes eingebunden wird // @require https://update.greasyfork.org/scripts/393085/763929/CommonsUtil.js

Du musst eine Erweiterung wie Tampermonkey, Greasemonkey oder Violentmonkey installieren, um dieses Skript zu installieren.

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.

Sie müssten eine Skript Manager Erweiterung installieren damit sie dieses Skript installieren können

(Ich habe schon ein Skript Manager, Lass mich es installieren!)

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);
      }
    });
  }
};