SinHelper

Sin Helper

目前为 2023-07-24 提交的版本。查看 最新版本

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

  1. ; (function () {
  2. window.sinHelper = {
  3. /* 网络拦截相关 */
  4. Xhr: {
  5. init: function () {
  6. let oldSend = XMLHttpRequest.prototype.send, _this = this;
  7. XMLHttpRequest.prototype.send = function () {
  8. let xhr = this
  9. this.addEventListener("load", function () {
  10. if (xhr.readyState != 4 || xhr.status != 200) return;
  11. if (xhr.responseType != '' && xhr.responseType != 'text') return;
  12. xhr.requestData = arguments
  13. xhr.responseHeders = xhr.getAllResponseHeaders()
  14. xhr.responseData = function () {
  15. try {
  16. return JSON.parse(xhr.responseText)
  17. } catch (e) {
  18. return xhr.responseText
  19. }
  20. }();
  21. _this.dispatchFetch(xhr)
  22. })
  23. oldSend.apply(this, arguments);
  24. }
  25. },
  26. rules: {}, // pathname => function
  27. routes: [], // functions
  28. registRules: function (ruleName, ruleFunc) {
  29. if (typeof (ruleFunc) != 'function') {
  30. console.error("Xhr Rules 必须是可调用的函数", ruleName)
  31. return
  32. }
  33. this.rules[ruleName] = ruleFunc
  34. },
  35. /* 调用注册(不可用)的 pathname 处理函数 */
  36. dispatchFetch: function (xhr) {
  37. let _this = this
  38. const url = new URL(xhr.responseURL)
  39. if (_this.rules[url.pathname]) {
  40. if (typeof (this.rules[url.pathname]) == 'function') {
  41. this.rules[url.pathname](xhr)
  42. } else {
  43. console.error("Xhr 处理函数错误:", url.pathname)
  44. }
  45. } else {
  46. _this.routes.forEach((route) => {
  47. if (typeof (route) == 'function') {
  48. route(xhr)
  49. }
  50. })
  51. }
  52. }
  53. },
  54. /* 原生JS 下载Excel */
  55. Excel: {
  56. 'trans2Base64': function (content) {
  57. return window.btoa(unescape(encodeURIComponent(content)));
  58. },
  59. 'exportExcelFromFront': function (params) {
  60. let _this = this
  61. const { cellList, headerList, caption, exportName = 'exportName' } = params;
  62.  
  63. const captionEle = caption ? `<caption>${caption}</caption>` : ''; // 表格标题
  64. const headerEle = `<tr>${headerList?.map((item) => `<th>${item}</th>`)?.join('')}</tr>`;
  65. const cellEle = cellList
  66. ?.map((itemRow) => `<tr>${itemRow?.map((itemCell) => `<td>${itemCell}</td>`)?.join('')}</tr>`)
  67. ?.join('');
  68.  
  69. const excelContent = `${captionEle}${headerEle}${cellEle}`;
  70. let worksheet = '工作表1';
  71. let excelFile =
  72. "<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'>";
  73. excelFile +=
  74. '<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>';
  75. excelFile += "<body><table width='10%' border='1'>";
  76. excelFile += excelContent;
  77. excelFile += '</table></body>';
  78. excelFile += '</html>';
  79. const link = `data:application/vnd.ms-excel;base64,${_this.trans2Base64(excelFile)}`;
  80. const a = document.createElement('a');
  81. a.download = `${exportName}.xlsx`;
  82. a.href = link;
  83. a.click();
  84. }
  85. }
  86. };
  87. })();

QingJ © 2025

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