Coder Utils

【使用前先看介绍/有问题可反馈】【欢迎一键三连(好评+打赏+收藏),你的支持是作者维护下去的最大动力!】Coder Utils(程序员专属工具)为程序员专门准备的常用 JavaScript 函数;目前已有:发送请求、下载Blob、下载文件、下载图片、文本复制、文本解码、参数转换;脚本定义的所有函数被定义于 window.utils 中,具体函数使用说明请参照脚本代码详情页。

当前为 2020-12-21 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Coder Utils
  3. // @name:en Coder Utils
  4. // @namespace http://tampermonkey.net/
  5. // @version 0.2.1
  6. // @description 【使用前先看介绍/有问题可反馈】【欢迎一键三连(好评+打赏+收藏),你的支持是作者维护下去的最大动力!】Coder Utils(程序员专属工具)为程序员专门准备的常用 JavaScript 函数;目前已有:发送请求、下载Blob、下载文件、下载图片、文本复制、文本解码、参数转换;脚本定义的所有函数被定义于 window.utils 中,具体函数使用说明请参照脚本代码详情页。
  7. // @description:en 【使用前先看介绍/有问题可反馈】【欢迎一键三连(好评+打赏+收藏),你的支持是作者维护下去的最大动力!】Coder Utils(程序员专属工具)为程序员专门准备的常用 JavaScript 函数;目前已有:发送请求、下载Blob、下载文件、下载图片、文本复制、文本解码、参数转换;脚本定义的所有函数被定义于 window.utils 中,具体函数使用说明请参照脚本代码详情页。
  8. // @author cc
  9. // @include *
  10. // @grant none
  11. // ==/UserScript==
  12.  
  13. (function() {
  14. 'use strict';
  15. /**
  16. * @brief 发送请求,若请求成功,请求结果将存储于 utils.response,请求 URL 将存储于 utils.url
  17. * @param {string} url 请求 URL, GET 请求的参数既可以置于 url 也可以置于 params
  18. * @param {string} params 请求参数,形式为 'k1=v1(&k2=v2&...)',默认为无参数
  19. * @param {string} mode 请求类型,默认为 GET 请求
  20. * @return {void}
  21. */
  22. function sendRequest(url, params='', mode='GET') {
  23. let request = new XMLHttpRequest();
  24. if (params.length > 0)
  25. url = `${url}?${encodeURIComponent(params)}`;
  26. request.open(mode, url, true);
  27. request.setRequestHeader('Content-Type', 'application/json');
  28. request.send();
  29. request.onreadystatechange = function () {
  30. if (request.readyState == 4 && request.status == 200) {
  31. utils.url = url;
  32. utils.response = JSON.parse(request.responseText);
  33. };
  34. };
  35. };
  36. /**
  37. * @brief 下载 Blob 对象
  38. * @param {Blob} blob Blob 对象
  39. * @param {string} fileName 下载的文件名,默认为 data.csv
  40. */
  41. function downloadBlob(blob, fileName) {
  42. let a = document.createElement('a');
  43. a.download = fileName;
  44. a.target = '_blank';
  45. a.href = URL.createObjectURL(blob);
  46. document.body.appendChild(a);
  47. a.click();
  48. document.body.removeChild(a);
  49. };
  50. /**
  51. * @brief 下载 CSV 文件
  52. * @param {string} csvContent CSV 数据,请使用 ',' 分隔数据值,使用 '\n' 分隔数据行,默认为空字符串
  53. * @param {string} fileName 下载的 CSV 文件名,默认为 data.csv
  54. * @return {void}
  55. */
  56. function downloadCsv(csvContent='', fileName='data.csv') {
  57. let a = document.createElement('a');
  58. let blob = new Blob(['\ufeff' + csvContent], {type: 'text/csv;charset=utf-8;'});
  59. utils.downloadBlob(blob, fileName);
  60. };
  61. /**
  62. * @brief 下载 base64 编码的图片
  63. * @param {string} base64Img base64 编码的图片
  64. * @param {string} fileName 下载的图片文件名,默认为 default.jpg
  65. */
  66. function downloadBase64Img(base64Img, fileName='default.jpg') {
  67. let img = atob(base64Img);
  68. let arr = new Uint8Array(img.length);
  69. for (let i = 0; i < img.length; ++i)
  70. arr[i] = img.charCodeAt(i);
  71. let blob = new Blob([arr], {type: 'image/png'});
  72. utils.downloadBlob(blob, fileName);
  73. };
  74. /**
  75. * @brief 将字符串复制至剪切板
  76. * @param {string} content 需要复制到剪切板的内容,默认为空字符串
  77. * @return {void}
  78. */
  79. function copyToClipboard(content='') {
  80. let textarea = document.createElement('textarea');
  81. textarea.value = content;
  82. document.body.appendChild(textarea);
  83. textarea.select();
  84. document.execCommand('copy');
  85. document.body.removeChild(textarea);
  86. };
  87. /**
  88. * @brief 解码字符串中的 \u 字符为 unicode 字符
  89. * @param {string} content 需要解码的含有 \u 开头字符的字符串
  90. * @return {string} 解码后的字符串
  91. */
  92. function decode(content='') {
  93. return unescape(content.replaceAll(/\\u/g, '%u'));
  94. };
  95. /**
  96. * @brief 将参数字符串与参数对象互相转换
  97. * @param {string, object} params 参数字符串或参数对象,若传入 URL 将自动取其参数字符串作为参数
  98. * @return {string, object} 转换后的参数对象或参数字符串,若转换失败返回 null
  99. */
  100. function parseQuery(params) {
  101. if (typeof params === 'object') {
  102. let str = '';
  103. for (let k in params)
  104. str += `${k}=${encodeURIComponent(params[k])}`;
  105. return str;
  106. } else if (typeof params === 'string') {
  107. let index = params.indexOf('?');
  108. let url = index >= 0 ? params.substring(index + 1) : params;
  109. let obj = {};
  110. let kvs = url.split('&');
  111. for (let kv of kvs) {
  112. let [k, v] = kv.split('=');
  113. obj[k] = decodeURIComponent(v);
  114. };
  115. return obj;
  116. } else {
  117. return null;
  118. };
  119. };
  120.  
  121. // 以上所有函数被定义于 window.utils 中
  122. window.utils = {
  123. sendRequest: sendRequest,
  124. downloadBlob: downloadBlob,
  125. downloadCsv: downloadCsv,
  126. downloadBase64Img: downloadBase64Img,
  127. copyToClipboard: copyToClipboard,
  128. decode: decode,
  129. parseQuery: parseQuery,
  130. };
  131. })();

QingJ © 2025

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