CommonsUtil

utility methods

Verze ze dne 02. 12. 2019. Zobrazit nejnovější verzi.

Tento skript by neměl být instalován přímo. Jedná se o knihovnu, kterou by měly jiné skripty využívat pomocí meta příkazu // @require https://update.greasyfork.org/scripts/393085/754478/CommonsUtil.js

K instalaci tototo skriptu si budete muset nainstalovat rozšíření jako Tampermonkey, Greasemonkey nebo Violentmonkey.

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

K instalaci tohoto skriptu si budete muset nainstalovat rozšíření jako Tampermonkey nebo Violentmonkey.

K instalaci tohoto skriptu si budete muset nainstalovat rozšíření jako Tampermonkey nebo Userscripts.

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

K instalaci tohoto skriptu si budete muset nainstalovat manažer uživatelských skriptů.

(Už mám manažer uživatelských skriptů, nechte mě ho nainstalovat!)

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.

(Už mám manažer uživatelských stylů, nechte mě ho nainstalovat!)

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

/**
 * 输出错误
 * @param functionName 方法名
 * @param error 错误
 */
const error = function (functionName, error) {
  console.error('function name: ' + functionName + "\nerror: " + error);
};

/**
 * 循环移除元素
 * @param ele 元素
 */
const removeRecursively = function (ele) {
  try {
    let parent = ele.parentElement;
    ele.remove();
    if (!!parent && !parent.innerHTML) {
      removeRecursively(parent);
    }
  } catch (e) {
    error('removeRecursively', e);
  }
};

/**
 * 移除选择器对象
 * @param selector 选择器
 */
const removeIt = function (selector) {
  try {
    removeRecursively(document.querySelector(selector));
  } catch (e) {
    error('removeIt', e);
  }
};

/**
 * 移除选择器所有对象
 * @param selector 选择器
 */
const removeAll = function (selector) {
  try {
    document.querySelectorAll(selector).forEach(function (ele) {
      removeRecursively(ele);
    });
  } catch (e) {
    error('removeAll', e);
  }
};

/**
 * 点击选择器对象
 * @param selector 选择器
 */
const clickIt = function (selector) {
  try {
    document.querySelector(selector).click();
  } catch (e) {
    error('clickIt', e);
  }
};