Coder Utils

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

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

  1. // ==UserScript==
  2. // @name Coder Utils
  3. // @name:en Coder Utils
  4. // @namespace http://tampermonkey.net/
  5. // @version 0.1.4
  6. // @description 【使用前先看介绍/有问题可反馈】【欢迎一键三连(好评+打赏+收藏),你的支持是作者维护下去的最大动力!】Coder Utils(程序员专属工具)为程序员专门准备的常用 JavaScript 函数;目前已有:发送请求、下载文件、文本复制、文本解码;脚本定义的所有函数被定义于 window.utils 中,具体函数使用说明请参照脚本代码详情页。
  7. // @description:en 【使用前先看介绍/有问题可反馈】【欢迎一键三连(好评+打赏+收藏),你的支持是作者维护下去的最大动力!】Coder Utils(程序员专属工具)为程序员专门准备的常用 JavaScript 函数;目前已有:发送请求、下载文件、文本复制、文本解码;脚本定义的所有函数被定义于 window.utils 中,具体函数使用说明请参照脚本代码详情页。
  8. // @author cc
  9. // @include *
  10. // @grant none
  11. // ==/UserScript==
  12.  
  13. (function() {
  14. 'use strict';
  15. /**
  16. * @brief 发送请求,若请求成功,请求结果将存储于 utils.response
  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 (mode == 'GET') {
  25. if (url.indexOf('?') < 0 && params.length > 0) {
  26. url = `${url}?${encodeURIComponent(params)}`;
  27. };
  28. } else if (mode == 'POST') {
  29. if (url.indexOf('?') >= 0) {
  30. let index = url.indexOf('?');
  31. params = url.substring(index) + 1;
  32. url = url.substring(0, index);
  33. };
  34. };
  35. request.open(mode, url, true);
  36. request.setRequestHeader('Content-Type', 'application/json');
  37. if (params.length > 0) {
  38. request.send(params);
  39. } else {
  40. request.send();
  41. };
  42. request.onreadystatechange = function () {
  43. if (request.readyState == 4 && request.status == 200) {
  44. utils.response = request.responseText;
  45. };
  46. };
  47. };
  48. /**
  49. * @brief 下载 CSV 文件
  50. * @param {string} csvContent CSV 数据,请使用 ',' 分隔数据值,使用 '\n' 分隔数据行,默认为空字符串
  51. * @param {string} fileName 下载的 CSV 文件名,默认为 data.csv
  52. * @return {void}
  53. */
  54. function downloadCsv(csvContent='', fileName='data.csv') {
  55. let pom = document.createElement('a');
  56. let blob = new Blob(['\ufeff' + csvContent], {type: 'text/csv;charset=utf-8;'});
  57. let url = URL.createObjectURL(blob);
  58. pom.href = url;
  59. pom.setAttribute('download', fileName);
  60. document.body.appendChild(pom);
  61. pom.click();
  62. document.body.removeChild(pom);
  63. };
  64. /**
  65. * @brief 将字符串复制至剪切板
  66. * @param {string} content 需要复制到剪切板的内容,默认为空字符串
  67. * @return {void}
  68. */
  69. function copyToClipboard(content='') {
  70. let textarea = document.createElement('textarea');
  71. textarea.value = content;
  72. document.body.appendChild(textarea);
  73. textarea.select();
  74. document.execCommand('copy');
  75. document.body.removeChild(textarea);
  76. };
  77. /**
  78. * @brief 解码字符串中的 \u 字符为 unicode 字符
  79. * @param {string} content 需要解码的含有 \u 开头字符的字符串
  80. * @return {string} 解码后的字符串
  81. */
  82. function decode(content='') {
  83. return unescape(content.replaceAll(/\\u/g, '%u'));
  84. };
  85.  
  86. // 以上所有函数被定义于 window.utils 中
  87. window.utils = {
  88. sendRequest: sendRequest,
  89. downloadCsv: downloadCsv,
  90. copyToClipboard: copyToClipboard,
  91. decode: decode,
  92. };
  93. })();

QingJ © 2025

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