Skip to content

Commit

Permalink
make bullet_xhr.js a valid js
Browse files Browse the repository at this point in the history
  • Loading branch information
flyerhzm committed Feb 14, 2019
1 parent acba9f7 commit b5e89cc
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 52 deletions.
100 changes: 49 additions & 51 deletions lib/bullet/bullet_xhr.js
@@ -1,55 +1,53 @@
<script language="JavaScript">
(function(){
var oldOpen = window.XMLHttpRequest.prototype.open;
var oldSend = window.XMLHttpRequest.prototype.send;
function newOpen(method, url, async, user, password) {
this._storedUrl = url;
return oldOpen.apply(this, arguments);
(function(){
var oldOpen = window.XMLHttpRequest.prototype.open;
var oldSend = window.XMLHttpRequest.prototype.send;
function newOpen(method, url, async, user, password) {
this._storedUrl = url;
return oldOpen.apply(this, arguments);
}
function newSend(data) {
if (this.onload) {
this._storedOnload = this.onload;
}
function newSend(data) {
if (this.onload) {
this._storedOnload = this.onload;
this.onload = newOnload;
return oldSend.apply(this, arguments);
}
function newOnload() {
if (this._storedUrl.startsWith(window.location.protocol + '//' + window.location.host) ||
!this._storedUrl.startsWith("http") // For relative paths
) {
var bulletFooterText = this.getResponseHeader("X-bullet-footer-text");
if (bulletFooterText) {
setTimeout(() => {
var oldHtml = document.getElementById("bullet-footer").innerHTML.split("<br>");
var header = oldHtml[0]
oldHtml = oldHtml.slice(1, oldHtml.length);
var newHtml = oldHtml.concat(JSON.parse(bulletFooterText));
newHtml = newHtml.slice(newHtml.length - 10, newHtml.length); // rotate through 10 most recent
document.getElementById(
"bullet-footer"
).innerHTML = `${header}<br>${newHtml.join("<br>")}`;
}, 0);
}
this.onload = newOnload;
return oldSend.apply(this, arguments);
}
function newOnload() {
if (this._storedUrl.startsWith(window.location.protocol + '//' + window.location.host) ||
!this._storedUrl.startsWith("http") // For relative paths
) {
var bulletFooterText = this.getResponseHeader("X-bullet-footer-text");
if (bulletFooterText) {
setTimeout(() => {
var oldHtml = document.getElementById("bullet-footer").innerHTML.split("<br>");
var header = oldHtml[0]
oldHtml = oldHtml.slice(1, oldHtml.length);
var newHtml = oldHtml.concat(JSON.parse(bulletFooterText));
newHtml = newHtml.slice(newHtml.length - 10, newHtml.length); // rotate through 10 most recent
document.getElementById(
"bullet-footer"
).innerHTML = `${header}<br>${newHtml.join("<br>")}`;
}, 0);
}
var bulletConsoleText = this.getResponseHeader("X-bullet-console-text");
if (bulletConsoleText && typeof(console) !== 'undefined' && console.log) {
setTimeout(() => {
JSON.parse(bulletConsoleText).forEach(message => {
if (console.groupCollapsed && console.groupEnd) {
console.groupCollapsed("Uniform Notifier");
console.log(message);
console.groupEnd();
} else {
console.log(message);
}
});
}, 0);
}
}
if (this._storedOnload) {
return this._storedOnload.apply(this, arguments);
var bulletConsoleText = this.getResponseHeader("X-bullet-console-text");
if (bulletConsoleText && typeof(console) !== 'undefined' && console.log) {
setTimeout(() => {
JSON.parse(bulletConsoleText).forEach(message => {
if (console.groupCollapsed && console.groupEnd) {
console.groupCollapsed("Uniform Notifier");
console.log(message);
console.groupEnd();
} else {
console.log(message);
}
});
}, 0);
}
}
window.XMLHttpRequest.prototype.open = newOpen;
window.XMLHttpRequest.prototype.send = newSend;
})();
</script>
if (this._storedOnload) {
return this._storedOnload.apply(this, arguments);
}
}
window.XMLHttpRequest.prototype.open = newOpen;
window.XMLHttpRequest.prototype.send = newSend;
})();
2 changes: 1 addition & 1 deletion lib/bullet/rack.rb
Expand Up @@ -108,7 +108,7 @@ def footer_header

# Make footer work for XHR requests by appending data to the footer
def xhr_script
File.read("#{__dir__}/bullet_xhr.js")
"<script type='text/javascript'>#{File.read("#{__dir__}/bullet_xhr.js")}</script>"
end
end
end

0 comments on commit b5e89cc

Please sign in to comment.