{"action":"created","issue":{"url":"https://api.github.com/repos/nodejitsu/node-http-proxy/issues/796","labels_url":"https://api.github.com/repos/nodejitsu/node-http-proxy/issues/796/labels{/name}","comments_url":"https://api.github.com/repos/nodejitsu/node-http-proxy/issues/796/comments","events_url":"https://api.github.com/repos/nodejitsu/node-http-proxy/issues/796/events","html_url":"https://github.com/nodejitsu/node-http-proxy/issues/796","id":64280969,"number":796,"title":"how to rewrite html body","user":{"login":"dabeike","id":9197693,"avatar_url":"https://avatars.githubusercontent.com/u/9197693?v=3","gravatar_id":"","url":"https://api.github.com/users/dabeike","html_url":"https://github.com/dabeike","followers_url":"https://api.github.com/users/dabeike/followers","following_url":"https://api.github.com/users/dabeike/following{/other_user}","gists_url":"https://api.github.com/users/dabeike/gists{/gist_id}","starred_url":"https://api.github.com/users/dabeike/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/dabeike/subscriptions","organizations_url":"https://api.github.com/users/dabeike/orgs","repos_url":"https://api.github.com/users/dabeike/repos","events_url":"https://api.github.com/users/dabeike/events{/privacy}","received_events_url":"https://api.github.com/users/dabeike/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":2,"created_at":"2015-03-25T14:21:42Z","updated_at":"2015-03-26T08:53:12Z","closed_at":null,"body":"how to change the contents of html,when the page is received from proxy?\r\nmy aim is to add some html code to the page,when the page returns to client\r\nthe code is like followings:\r\n\r\n    proxy.on('proxyRes', function (proxyRes, request, response){\r\n         proxyRes.on('end',function(){\r\n                if(proxyRes.headers['content-type']=='text/html;charset=utf-8'){\r\n                    proxyRes.write('hhhhhhhhhhh');   //this sentence may be wrong\r\n                }\r\n            })\r\n    })"},"comment":{"url":"https://api.github.com/repos/nodejitsu/node-http-proxy/issues/comments/86408148","html_url":"https://github.com/nodejitsu/node-http-proxy/issues/796#issuecomment-86408148","issue_url":"https://api.github.com/repos/nodejitsu/node-http-proxy/issues/796","id":86408148,"user":{"login":"kokarn","id":1373940,"avatar_url":"https://avatars.githubusercontent.com/u/1373940?v=3","gravatar_id":"","url":"https://api.github.com/users/kokarn","html_url":"https://github.com/kokarn","followers_url":"https://api.github.com/users/kokarn/followers","following_url":"https://api.github.com/users/kokarn/following{/other_user}","gists_url":"https://api.github.com/users/kokarn/gists{/gist_id}","starred_url":"https://api.github.com/users/kokarn/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/kokarn/subscriptions","organizations_url":"https://api.github.com/users/kokarn/orgs","repos_url":"https://api.github.com/users/kokarn/repos","events_url":"https://api.github.com/users/kokarn/events{/privacy}","received_events_url":"https://api.github.com/users/kokarn/received_events","type":"User","site_admin":false},"created_at":"2015-03-26T08:53:12Z","updated_at":"2015-03-26T08:53:12Z","body":"I'm not at all sure what's the best way to do this, but this is a scrip we've used successfully to inject a script into a page.\r\n\r\n```\r\nvar scriptElm = '<script src=\"test.js\"></script>';\r\n\r\nfunction modifyHtml( str ) {\r\n    // Add or script to the page\r\n    if( str.indexOf( '</body>' ) > -1 ) {\r\n        str = str.replace( '</body>', scriptElm + '</body>' );\r\n    } else if ( str.indexOf( '</html>' ) > -1 ){\r\n        str = str.replace( '</html>', scriptElm + '</html>' );\r\n    } else {\r\n        str = str + scriptElm;\r\n    }\r\n\r\n    return str;\r\n}\r\n\r\nproxy.on( 'proxyRes', function ( proxyRes, request, response ) {\r\n    if( proxyRes.headers &&\r\n        proxyRes.headers[ 'content-type' ] &&\r\n        proxyRes.headers[ 'content-type' ].match( 'text/html' ) ) {\r\n\r\n        var _end = response.end,\r\n            chunks,\r\n            _writeHead = response.writeHead;\r\n\r\n        response.writeHead = function(){\r\n            if( proxyRes.headers && proxyRes.headers[ 'content-length' ] ){\r\n                response.setHeader(\r\n                    'content-length',\r\n                    parseInt( proxyRes.headers[ 'content-length' ], 10 ) + scriptElm.length\r\n                );\r\n            }\r\n\r\n            // This disables chunked encoding\r\n            response.setHeader( 'transfer-encoding', '' );\r\n\r\n            // Disable cache for all http as well\r\n            response.setHeader( 'cache-control', 'no-cache' );\r\n\r\n            _writeHead.apply( this, arguments );\r\n        };\r\n\r\n        response.write = function( data ) {\r\n            if( chunks ) {\r\n                chunks += data;\r\n            } else {\r\n                chunks = data;\r\n            }\r\n        };\r\n\r\n        response.end = function() {\r\n            if( chunks && chunks.toString ) {\r\n                _end.apply( response, [ modifyHtml( chunks.toString() ) ] );\r\n            } else {\r\n                _end.apply( response, arguments );\r\n            }\r\n        };\r\n    }\r\n});\r\n```"}}