@@ -48,10 +48,11 @@ function staticServer(root) {
48
48
var hasNoOrigin = ! req . headers . origin ;
49
49
var injectCandidates = [
50
50
new RegExp ( '</body>' , 'i' ) ,
51
- new RegExp ( '</svg>' ) ,
51
+ new RegExp ( '</svg>' , 'g' ) ,
52
52
new RegExp ( '</head>' , 'i' ) ,
53
53
] ;
54
54
var injectTag = null ;
55
+ var injectCount = 0 ;
55
56
56
57
function directory ( ) {
57
58
var pathname = url . parse ( req . originalUrl ) . pathname ;
@@ -62,15 +63,16 @@ function staticServer(root) {
62
63
63
64
function file ( filepath /*, stat*/ ) {
64
65
var x = path . extname ( filepath ) . toLocaleLowerCase ( ) ,
65
- match ,
66
+ matches ,
66
67
possibleExtensions = [ '' , '.html' , '.htm' , '.xhtml' , '.php' , '.svg' ] ;
67
68
if ( hasNoOrigin && possibleExtensions . indexOf ( x ) > - 1 ) {
68
69
// TODO: Sync file read here is not nice, but we need to determine if the html should be injected or not
69
70
var contents = fs . readFileSync ( filepath , 'utf8' ) ;
70
71
for ( var i = 0 ; i < injectCandidates . length ; ++ i ) {
71
- match = injectCandidates [ i ] . exec ( contents ) ;
72
- if ( match ) {
73
- injectTag = match [ 0 ] ;
72
+ matches = contents . match ( injectCandidates [ i ] ) ;
73
+ injectCount = matches && matches . length || 0 ;
74
+ if ( injectCount ) {
75
+ injectTag = matches [ 0 ] ;
74
76
break ;
75
77
}
76
78
}
@@ -94,7 +96,7 @@ function staticServer(root) {
94
96
function inject ( stream ) {
95
97
if ( injectTag ) {
96
98
// We need to modify the length given to browser
97
- var len = INJECTED_CODE . length + res . getHeader ( 'Content-Length' ) ;
99
+ var len = INJECTED_CODE . length * injectCount + res . getHeader ( 'Content-Length' ) ;
98
100
res . setHeader ( 'Content-Length' , len ) ;
99
101
var originalPipe = stream . pipe ;
100
102
stream . pipe = function ( resp ) {
0 commit comments