Skip to content

Commit

Permalink
Fix an issue with inserting JS into HTML
Browse files Browse the repository at this point in the history
  • Loading branch information
xyproto committed Jun 12, 2024
1 parent 2fbf1fb commit 425ecd5
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions engine/sse.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,11 @@ func InsertScriptTag(htmldata, js []byte) []byte {
js = append(js, []byte("</script>")...)
}

// If there is no htmldata, then just return the script tag with JS
if len(htmldata) == 0 {
return js
}

// Place the script at the end of the body, if there is a body
switch {
case bytes.Contains(htmldata, []byte("</body>")):
Expand All @@ -73,6 +78,6 @@ func InsertScriptTag(htmldata, js []byte) []byte {
return bytes.Replace(htmldata, []byte("<html>"), append(append([]byte("<html><head>"), js...), []byte("</head>")...), 1)
}

// In the unlikely event that no place to insert the JavaScript was found
return htmldata
// In the unlikely event that no place to insert the JavaScript was found, just add the script tag to the end
return append(htmldata, js...)
}

0 comments on commit 425ecd5

Please sign in to comment.