httpSend

发送http请求

目前為 2019-01-01 提交的版本,檢視 最新版本

此腳本不應該直接安裝,它是一個供其他腳本使用的函式庫。欲使用本函式庫,請在腳本 metadata 寫上: // @require https://update.greasyfork.org/scripts/376085/658449/httpSend.js

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

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

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// @grant        GM_xmlhttpRequest
/*
*使用httpSend()函数调用,需把@require放在// @grant       GM_xmlhttpRequest之后
*参数详解:
*url:请求的url,必须!
*type:请求方式(get/post),必须!
*mode:请求调用函数(jq/gm),必须!
*headers:自定义请求头,仅gm模式可用
*data:要发送的数据,仅post方式可用
*timeout:请求超时时间,单位毫秒
*dataType:返回数据类型(arraybuffer,blob,json)
*callback:请求完成回调函数
*username:用户名
*password:密码
*/
    function httpSend(i,e={}){
        if(!i.url){throw "缺少请求的url!"};
        if(!i.type){throw "缺少请求方式(get/post)!"};
        if(!i.mode){throw "缺少请求模式(jq/gm)!"};
        e.url=i.url;
        i.username&&(e.username=i.username);
        i.password&&(e.password=i.password);
        i.data&&(e.data=i.data);
        i.timeout?e.timeout=i.timeout:e.timeout=30000;
        i.headers&&(e.headers=i.headers);
        if(/^jq$/i.test(i.mode)){
            e.xhrFields={withCredentials: true};
            e.crossDomain=true;
            e.cache=false;
            e.type=i.type;
            i.dataType&&(e.dataType=i.dataType);
            i.callback&&(e.complete=function(e){i.callback(jqTransform(e))});
            $.ajax(e);
        }else if(/^gm$/i.test(i.mode)){
            i.type&&(e.method=i.type.toUpperCase());
            i.dataType&&(e.responseType=i.dataType);
            i.callback&&(e.onload=function(e){i.callback(gmTransform(e))});
            GM_xmlhttpRequest(e);
        }else{throw "此请求模式("+i.mode+")不存在!"}
    }
    function jqTransform(response){
        var data={};
        data.status=response.status;
        data.statusText=response.statusText;
        data.readyState=response.readyState;
        data.text=response.responseText;
        data.json=typeof response.response=="object"?response.response:toJSON(response.responseText);
        data.responseHeaders=response.responseHeaders;
        data.getResponseHeader=response.getResponseHeader;
        return data;
    }
    function gmTransform(response){
        var data={};
        data.status=response.status;
        data.statusText=response.statusText;
        data.readyState=response.readyState;
        data.text=response.responseText;
        data.json=response.response||toJSON(response.responseText);
        data.responseHeaders=response.responseHeaders;
        data.finalUrl=response.finalUrl;
        return data;
    }
    function toJSON(str) {
        if (typeof str == 'string'&&(/^\{[\w\W]*?\}$/gim.test(str))) {
            try {
                var json=eval('('+str+')');
                return json;
            } catch(e) {
                return {};
            }
        }
    }