Created
July 18, 2022 03:20
-
-
Save ibnishak/5611287f9529684b5a366890c3172b61 to your computer and use it in GitHub Desktop.
Updating HTML with new tags using goquery
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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