Skip to content

Commit

Permalink
fix: These middleware should insert html content only on html pages
Browse files Browse the repository at this point in the history
  • Loading branch information
gregorybesson committed Jul 13, 2017
1 parent cc660e9 commit 688503e
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 14 deletions.
14 changes: 9 additions & 5 deletions src/server/middlewares/liveReload.js
Expand Up @@ -11,11 +11,15 @@ var middleware = function(req, res, next) {
if( req.url.indexOf('/abe/') > -1) {
var send = res.send
res.send = function (string) {
var body = string instanceof Buffer ? string.toString() : string
body = body.replace(/<\/body>/g, function (w) {
return '<script src="/abecms/libs/livereload.js?snipver=1&host=localhost&port='+port+'"></script>\n<script>document.addEventListener("LiveReloadDisconnect", function() {\nsetTimeout(function() {\nwindow.top.location.reload()\n}, 2000);\n})\n</script>' + w
})
send.call(this, body)
if(typeof string === 'string' || string instanceof Buffer){
var body = string instanceof Buffer ? string.toString() : string
body = body.replace(/<\/body>/g, function (w) {
return '<script src="/abecms/libs/livereload.js?snipver=1&host=localhost&port='+port+'"></script>\n<script>document.addEventListener("LiveReloadDisconnect", function() {\nsetTimeout(function() {\nwindow.top.location.reload()\n}, 2000);\n})\n</script>' + w
})
send.call(this, body)
} else {
send.call(this, string)
}
}
next()
} else {
Expand Down
22 changes: 13 additions & 9 deletions src/server/middlewares/login.js
@@ -1,15 +1,19 @@
var middleware = function(req, res, next) {
if( req.url.indexOf('/abe/') > -1) {
if( req.url.indexOf('/abe/') > -1 && req.url.indexOf('/abe/rest/') < 0) {
var send = res.send
res.send = function (string) {
var body = string instanceof Buffer ? string.toString() : string
body = body.replace(/<\/body>/g, function (w) {
return '<input type=\'hidden\' id=\'globalCsrfToken\' value=\'' + res.locals._csrf + '\' /><script src=\'/abecms/scripts/user-login-compiled.js\'></script>' + w
})
body = body.replace(/<\/form>/g, function (w) {
return '<input type=\'hidden\' name=\'_csrf\' value=\'' + res.locals._csrf + '\' />' + w
})
send.call(this, body)
if(typeof string === 'string' || string instanceof Buffer){
var body = string instanceof Buffer ? string.toString() : string
body = body.replace(/<\/body>/g, function (w) {
return '<input type=\'hidden\' id=\'globalCsrfToken\' value=\'' + res.locals._csrf + '\' /><script src=\'/abecms/scripts/user-login-compiled.js\'></script>' + w
})
body = body.replace(/<\/form>/g, function (w) {
return '<input type=\'hidden\' name=\'_csrf\' value=\'' + res.locals._csrf + '\' />' + w
})
send.call(this, body)
} else {
send.call(this, string)
}
}
// res.locals.csrfToken = token
next()
Expand Down

0 comments on commit 688503e

Please sign in to comment.