From 6b37abf045c23f154c853e5e01b1a24469b9d428 Mon Sep 17 00:00:00 2001 From: Nikos M Date: Fri, 10 Jun 2022 11:21:55 +0300 Subject: [PATCH] v.1.0.0 contd --- README.md | 2 +- src/AjaxListener.js | 33 ++++++++++++++++++++++++++++----- src/AjaxListener.min.js | 2 +- test/listener.js | 2 +- 4 files changed, 31 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index e1df7b0..4aa98ca 100644 --- a/README.md +++ b/README.md @@ -12,7 +12,7 @@ Version: **1.0.0** (5 kB minified) function mylistener(req, res) { console.log('REQUEST', 'API', req.getAPI(), 'Method', req.getMethod(), 'URL', req.getURL(), 'Headers', req.getHeaders(), 'Body', req.getBody()); - console.log('RESPONSE', 'API', res.getAPI(), 'Status', res.getStatus(), 'Headers', res.getHeaders(), 'Body', res.getBody()); + console.log('RESPONSE', 'API', res.getAPI(), 'Status', res.getStatus(), 'URL', res.getURL(), 'Headers', res.getHeaders(), 'Body', res.getBody()); } AjaxListener.install().onRequest(mylistener); diff --git a/src/AjaxListener.js b/src/AjaxListener.js index 447d375..94c31c5 100644 --- a/src/AjaxListener.js +++ b/src/AjaxListener.js @@ -213,14 +213,16 @@ Request.prototype = { }; AjaxListener.Request = Request; -function Response(api, status, _headersFactory, _body) +function Response(api, status, _url, _headersFactory, _body) { - var self = this, headers = null, body = null; + var self = this, url = null, headers = null, body = null; self.dispose = function() { status = null; + _url = null; _headersFactory = null; _body = null; + url = null; headers = null; body = null; return self; @@ -231,6 +233,26 @@ function Response(api, status, _headersFactory, _body) self.getStatus = function() { return status; }; + self.getURL = function(raw) { + if (true === raw) return _url; + if (null == url && null != _url) + { + var u = new window.URL(_url, get_base_url()); + url = { + href: u.href, + origin: u.origin, + hostname: u.hostname, + protocol: u.protocol, + host: u.host, + port: u.port, + path: u.pathname, + query: u.search, + hash: u.hash, + queryParams: u.search && u.search.length ? parse_url_params(u.search.slice(1)) : {} + }; + } + return url; + }; self.getHeaders = function() { if (null == headers && null != _headersFactory) { @@ -267,6 +289,7 @@ Response.prototype = { ,dispose: null ,getAPI: null ,getStatus: null + ,getURL: null ,getHeaders: null ,getBody: null }; @@ -284,7 +307,7 @@ function listenerFetch(request) request.text().then(function(requestText) { notify( new Request('fetch', request.method, request.url, factory(extract_headers, request.headers), requestText), - new Response('fetch', response.status, factory(extract_headers, response.headers), responseText) + new Response('fetch', response.status, response.url || request.url, factory(extract_headers, response.headers), responseText) ); }); } @@ -292,7 +315,7 @@ function listenerFetch(request) { notify( new Request('fetch', 'GET', request, factory(null, {}), ''), - new Response('fetch', response.status, factory(extract_headers, response.headers), responseText) + new Response('fetch', response.status, response. url || request, factory(extract_headers, response.headers), responseText) ); } })/*.catch(function(err) {})*/; @@ -323,7 +346,7 @@ function listenerOpen(method, url) { notify( new Request('xhr', method, url, factory(null, headers), null == body ? '' : body), - new Response('xhr', self.status, factory(parse_headers, self.getAllResponseHeaders()), self.responseText) + new Response('xhr', self.status, self.responseURL && self.responseURL.length ? self.responseURL : url, factory(parse_headers, self.getAllResponseHeaders()), self.responseText) ); } }; diff --git a/src/AjaxListener.min.js b/src/AjaxListener.min.js index 3db3e42..de99dc7 100644 --- a/src/AjaxListener.min.js +++ b/src/AjaxListener.min.js @@ -5,4 +5,4 @@ * https://github.com/foo123/AjaxListener.js * **/ -!function(e,t){"use strict";e[t]=function(){"use strict";var r={VERSION:"1.0.0"},l=[],n=false,s=Object.prototype.hasOwnProperty,e=Function.prototype.toString,t=/[\r\n]+/,o=/^[^\[\]\.]+(\[[^\[\]\.]+\])+(\[\])?$/,i=/\[([^\[\]]+)\]/g,u=/^\s*function\s*[A-Za-z0-9_$]+\s*\([^\(\)]*\)\s*\{\s*\[native code\]\s*\}\s*$/i,f=window.fetch,a=window.XMLHttpRequest,c,p,d;function h(){return window.location.origin+window.location.pathname}function g(n){return"function"===typeof n&&u.test(e.call(n))}function w(n){return window.FormData&&n instanceof window.FormData}function y(n){return o.test(n)?n.replace(i,".$1").split(".").map(function(n){return decodeURIComponent(n)}):[decodeURIComponent(n)]}function v(n,e,t){for(var r,o=0,i=e.length;o