SinHelper

Sin Helper

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

  1. ; (function () {
  2. window.sinHelper = {
  3. Cache: {
  4.  
  5. },
  6. /* 网络拦截相关 */
  7. Xhr: {
  8. inited: false,
  9. init: function () {
  10. if (this.inited === true) return;
  11. let oldSend = XMLHttpRequest.prototype.send, _this = this;
  12. XMLHttpRequest.prototype.send = function () {
  13. let xhr = this
  14. this.addEventListener("load", function () {
  15. if (xhr.readyState != 4 || xhr.status != 200) return;
  16. if (xhr.responseType != '' && xhr.responseType != 'text') return;
  17. xhr.requestData = arguments
  18. xhr.responseHeders = xhr.getAllResponseHeaders()
  19. xhr.responseData = function () {
  20. try {
  21. return JSON.parse(xhr.responseText)
  22. } catch (e) {
  23. return xhr.responseText
  24. }
  25. }();
  26. _this.dispatchFetch(xhr)
  27. })
  28. oldSend.apply(this, arguments);
  29. }
  30. this.inited = true;
  31. },
  32. rules: {}, // pathname => function
  33. routes: [], // functions
  34. registRules: function (ruleName, ruleFunc) {
  35. if (typeof (ruleFunc) != 'function') {
  36. console.error("Xhr Rules 必须是可调用的函数", ruleName)
  37. return
  38. }
  39. this.rules[ruleName] = ruleFunc
  40. },
  41. /* 调用注册(不可用)的 pathname 处理函数 */
  42. dispatchFetch: function (xhr) {
  43. let _this = this
  44. const url = new URL(xhr.responseURL)
  45. if (_this.rules[url.pathname]) {
  46. if (typeof (this.rules[url.pathname]) == 'function') {
  47. this.rules[url.pathname](xhr)
  48. } else {
  49. console.error("Xhr 处理函数错误:", url.pathname)
  50. }
  51. } else {
  52. _this.routes.forEach((route) => {
  53. if (typeof (route) == 'function') {
  54. route(xhr)
  55. }
  56. })
  57. }
  58. }
  59. },
  60. Url: {
  61. info: function (_url) {
  62. let urlInfo = new URL(_url)
  63.  
  64. urlInfo['getParams'] = function (_key) {
  65. let _obj = Object.fromEntries(urlInfo.searchParams.entries())
  66. if (_key) return _obj[_key] || null
  67. return _obj
  68. }
  69.  
  70. return urlInfo
  71. },
  72. getParams: function (_url, _key) {
  73. let urlStr = _url.split('?')[1]
  74. let urlSearchParams = new URLSearchParams(urlStr)
  75. let result = Object.fromEntries(urlSearchParams.entries())
  76. if (_key) return result[_key] || null
  77. return result
  78. }
  79. },
  80. /* 原生JS 下载Excel */
  81. Excel: {
  82. 'trans2Base64': function (content) {
  83. return window.btoa(unescape(encodeURIComponent(content)));
  84. },
  85. 'exportExcelFromFront': function (params) {
  86. let _this = this
  87. const { cellList, headerList, caption, exportName = 'exportName' } = params;
  88.  
  89. const captionEle = caption ? `<caption>${caption}</caption>` : ''; // 表格标题
  90. const headerEle = `<tr>${headerList?.map((item) => `<th>${item}</th>`)?.join('')}</tr>`;
  91. const cellEle = cellList
  92. ?.map((itemRow) => `<tr>${itemRow?.map((itemCell) => `<td>${itemCell}</td>`)?.join('')}</tr>`)
  93. ?.join('');
  94.  
  95. const excelContent = `${captionEle}${headerEle}${cellEle}`;
  96. let worksheet = '工作表1';
  97. let excelFile =
  98. "<html xmlns:o='urn:schemas-microsoft-com:office:office' xmlns:x='urn:schemas-microsoft-com:office:excel' xmlns='http://www.w3.org/TR/REC-html40'>";
  99. excelFile +=
  100. `<head><!--[if gte mso 9]><xml><x:ExcelWorkbook><x:ExcelWorksheets><x:ExcelWorksheet><x:Name>${worksheet}</x:Name><x:WorksheetOptions><x:DisplayGridlines/></x:WorksheetOptions></x:ExcelWorksheet></x:ExcelWorksheets></x:ExcelWorkbook></xml><![endif]--></head>`;
  101. excelFile += "<body><table width='10%' border='1'>";
  102. excelFile += excelContent;
  103. excelFile += '</table></body>';
  104. excelFile += '</html>';
  105. const link = `data:application/vnd.ms-excel;base64,${_this.trans2Base64(excelFile)}`;
  106. const a = document.createElement('a');
  107. a.download = `${exportName}.xlsx`;
  108. a.href = link;
  109. a.click();
  110. }
  111. },
  112. /* 数学函数 */
  113. Math: {
  114. round: function (_number, _precision) {
  115. if (!_precision) _precision = 0
  116. _precision = _precision * 1
  117. let _power = 10 ** _precision
  118. return Math.round(_number * _power) / _power
  119. }
  120. },
  121. Str: {
  122. decodeBase64Safe: function (_encoded) {
  123. _encoded = _encoded.replace(/-/g, '+').replace(/_/g, '/');
  124. while (_encoded.length % 4) {
  125. _encoded += '=';
  126. }
  127. return atob(_encoded);
  128. }
  129. },
  130. Time: {
  131. /**
  132. * 格式化时间戳
  133. * @param {string|Date|number} [value='']
  134. * @param {string} [fmt='yyyy-MM-dd hh:mm:ss']
  135. * @returns {string}
  136. */
  137. format: function (value, fmt) {
  138. let date = value
  139. if (typeof value !== 'object' && !value.hasOwnProperty('getFullYear')) {
  140. date = new Date(), value = value + ''
  141. if (value.length === 10) date = new Date(value * 1000)
  142. if (value.length === 13) date = new Date(value * 1)
  143. }
  144.  
  145. if (!fmt) fmt = 'yyyy-MM-dd hh:mm:ss'
  146. let o = {
  147. "M+": date.getMonth() + 1,
  148. "d+": date.getDate(),
  149. "h+": date.getHours(),
  150. "m+": date.getMinutes(),
  151. "s+": date.getSeconds(),
  152. "q+": Math.floor((date.getMonth() + 3) / 3),
  153. "S": date.getMilliseconds()
  154. };
  155. if (/(y+)/.test(fmt)) {
  156. fmt = fmt.replace(RegExp.$1, (date.getFullYear() + "").substr(4 - RegExp.$1.length));
  157. }
  158. for (let k in o) {
  159. if (new RegExp("(" + k + ")").test(fmt)) {
  160. fmt = fmt.replace(RegExp.$1, (RegExp.$1.length === 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
  161. }
  162. }
  163. return fmt;
  164. },
  165. /**
  166. * 秒数 转 时分秒
  167. */
  168. convert: function (seconds) {
  169. let d = 0, h = 0, m = 0, s = 0, res = ''
  170.  
  171. if (seconds >= 86400) d = parseInt(seconds / 60 / 60 / 24)
  172. if (seconds >= 3600) h = parseInt(seconds / 60 / 60 % 24)
  173. if (seconds >= 60) m = parseInt(seconds / 60 % 60)
  174. s = parseInt(seconds % 60)
  175.  
  176. if (d > 0) res += d + '天'
  177. if (h > 0) res += (h < 10 ? '0' + h : h) + '小时'
  178. if (m > 0) res += (m < 10 ? '0' + m : m) + '分'
  179. if (s > 0) res += (s < 10 ? '0' + s : s) + '秒'
  180. return res
  181. }
  182. }
  183. };
  184. })();

QingJ © 2025

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