您需要先安装一个扩展,例如 篡改猴、Greasemonkey 或 暴力猴,之后才能安装此脚本。
您需要先安装一个扩展,例如 篡改猴 或 暴力猴,之后才能安装此脚本。
您需要先安装一个扩展,例如 篡改猴 或 暴力猴,之后才能安装此脚本。
您需要先安装一个扩展,例如 篡改猴 或 Userscripts ,之后才能安装此脚本。
您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey,才能安装此脚本。
您需要先安装用户脚本管理器扩展后才能安装此脚本。
【使用前先看介绍/有问题可反馈】【欢迎一键三连(好评+打赏+收藏),你的支持是作者维护下去的最大动力!】Coder Utils(程序员专属工具)为程序员专门准备的常用 JavaScript 函数;目前已有:发送请求、下载文件、文本复制、文本解码、参数转换;脚本定义的所有函数被定义于 window.utils 中,具体函数使用说明请参照脚本代码详情页。
当前为
- // ==UserScript==
- // @name Coder Utils
- // @name:en Coder Utils
- // @namespace http://tampermonkey.net/
- // @version 0.1.6
- // @description 【使用前先看介绍/有问题可反馈】【欢迎一键三连(好评+打赏+收藏),你的支持是作者维护下去的最大动力!】Coder Utils(程序员专属工具)为程序员专门准备的常用 JavaScript 函数;目前已有:发送请求、下载文件、文本复制、文本解码、参数转换;脚本定义的所有函数被定义于 window.utils 中,具体函数使用说明请参照脚本代码详情页。
- // @description:en 【使用前先看介绍/有问题可反馈】【欢迎一键三连(好评+打赏+收藏),你的支持是作者维护下去的最大动力!】Coder Utils(程序员专属工具)为程序员专门准备的常用 JavaScript 函数;目前已有:发送请求、下载文件、文本复制、文本解码、参数转换;脚本定义的所有函数被定义于 window.utils 中,具体函数使用说明请参照脚本代码详情页。
- // @author cc
- // @include *
- // @grant none
- // ==/UserScript==
- (function() {
- 'use strict';
- /**
- * @brief 发送请求,若请求成功,请求结果将存储于 utils.response
- * @param {string} url 请求 URL, GET 请求的参数既可以置于 url 也可以置于 params
- * @param {string} params 请求参数,形式为 'k1=v1&k2=v2(&...)',默认为无参数
- * @param {string} mode 请求类型,默认为 GET 请求
- * @return {void}
- */
- function sendRequest(url, params='', mode='GET') {
- let request = new XMLHttpRequest();
- if (mode == 'GET') {
- if (url.indexOf('?') < 0 && params.length > 0) {
- url = `${url}?${encodeURIComponent(params)}`;
- };
- } else if (mode == 'POST') {
- if (url.indexOf('?') >= 0) {
- let index = url.indexOf('?');
- params = url.substring(index) + 1;
- url = url.substring(0, index);
- };
- };
- request.open(mode, url, true);
- request.setRequestHeader('Content-Type', 'application/json');
- if (params.length > 0) {
- request.send(params);
- } else {
- request.send();
- };
- request.onreadystatechange = function () {
- if (request.readyState == 4 && request.status == 200) {
- utils.response = request.responseText;
- };
- };
- };
- /**
- * @brief 下载 CSV 文件
- * @param {string} csvContent CSV 数据,请使用 ',' 分隔数据值,使用 '\n' 分隔数据行,默认为空字符串
- * @param {string} fileName 下载的 CSV 文件名,默认为 data.csv
- * @return {void}
- */
- function downloadCsv(csvContent='', fileName='data.csv') {
- let pom = document.createElement('a');
- let blob = new Blob(['\ufeff' + csvContent], {type: 'text/csv;charset=utf-8;'});
- let url = URL.createObjectURL(blob);
- pom.href = url;
- pom.setAttribute('download', fileName);
- document.body.appendChild(pom);
- pom.click();
- document.body.removeChild(pom);
- };
- /**
- * @brief 将字符串复制至剪切板
- * @param {string} content 需要复制到剪切板的内容,默认为空字符串
- * @return {void}
- */
- function copyToClipboard(content='') {
- let textarea = document.createElement('textarea');
- textarea.value = content;
- document.body.appendChild(textarea);
- textarea.select();
- document.execCommand('copy');
- document.body.removeChild(textarea);
- };
- /**
- * @brief 解码字符串中的 \u 字符为 unicode 字符
- * @param {string} content 需要解码的含有 \u 开头字符的字符串
- * @return {string} 解码后的字符串
- */
- function decode(content='') {
- return unescape(content.replaceAll(/\\u/g, '%u'));
- };
- /**
- * @brief 将参数字符串与参数对象互相转换
- * @param {string, object} params 参数字符串或参数对象,若传入 URL 将自动取其参数字符串作为参数
- * @return {string, object} 转换后的参数对象或参数字符串,若转换失败返回 null
- */
- function parseQuery(params) {
- if (typeof params === 'object') {
- let str = '';
- for (let k in params)
- str += `${k}=${encodeURIComponent(params[k])}`;
- return str;
- } else if (typeof params === 'string') {
- let index = params.indexOf('?');
- let url = index >= 0 ? params.substring(index + 1) : params;
- let obj = {};
- let kvs = url.split('&');
- for (let kv of kvs) {
- let [k, v] = kv.split('=');
- obj[k] = decodeURIComponent(v);
- };
- return obj;
- } else {
- return null;
- };
- };
- // 以上所有函数被定义于 window.utils 中
- window.utils = {
- sendRequest: sendRequest,
- downloadCsv: downloadCsv,
- copyToClipboard: copyToClipboard,
- decode: decode,
- parseQuery: parseQuery,
- };
- })();
QingJ © 2025
镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址