Lib_MyAPI

封装了一些常用的API,用于方便调用

此脚本不应直接安装,它是一个供其他脚本使用的外部库。如果您需要使用该库,请在脚本元属性加入:// @require https://update.gf.qytechs.cn/scripts/489184/1338830/Lib_MyAPI.js

  1. // ==UserScript==
  2. // @name Lib_MyAPI
  3. // @namespace AC-API
  4. // @include *
  5. // @run-at document-start
  6. // @version 2024.03.07
  7. // @grant GM_xmlhttpRequest
  8. // @description 封装了一些常用的API,用于方便调用
  9. // ==/UserScript==
  10.  
  11. // 避免函数污染
  12. const ACMO = window.MutationObserver || window.WebKitMutationObserver || window.MozMutationObserver;
  13.  
  14. // 不能用unsafeWindow来暴露MyApi,否则会有http的安全问题
  15. const MyApi = (() => {
  16. /**
  17. * @param css CSS的正文内容
  18. * @param className 被添加的css节点的class,用于onlyOne检测
  19. * @param isReload 是否自动更新:移除后重新添加
  20. */
  21. function addStyle(css, className = '', isReload = false){ // 添加CSS代码,不考虑文本载入时间,带有className
  22. const tout = setInterval(function() {
  23. if (document.body != null) {
  24. clearInterval(tout);
  25. if (className) {
  26. // 节点不存在,或者是准备覆盖的时候
  27. if (isReload === false && document.querySelector("." + className) != null) {
  28. return;
  29. }
  30.  
  31. // 节点已经存在,并且不准备覆盖
  32. try {
  33. document.querySelector("." + className).remove();
  34. } catch (e) {
  35. }
  36. }
  37. const cssNode = document.createElement("style");
  38. if (className) {
  39. cssNode.className = className;
  40. }
  41. cssNode.innerHTML = css;
  42. try {
  43. document.body.appendChild(cssNode);
  44. } catch (e) {
  45. console.log(e.message);
  46. }
  47. }
  48. }, 200);
  49. }
  50.  
  51. function addScript(scriptInner) {
  52. const scriptNode = document.createElement('script')
  53. scriptNode.innerText = scriptInner
  54. document.head.appendChild(scriptNode)
  55. }
  56.  
  57. const safeWaitFunc = function waitForElm(selector, callbackFunc = node => {}, findTick = 200, clearAfterFind = true, timeout = 20000 * 1000) {
  58. const handle = () => {
  59. if ((typeof (selector) === "string")) {
  60. let res = document.querySelectorAll(selector);
  61. if(res && res.length) {
  62. callbackFunc(res);
  63. }
  64. } else if (typeof (selector) === "function") {
  65. const res = selector()
  66. if(res && res.length) {
  67. callbackFunc(res);
  68. }
  69. }
  70. }
  71. let lastRunAt = 0 // 最近一次运行的时间
  72. let onlyTimer;
  73. const firstRunAt = Date.now()
  74. // 其他时间增加监听即可
  75. const watcher = new ACMO(() => {
  76. // 超过节流时间间隔,立即执行事件处理函数
  77. if(Date.now() - lastRunAt > findTick) {
  78. handle()
  79. if(clearAfterFind) {
  80. watcher.disconnect();
  81. }
  82. lastRunAt = Date.now()
  83. } else {
  84. // 未超过节流时间间隔,设置定时器延迟执行事件处理函数
  85. clearTimeout(timer);
  86. onlyTimer = setTimeout(() => {
  87. handle();
  88. lastRunAt = Date.now()
  89. }, findTick);
  90. }
  91. // 超过最大时限,那么停止监听
  92. if(Date.now() - firstRunAt > timeout) {
  93. watcher.disconnect();
  94. }
  95. });
  96.  
  97. watcher.observe(document.body, {
  98. childList: true,
  99. subtree: true
  100. });
  101. }
  102.  
  103. function getUrlAttribute(attribute, needDecode = true){
  104. var searchValue = (window.location.search.substr(1) + "").split("&");
  105. for (var i = 0; i < searchValue.length; i++) {
  106. var key_value = searchValue[i].split("=");
  107. var reg = new RegExp("^"+attribute+"$");
  108. if (reg.test(key_value[0])) {
  109. var searchWords = key_value[1];
  110. return needDecode?decodeURIComponent(searchWords):searchWords;
  111. }
  112. }
  113. }
  114.  
  115. const http = {
  116. async get(url) {
  117. return new Promise((resolve, reject) => {
  118. GM_xmlhttpRequest({
  119. url,
  120. method: 'GET',
  121. timeout: 10000,
  122. onload: resp => resolve([null, resp.responseText]),
  123. onerror: resp => reject([resp, {}])
  124. })
  125. })
  126. },
  127. async post(url, data) {
  128. return new Promise((resolve, reject) => {
  129. GM_xmlhttpRequest({
  130. url,
  131. data,
  132. method: 'POST',
  133. timeout: 10000,
  134. onload: resp => resolve([null, resp.responseText]),
  135. onerror: resp => reject([resp, {}])
  136. })
  137. })
  138. }
  139. }
  140.  
  141. return {
  142. addStyle,
  143. addScript,
  144. safeWaitFunc,
  145. getUrlAttribute,
  146. http
  147. }
  148. })()

QingJ © 2025

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