Priate Lib

自用

目前為 2021-11-30 提交的版本,檢視 最新版本

此腳本不應該直接安裝,它是一個供其他腳本使用的函式庫。欲使用本函式庫,請在腳本 metadata 寫上: // @require https://update.greasyfork.org/scripts/435476/993480/Priate%20Lib.js

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         Priate Lib
// @version      0.0.1
// @description  自用
// @author       Priate
// @grant        GM_xmlhttpRequest
// ==/UserScript==

// 睡眠多少秒
async function Sleep(sleepSecs) {
	return new Promise((resolve, reject) => {
		setTimeout(() => {
			resolve()
		}, sleepSecs * 1000)
	})
}

// 等待某个函数执行完毕(每多少秒检测一次)
async function WaitUntil(conditionFunc, sleepSecs) {
	sleepSecs = sleepSecs || 1
	return new Promise((resolve, reject) => {
		if (conditionFunc()) resolve()
		let interval = setInterval(() => {
			if (conditionFunc()) {
				clearInterval(interval)
				resolve()
			}
		}, sleepSecs * 1000)
	})
}

// GM_xmlhttpRequest 简单封装
function Request(url, opt = {}) {
	Object.assign(opt, {
		url,
		timeout: 2000,
		responseType: 'json'
	})

	return new Promise((resolve, reject) => {
		opt.onerror = opt.ontimeout = reject
		opt.onload = resolve
		GM_xmlhttpRequest(opt)
	}).then(res => {
		if (res.status === 200) return Promise.resolve(res.response)
		else return Promise.reject(res)
	}, err => {
		return Promise.reject(err)
	})
}

// easy http(s) get
function Get(url, opt = {}) {
	Object.assign(opt, {
		method: 'GET'
	})
	return Request(url, opt)
}

// easy http(s) post
function Post(url, opt = {}) {
	Object.assign(opt, {
		method: 'POST'
	})
	return Request(url, opt)
}

// simple toast
function showToast(msg, doNotFade) {
	let style = `position: fixed; right: 10px; top: 80px; width: 300px; text-align: left; background-color: rgba(255, 255, 255, 0.9); z-index: 99; padding: 10px 20px; border-radius: 5px; color: #222; box-shadow: 0px 0px 5px rgba(0, 0, 0, 0.2); font-weight: bold;`

	let span = document.createElement('span')
	span.setAttribute('style', style)
	span.innerText = msg
	document.body.appendChild(span)
	if (!doNotFade) {
		setTimeout(() => {
			document.body.removeChild(span)
		}, 5000)
	}
}

async function GetElementByText(startElem, selector, text, exist) {
	/*
  selector: 选择器
  text: 内容
  exist: 是否只要存在就ojbk
  */
	exist = exist || false
	let elemList = startElem.querySelectorAll(selector)
	for (let i = 0; i < elemList.length; ++i) {
		let elem = elemList[i]
		if (exist) {
			if (elem.innerText.search(text) !== -1) return elem
		} else {
			if (elem.innerText === text) return elem
		}
	}
}
/**
 * 替换全部匹配到的内容
 * @param FindText  需要查找的字符串
 * @param RepText   将要替换的字符串
 * @returns {string}
 */
String.prototype.replaceAll = function(FindText, RepText) {
	let regExp = new RegExp(FindText, "g");
	return this.replace(regExp, RepText);
}

/**
 * 移除iframe页面元素,用于wifi劫持和去除iframe广告
 */
function removeIframe() {
	let filter = new Object();
	filter.ad = function() {
		let tar = document.getElementsByTagName('iframe');
		let len = tar.length;
		if (len > 0) {
			for (let i = 0; i < len; i++) {
				tar[0].remove()
			}
		}
	}
	filter.timer = function() {
		let clean = setInterval(function() {
			if (document.getElementsByTagName('iframe').length == 0) {
				clearInterval(clean)
				console.log('清除')
			} else {
				filter.ad()
			}
		}, 300)
	}
	filter.timer()
}
/**
 * 向页面中添加div
 * @param className   类名
 * @param innerHtml   内容
 * @param clickFunc   点击事件函数
 * @returns {HTMLDivElement}
 */
function loadDiv(className = '', innerHtml = '', clickFunc = false) {
	let div = document.createElement('div')
	div.className = className
	div.innerHTML = innerHtml
	if (typeof clickFunc == 'function') {
		div.onclick = clickFunc
	}
	document.body.append(div)
	return div
}
/**
 * 加载js文件
 * @param url  js文件路径
 * @param callback  加载成功后执行的回调函数
 */
function loadJs(url, callback) {
	let head = document.getElementsByTagName('head')[0];
	let script = document.createElement('script');
	script.type = 'text/javascript';
	script.src = url;
	if (typeof(callback) == 'function') {
		script.onload = script.onreadystatechange = function() {
			if (!this.readyState || this.readyState === "loaded" || this.readyState === "complete") {
				callback();
				script.onload = script.onreadystatechange = null;
			}
		};
	}
	head.appendChild(script);
}
/**
 * 获取当前URL地址参数
 * @param name  参数名称
 * @returns {string|null}
 */
function getUrlParam(name) {
	let reg = new RegExp("(.|&)" + name + "=([^&]*)(&|$)");
	let r = window.location.href.match(reg);
	if (r != null) return unescape(r[2]);
	return null;
}
/**
 * 执行函数
 * @param func    函数
 * @param time    延时,负数:延时->执行,正数:执行->延时
 * @param desc
 * @returns {Promise<unknown>}
 */
function obsFunc(func, time = 0, desc = 'func') {
	return new Promise(resolve => {
		if (!!func) {
			if (time < 0) {
				setTimeout(() => {
					func()
					console.log(desc)
					resolve('func')
				}, Math.abs(time) * 1000)
			} else if (time > 0) {
				func()
				setTimeout(() => {
					console.log(desc)
					resolve('func')
				}, Math.abs(time) * 1000)
			} else {
				func()
				console.log(desc)
				resolve('func')
			}
		}
	})
}


/**
 * 懒加载某元素
 * @param el  元素选择器(字符串)
 * @param func 回调函数
 * @param times 次数
 * @param interval 间隔时间
 */
function loadElement(el, func, times, interval) {
	var _times = times || 100, //100次
		_interval = interval || 200, //200毫秒每次
		_self = document.querySelector(el),
		_iIntervalID; //定时器id
	if (_self) { //如果已经获取到了,就直接执行函数
		func && func.call(el);
	} else {
		_iIntervalID = setInterval(function() {
			if (!_times) { //是0就退出
				clearInterval(_iIntervalID);
			}
			_times <= 0 || _times--; //如果是正数就 --
			_self = document.querySelector(el); //再次选择
			if (_self) { //判断是否取到
				func && func.call(el);
				clearInterval(_iIntervalID);
			}
		}, _interval);
	}
	return this;
}


/**
 * 获取当前时间字符串
 */
function getNowFormatDate() {
	var date = new Date();
	var seperator1 = "-";
	var seperator2 = ":";
	var month = date.getMonth() + 1;
	var strDate = date.getDate();
	if (month >= 1 && month <= 9) {
		month = "0" + month;
	}
	if (strDate >= 0 && strDate <= 9) {
		strDate = "0" + strDate;
	}
	var currentdate = month + seperator1 + strDate + " " + date.getHours() + seperator2 + date.getMinutes()
	return currentdate;
}