Lib_MyAPI

又拍云自动活动签到程序

目前为 2024-03-06 提交的版本。查看 最新版本

此脚本不应直接安装,它是一个供其他脚本使用的外部库。如果您需要使用该库,请在脚本元属性加入:// @require https://update.cn-greasyfork.org/scripts/489184/1338823/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. // @grant unsafeWindow
  9. // @description 又拍云自动活动签到程序
  10. // ==/UserScript==
  11.  
  12. // 避免函数污染
  13. const ACMO = window.MutationObserver || window.WebKitMutationObserver || window.MozMutationObserver;
  14.  
  15. unsafeWindow.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. handle()
  76. // 其他时间增加监听即可
  77. const watcher = new ACMO(() => {
  78. // 超过节流时间间隔,立即执行事件处理函数
  79. if(Date.now() - lastRunAt > findTick) {
  80. handle()
  81. if(clearAfterFind) {
  82. watcher.disconnect();
  83. }
  84. lastRunAt = Date.now()
  85. } else {
  86. // 未超过节流时间间隔,设置定时器延迟执行事件处理函数
  87. clearTimeout(timer);
  88. onlyTimer = setTimeout(() => {
  89. handle();
  90. lastRunAt = Date.now()
  91. }, findTick);
  92. }
  93. // 超过最大时限,那么停止监听
  94. if(Date.now() - firstRunAt > timeout) {
  95. watcher.disconnect();
  96. }
  97. });
  98.  
  99. watcher.observe(document.body, {
  100. childList: true,
  101. subtree: true
  102. });
  103. }
  104.  
  105. function getUrlAttribute(attribute, needDecode = true){
  106. var searchValue = (window.location.search.substr(1) + "").split("&");
  107. for (var i = 0; i < searchValue.length; i++) {
  108. var key_value = searchValue[i].split("=");
  109. var reg = new RegExp("^"+attribute+"$");
  110. if (reg.test(key_value[0])) {
  111. var searchWords = key_value[1];
  112. return needDecode?decodeURIComponent(searchWords):searchWords;
  113. }
  114. }
  115. }
  116.  
  117. const http = {
  118. async get(url) {
  119. return new Promise((resolve, reject) => {
  120. GM_xmlhttpRequest({
  121. url,
  122. method: 'GET',
  123. timeout: 10000,
  124. onload: resp => resolve([null, resp.responseText]),
  125. onerror: resp => reject([resp, {}])
  126. })
  127. })
  128. },
  129. async post(url, data) {
  130. return new Promise((resolve, reject) => {
  131. GM_xmlhttpRequest({
  132. url,
  133. data,
  134. method: 'POST',
  135. timeout: 10000,
  136. onload: resp => resolve([null, resp.responseText]),
  137. onerror: resp => reject([resp, {}])
  138. })
  139. })
  140. }
  141. }
  142.  
  143. return {
  144. addStyle,
  145. addScript,
  146. safeWaitFunc,
  147. getUrlAttribute,
  148. http
  149. }
  150. })()