CommonsUtil

utility methods

Stan na 08-01-2020. Zobacz najnowsza wersja.

Ten skrypt nie powinien być instalowany bezpośrednio. Jest to biblioteka dla innych skyptów do włączenia dyrektywą meta // @require https://update.greasyfork.org/scripts/393085/763929/CommonsUtil.js

Aby zainstalować ten skrypt, wymagana jest instalacje jednego z następujących rozszerzeń: Tampermonkey, Greasemonkey lub Violentmonkey.

Aby zainstalować ten skrypt, wymagana jest instalacje jednego z następujących rozszerzeń: Tampermonkey, Violentmonkey.

Aby zainstalować ten skrypt, wymagana jest instalacje jednego z następujących rozszerzeń: Tampermonkey, Violentmonkey.

Aby zainstalować ten skrypt, wymagana będzie instalacja rozszerzenia Tampermonkey lub Userscripts.

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

Aby zainstalować ten skrypt, musisz zainstalować rozszerzenie menedżera skryptów użytkownika.

(Mam już menedżera skryptów użytkownika, pozwól mi to zainstalować!)

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.

Będziesz musiał zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

Będziesz musiał zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

Musisz zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

(Mam już menedżera stylów użytkownika, pozwól mi to zainstalować!)

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