I’ve performed round a giant with web/html library and I’ve been capable of modify every “src” attribute of every “img” tag to level to a distinct picture. Nevertheless, I’m a bit stumped right here as a result of I’m attempting to do one thing a bit extra advanced. I have to insert some HTML right into a pre-existing head tag. What I have to insert loks like this:
<![CDATA[n<script type="text/javascript">nvar continentCode="EU";nvar doComplianceCheck=true;n</script>n<script src="/another.js"></script>n<script src="path/to/some.js"></script>]]>
Is there a easy option to slap that into the pinnacle tag?
Thanks upfront for any help.
_Ramy
The best manner is to make use of Javascript so as to add new code to innerHTML.
Not that straightforward however type of “Go innerHTML” is to render each to buffer after which mix them into a brand new web page. Pseudocode
var tpl *template.Template
var hdrbuf bytes.Buffer
var pagebuf bytes.Buffer
func most important() {
insert := `<div>injected header</div>`
web page := `<physique><!-- PLACEHOLDER --></physique>`
tpl.ExecuteTemplate(&pagebuf, web page, nil)
tpl.ExecuteTemplate(&insert, web page, nil)
combined_buf := add_hdr2page()
fmt.Println(combined_buf)
// take a look at
fmt.Println(string(new_page))
}
// add header to each web page
func add_hdr2page() []byte {
placeholder := []byte(`<!-- PLACEHOLDER -->`)
combined_buf := bytes.ReplaceAll(pagebuf.Bytes(), placeholder, hdrbuf.Bytes())
return combined_buf
}
I’m getting a bit misplaced on this code. May you clarify what the varied variables are? Additionally I’m a bit unclear on how ExecuteTemplate works precisely. How do you specify which a part of the pagebuf you wish to insert your html into? I have to append to no matter is already within the HEAD tag.
There may be three elements:
- web page (most important web page)
- placeholder (the place to insert)
- inject (what to insert)
You usually ship the complete rendered template to the “http.ResponseWriter” (your web site)
tpl.ExecuteTemplate(w, web page, nil)
OR preserve it in reminiscence (buffer) till you write the html to the shopper direct as there isn’t a want for re-rendering the mixed html.
w.Write(combined_buf)
However as I stated earlier than there are different methods to do that less complicated utilizing Javascript OR inject by passing a parameter to the template.
{{.}}
ah okay i see what you imply now. The issue is that the HTML that I wish to inject into is NOT a template. It’s an HTML web page that’s coming from a webserver. So there isn’t something in that HTML that claims “insert right here” or something such as you would have in a template. I simply have to append my snippet to the HEAD tag because it involves me from the webserver.
If it’s a HTML web page you possibly can learn it as HTML and insert as HTML IMO…
package deal most important
import (
"bytes"
"fmt"
)
var hdrbuf bytes.Buffer
var pagebuf bytes.Buffer
func most important() {
//get snippet
snippet := `<![CDATA[n<script type="text/javascript">nvar continentCode="EU";nvar doComplianceCheck=true;n</script>n<script src="/another.js"></script>n<script src="path/to/some.js"></script>]]>`
pageHTML := `<head><!-- SNIPPET_HERE --></head>`
// change the INJECT_HERE "placeholder" with the snippet
placeholder := []byte(`<!-- SNIPPET_HERE -->`)
combined_page := bytes.ReplaceAll([]byte(pageHTML), placeholder, []byte(snippet))
fmt.Println(string(combined_page))
// Anticipated output: <physique><div>injected header></div></physique>
}
OR you possibly can take a look at with utilizing <iframe>
or <embed>
relying in your want.
I ended up utilizing goQuery as follows:
bytes, err := io.ReadAll(resp.Physique)
if err != nil {
fmt.Printf("Error producing HTML: %vn", err)
return
}
doc, err := goquery.NewDocumentFromReader(strings.NewReader(string(bytes)))
if err != nil {
fmt.Printf("Error producing HTML: %vn", err)
return
}
doc.Discover("head").AppendHtml("<script sort="textual content/javascript">nvar continentCode="EU";nvar doComplianceCheck=true;n</script>n<script src="/privacyservice.js"></script>n<script src="/path/to/some.js"></script>")
// Print the modified HTML
modifiedHtml, err := doc.Html()
if err != nil {
fmt.Printf("Error producing HTML: %vn", err)
return
}
w.Header().Reset(resp.Header)
w.Header().Del("Content material-Sort")
w.WriteHeader(resp.StatusCode)
fmt.Fprint(w, modifiedHtml)