Skip to content

Commit 21bb4b4

Browse files
committed
Merge pull request php-debugbar#167 from maximebf/xhrlistener
Add bindToXHR method
2 parents 9010755 + 7d13728 commit 21bb4b4

File tree

2 files changed

+46
-1
lines changed

2 files changed

+46
-1
lines changed

src/DebugBar/JavascriptRenderer.php

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@ class JavascriptRenderer
6969
protected $ajaxHandlerClass = 'PhpDebugBar.AjaxHandler';
7070

7171
protected $ajaxHandlerBindToJquery = true;
72+
73+
protected $ajaxHandlerBindToXHR = false;
7274

7375
protected $openHandlerClass = 'PhpDebugBar.OpenHandler';
7476

@@ -446,6 +448,27 @@ public function isAjaxHandlerBoundToJquery()
446448
return $this->ajaxHandlerBindToJquery;
447449
}
448450

451+
/**
452+
* Sets whether to call bindToXHR() on the ajax handler
453+
*
454+
* @param boolean $bind
455+
*/
456+
public function setBindAjaxHandlerToXHR($bind = true)
457+
{
458+
$this->ajaxHandlerBindToXHR = $bind;
459+
return $this;
460+
}
461+
462+
/**
463+
* Checks whether bindToXHR() will be called on the ajax handler
464+
*
465+
* @return boolean
466+
*/
467+
public function isAjaxHandlerBoundToXHR()
468+
{
469+
return $this->ajaxHandlerBindToXHR;
470+
}
471+
449472
/**
450473
* Sets the class name of the js open handler
451474
*
@@ -817,7 +840,9 @@ protected function getJsInitializationCode()
817840

818841
if ($this->ajaxHandlerClass) {
819842
$js .= sprintf("%s.ajaxHandler = new %s(%s);\n", $this->variableName, $this->ajaxHandlerClass, $this->variableName);
820-
if ($this->ajaxHandlerBindToJquery) {
843+
if ($this->ajaxHandlerBindToXHR) {
844+
$js .= sprintf("%s.ajaxHandler.bindToXHR();\n", $this->variableName);
845+
} else if ($this->ajaxHandlerBindToJquery) {
821846
$js .= sprintf("if (jQuery) %s.ajaxHandler.bindToJquery(jQuery);\n", $this->variableName);
822847
}
823848
}

src/DebugBar/Resources/debugbar.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1078,6 +1078,26 @@ if (typeof(PhpDebugBar) == 'undefined') {
10781078
self.handle(xhr);
10791079
}
10801080
});
1081+
},
1082+
1083+
/**
1084+
* Attaches an event listener to XMLHttpRequest
1085+
*
1086+
* @this {AjaxHandler}
1087+
*/
1088+
bindToXHR: function() {
1089+
var self = this;
1090+
var proxied = XMLHttpRequest.prototype.open;
1091+
XMLHttpRequest.prototype.open = function(method, url, async, user, pass) {
1092+
var xhr = this;
1093+
this.addEventListener("readystatechange", function() {
1094+
var skipUrl = self.debugbar.openHandler ? self.debugbar.openHandler.get('url') : null;
1095+
if (xhr.readyState == 4 && url.indexOf(skipUrl) !== 0) {
1096+
self.handle(xhr);
1097+
}
1098+
}, false);
1099+
proxied.apply(this, Array.prototype.slice.call(arguments));
1100+
};
10811101
}
10821102

10831103
});

0 commit comments

Comments
 (0)