Coder Utils

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

Ekde 2020/12/14. Vidu La ĝisdata versio.

You will need to install an extension such as Tampermonkey, Greasemonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey or Userscripts to install this script.

You will need to install an extension such as Tampermonkey to install this script.

You will need to install a user script manager extension to install this script.

(I already have a user script manager, let me install it!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(I already have a user style manager, let me install it!)

// ==UserScript==
// @name         Coder Utils
// @name:en      Coder Utils
// @namespace    http://tampermonkey.net/
// @version      0.1.4
// @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'));
    };

    // 以上所有函数被定义于 window.utils 中
    window.utils = {
        sendRequest: sendRequest,
        downloadCsv: downloadCsv,
        copyToClipboard: copyToClipboard,
        decode: decode,
    };
})();