From f3ca3f42a89a945e233e4c0808639181519c8f72 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9E=AB=E8=B0=B7=E5=89=91=E4=BB=99?= Date: Wed, 1 Apr 2020 17:36:21 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0GM=5FxmlhttpRequest?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- script-universal_function.js | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/script-universal_function.js b/script-universal_function.js index 5292cd59..873a0452 100644 --- a/script-universal_function.js +++ b/script-universal_function.js @@ -1,22 +1,25 @@ -//仿GM_xmlhttpRequest函数v1.3 +//仿GM_xmlhttpRequest函数v1.4 const GM_xmlhttpRequest = function(GM_param) { const xhr = new XMLHttpRequest(); //创建XMLHttpRequest对象 xhr.open(GM_param.method, GM_param.url, true); if (GM_param.responseType) xhr.responseType = GM_param.responseType; if (GM_param.overrideMimeType) xhr.overrideMimeType(GM_param.overrideMimeType); - xhr.onreadystatechange = function() //设置回调函数 + xhr.onreadystatechange = function(e) //设置回调函数 { - if (xhr.readyState === xhr.DONE) { //请求完成时 - if (xhr.status === 200 && GM_param.onload) //正确加载时 + const _xhr = e.target; + if (_xhr.readyState === _xhr.DONE) { //请求完成时 + if (_xhr.status === 200 && GM_param.onload) //正确加载时 { - GM_param.onload(xhr); + GM_param.onload(_xhr); } - if (xhr.status !== 200 && GM_param.onerror) //发生错误时 + if (_xhr.status !== 200 && GM_param.onerror) //发生错误时 { - GM_param.onerror(xhr); + GM_param.onerror(_xhr); } } }; + if (GM_param.onprogress) + xhr.upload.onprogress = function(e){GM_param.onprogress(e.target)}; //添加header for (let header in GM_param.headers) { xhr.setRequestHeader(header, GM_param.headers[header]);