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.6
  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. * @brief 将参数字符串与参数对象互相转换
  87. * @param {string, object} params 参数字符串或参数对象,若传入 URL 将自动取其参数字符串作为参数
  88. * @return {string, object} 转换后的参数对象或参数字符串,若转换失败返回 null
  89. */
  90. function parseQuery(params) {
  91. if (typeof params === 'object') {
  92. let str = '';
  93. for (let k in params)
  94. str += `${k}=${encodeURIComponent(params[k])}`;
  95. return str;
  96. } else if (typeof params === 'string') {
  97. let index = params.indexOf('?');
  98. let url = index >= 0 ? params.substring(index + 1) : params;
  99. let obj = {};
  100. let kvs = url.split('&');
  101. for (let kv of kvs) {
  102. let [k, v] = kv.split('=');
  103. obj[k] = decodeURIComponent(v);
  104. };
  105. return obj;
  106. } else {
  107. return null;
  108. };
  109. };
  110.  
  111. // 以上所有函数被定义于 window.utils 中
  112. window.utils = {
  113. sendRequest: sendRequest,
  114. downloadCsv: downloadCsv,
  115. copyToClipboard: copyToClipboard,
  116. decode: decode,
  117. parseQuery: parseQuery,
  118. };
  119. })();

QingJ © 2025

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