PriateLib

自用

目前為 2022-01-17 提交的版本,檢視 最新版本

此腳本不應該直接安裝,它是一個供其他腳本使用的函式庫。欲使用本函式庫,請在腳本 metadata 寫上: // @require https://update.gf.qytechs.cn/scripts/435476/1009442/PriateLib.js

  1. // ==UserScript==
  2. // @name PriateLib
  3. // @version 0.0.3
  4. // @description 自用
  5. // @author Priate
  6. // @grant GM_xmlhttpRequest
  7. // ==/UserScript==
  8.  
  9. // 睡眠多少秒
  10. async function Sleep(sleepSecs) {
  11. return new Promise((resolve, reject) => {
  12. setTimeout(() => {
  13. resolve()
  14. }, sleepSecs * 1000)
  15. })
  16. }
  17.  
  18. // 等待某个函数执行完毕(每多少秒检测一次)
  19. async function WaitUntil(conditionFunc, sleepSecs) {
  20. sleepSecs = sleepSecs || 1
  21. return new Promise((resolve, reject) => {
  22. if (conditionFunc()) resolve()
  23. let interval = setInterval(() => {
  24. if (conditionFunc()) {
  25. clearInterval(interval)
  26. resolve()
  27. }
  28. }, sleepSecs * 1000)
  29. })
  30. }
  31.  
  32. // GM_xmlhttpRequest 简单封装
  33. function Request(url, opt = {}) {
  34. Object.assign(opt, {
  35. url,
  36. timeout: 2000,
  37. responseType: 'json'
  38. })
  39.  
  40. return new Promise((resolve, reject) => {
  41. opt.onerror = opt.ontimeout = reject
  42. opt.onload = resolve
  43. GM_xmlhttpRequest(opt)
  44. }).then(res => {
  45. if (res.status === 200) return Promise.resolve(res.response)
  46. else return Promise.reject(res)
  47. }, err => {
  48. return Promise.reject(err)
  49. })
  50. }
  51.  
  52. // easy http(s) get
  53. function Get(url, opt = {}) {
  54. Object.assign(opt, {
  55. method: 'GET'
  56. })
  57. return Request(url, opt)
  58. }
  59.  
  60. // easy http(s) post
  61. function Post(url, opt = {}) {
  62. Object.assign(opt, {
  63. method: 'POST'
  64. })
  65. return Request(url, opt)
  66. }
  67.  
  68. // simple toast
  69. function showToast(msg, doNotFade) {
  70. 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;`
  71.  
  72. let span = document.createElement('span')
  73. span.setAttribute('style', style)
  74. span.innerText = msg
  75. document.body.appendChild(span)
  76. if (!doNotFade) {
  77. setTimeout(() => {
  78. document.body.removeChild(span)
  79. }, 5000)
  80. }
  81. }
  82.  
  83. async function GetElementByText(startElem, selector, text, exist) {
  84. /*
  85. selector: 选择器
  86. text: 内容
  87. exist: 是否只要存在就ojbk
  88. */
  89. exist = exist || false
  90. let elemList = startElem.querySelectorAll(selector)
  91. for (let i = 0; i < elemList.length; ++i) {
  92. let elem = elemList[i]
  93. if (exist) {
  94. if (elem.innerText.search(text) !== -1) return elem
  95. } else {
  96. if (elem.innerText === text) return elem
  97. }
  98. }
  99. }
  100. /**
  101. * 替换全部匹配到的内容
  102. * @param FindText 需要查找的字符串
  103. * @param RepText 将要替换的字符串
  104. * @returns {string}
  105. */
  106. String.prototype.replaceAll = function(FindText, RepText) {
  107. let regExp = new RegExp(FindText, "g");
  108. return this.replace(regExp, RepText);
  109. }
  110.  
  111. /**
  112. * 移除iframe页面元素,用于wifi劫持和去除iframe广告
  113. */
  114. function removeIframe() {
  115. let filter = new Object();
  116. filter.ad = function() {
  117. let tar = document.getElementsByTagName('iframe');
  118. let len = tar.length;
  119. if (len > 0) {
  120. for (let i = 0; i < len; i++) {
  121. tar[0].remove()
  122. }
  123. }
  124. }
  125. filter.timer = function() {
  126. let clean = setInterval(function() {
  127. if (document.getElementsByTagName('iframe').length == 0) {
  128. clearInterval(clean)
  129. console.log('清除')
  130. } else {
  131. filter.ad()
  132. }
  133. }, 300)
  134. }
  135. filter.timer()
  136. }
  137. /**
  138. * 向页面中添加div
  139. * @param className 类名
  140. * @param innerHtml 内容
  141. * @param clickFunc 点击事件函数
  142. * @returns {HTMLDivElement}
  143. */
  144. function loadDiv(className = '', innerHtml = '', clickFunc = false) {
  145. let div = document.createElement('div')
  146. div.className = className
  147. div.innerHTML = innerHtml
  148. if (typeof clickFunc == 'function') {
  149. div.onclick = clickFunc
  150. }
  151. document.body.append(div)
  152. return div
  153. }
  154. /**
  155. * 加载js文件
  156. * @param url js文件路径
  157. * @param callback 加载成功后执行的回调函数
  158. */
  159. function loadJs(url, callback) {
  160. let head = document.getElementsByTagName('head')[0];
  161. let script = document.createElement('script');
  162. script.type = 'text/javascript';
  163. script.src = url;
  164. if (typeof(callback) == 'function') {
  165. script.onload = script.onreadystatechange = function() {
  166. if (!this.readyState || this.readyState === "loaded" || this.readyState === "complete") {
  167. callback();
  168. script.onload = script.onreadystatechange = null;
  169. }
  170. };
  171. }
  172. head.appendChild(script);
  173. }
  174. /**
  175. * 获取当前URL地址参数
  176. * @param name 参数名称
  177. * @returns {string|null}
  178. */
  179. function getUrlParam(name) {
  180. let reg = new RegExp("(.|&)" + name + "=([^&]*)(&|$)");
  181. let r = window.location.href.match(reg);
  182. if (r != null) return unescape(r[2]);
  183. return null;
  184. }
  185. /**
  186. * 执行函数
  187. * @param func 函数
  188. * @param time 延时,负数:延时->执行,正数:执行->延时
  189. * @param desc
  190. * @returns {Promise<unknown>}
  191. */
  192. function obsFunc(func, time = 0, desc = 'func') {
  193. return new Promise(resolve => {
  194. if (!!func) {
  195. if (time < 0) {
  196. setTimeout(() => {
  197. func()
  198. console.log(desc)
  199. resolve('func')
  200. }, Math.abs(time) * 1000)
  201. } else if (time > 0) {
  202. func()
  203. setTimeout(() => {
  204. console.log(desc)
  205. resolve('func')
  206. }, Math.abs(time) * 1000)
  207. } else {
  208. func()
  209. console.log(desc)
  210. resolve('func')
  211. }
  212. }
  213. })
  214. }
  215.  
  216.  
  217. /**
  218. * 懒加载某元素
  219. * @param el 元素选择器(字符串)
  220. * @param func 回调函数
  221. * @param times 次数
  222. * @param interval 间隔时间
  223. */
  224. function loadElement(el, func, times, interval) {
  225. var _times = times || 100, //100次
  226. _interval = interval || 200, //200毫秒每次
  227. _self = document.querySelector(el),
  228. _iIntervalID; //定时器id
  229. if (_self) { //如果已经获取到了,就直接执行函数
  230. func && func.call(el);
  231. } else {
  232. _iIntervalID = setInterval(function() {
  233. if (!_times) { //是0就退出
  234. clearInterval(_iIntervalID);
  235. }
  236. _times <= 0 || _times--; //如果是正数就 --
  237. _self = document.querySelector(el); //再次选择
  238. if (_self) { //判断是否取到
  239. func && func.call(el);
  240. clearInterval(_iIntervalID);
  241. }
  242. }, _interval);
  243. }
  244. return this;
  245. }
  246.  
  247.  
  248. /**
  249. * 获取当前时间字符串
  250. */
  251. function getNowFormatDate() {
  252. var date = new Date();
  253. var seperator1 = "-";
  254. var seperator2 = ":";
  255. var month = date.getMonth() + 1;
  256. var strDate = date.getDate();
  257. if (month >= 1 && month <= 9) {
  258. month = "0" + month;
  259. }
  260. if (strDate >= 0 && strDate <= 9) {
  261. strDate = "0" + strDate;
  262. }
  263. var currentdate = month + seperator1 + strDate + " " + date.getHours() + seperator2 + date.getMinutes()
  264. return currentdate;
  265. }
  266.  
  267. /**
  268. * Aria2下载
  269. * @param url 下载文件地址
  270. * @param filename 下载文件名(带后缀)
  271. * @param config 下载配置
  272. */
  273. function Aria2(url, filename, config = {
  274. wsurl: "ws://127.0.0.1:6800/jsonrpc",
  275. token: ""
  276. }) {
  277. var wsurl = config.wsurl
  278. var token = config.token
  279. var uris = [url]
  280. var options = {
  281. "max-connection-per-server": "16",
  282. "user-agent": "Opera\/9.80 (Windows NT 6.0) Presto\/2.12.388 Version\/12.14",
  283. }
  284. if (filename != "") {
  285. options.out = filename;
  286. }
  287.  
  288. let json = {
  289. "id": "priate-script",
  290. "jsonrpc": '2.0',
  291. "method": 'aria2.addUri',
  292. "params": [uris, options],
  293. };
  294.  
  295. if (token != "") {
  296. json.params.unshift("token:" + token);
  297. }
  298. var ws = new WebSocket(wsurl);
  299.  
  300. ws.onerror = event => {
  301. console.log(event);
  302. };
  303. ws.onopen = () => {
  304. ws.send(JSON.stringify(json));
  305. }
  306.  
  307. ws.onmessage = event => {
  308. console.log(event);
  309. let received_msg = JSON.parse(event.data);
  310. if (received_msg.error !== undefined) {
  311. if (received_msg.error.code === 1)
  312. console.log(received_msg.error.message)
  313. }
  314. switch (received_msg.method) {
  315. case "aria2.onDownloadStart":
  316. console.log('Aria2 已经开始下载!' + filename)
  317. break;
  318. case "aria2.onDownloadError":
  319. ;
  320. console.log('Aria2 下载错误!');
  321. break;
  322. case "aria2.onDownloadComplete":
  323. console.log('Aria2 下载完成!');
  324. break;
  325. default:
  326. break;
  327. }
  328. };
  329. }
  330.  
  331.  
  332. /**
  333. * 显示赞赏图片
  334. */
  335. function showDonate() {
  336. var priate_donate_script_div = document.createElement("div")
  337. priate_donate_script_div.innerHTML = `
  338. <div id="priate_donate_script_div">
  339. <p class="priate_donate_script_p"><a href="https://donate.virts.app" target="_blank"><img class="priate_donate_script_img" src="https://donate.virts.app/images/pay/all.jpg"></a></p>
  340. <p class="priate_donate_script_text">谢谢你这么好看还给我零花钱~</p>
  341. <p class="priate_donate_script_text">点击任意一处即可恢复页面~</p>
  342. </div>
  343. `
  344. GM_addStyle(`
  345. #priate_donate_script_div{
  346. height : 100%;
  347. width : 100%;
  348. position : fixed;
  349. z-index : 2147483647;
  350. top : 0;
  351. background-color: pink;
  352. }
  353. .priate_donate_script_p{
  354. text-align : center;
  355. margin-top : 150px;
  356. }
  357. .priate_donate_script_img{
  358. cursor : pointer;
  359. width : 600px;
  360. }
  361. .priate_donate_script_text{
  362. text-align : center;
  363. font-size: 30px;
  364. color: red;
  365. }
  366. `);
  367. document.querySelector("html").appendChild(priate_donate_script_div)
  368. priate_donate_script_div.addEventListener('click', function() {
  369. priate_donate_script_div.remove()
  370. })
  371. }

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址