Skip to content

Instantly share code, notes, and snippets.

@ibnishak
Created July 18, 2022 03:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ibnishak/5611287f9529684b5a366890c3172b61 to your computer and use it in GitHub Desktop.
Save ibnishak/5611287f9529684b5a366890c3172b61 to your computer and use it in GitHub Desktop.
Updating HTML with new tags using goquery
file, err := os.OpenFile("b.html", os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0o755)
if err != nil {
fmt.Println(err.Error()) // TODO Error handling
}
defer file.Close()
doc, err := goquery.NewDocumentFromReader(file)
if err != nil {
fmt.Println(err.Error()) // TODO Error handling
}
l := doc.Find("body")
node := html.Node{
Type: html.ElementNode,
DataAtom: atom.Script,
Data: "script",
Attr: []html.Attribute{
{
Key: "src",
Val: "/hello.js",
},
},
}
l.AppendNodes(&node)
html, err := doc.Selection.Html()
if err != nil {
fmt.Println(err.Error()) // TODO Error handling
}
_, err = file.WriteAt([]byte(html), 0)
if err != nil {
fmt.Println(err.Error()) // TODO Error handling
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment