Documentation ¶
Overview ¶
Package inject provides script and style injection utilities.
Index ¶
- func Body(html, s string) string
- func Comment(s string) string
- func GoogleAnalytics(trackingID string) string
- func Head(html, s string) string
- func Script(src string) string
- func ScriptInline(s string) string
- func Segment(key string) string
- func Style(href string) string
- func StyleInline(s string) string
- func Var(kind, name string, v interface{}) string
- type Rule
- type Rules
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Body ¶
Body injects a string before the closing body tag.
Example ¶
package main import ( "fmt" "github.com/apex/up/internal/inject" ) var html = `<!doctype html> <html> <head> <meta charset="utf-8"> <title>Example</title> </head> <body> <p>Hello World</p> </body> </html> ` func main() { s := inject.Body(html, inject.Comment("Version 1.0.3")) fmt.Printf("%s\n", s) }
Output: <!doctype html> <html> <head> <meta charset="utf-8"> <title>Example</title> </head> <body> <p>Hello World</p> <!-- Version 1.0.3 --> </body> </html>
func Comment ¶
Comment returns an html comment.
Example ¶
package main import ( "fmt" "github.com/apex/up/internal/inject" ) func main() { fmt.Printf("%s\n", inject.Comment(`Hello World`)) }
Output: <!-- Hello World -->
func GoogleAnalytics ¶
GoogleAnalytics inline script with tracking key.
Example ¶
package main import ( "fmt" "github.com/apex/up/internal/inject" ) func main() { fmt.Printf("%s\n", inject.GoogleAnalytics(`KEY HERE`)) }
Output: <script> (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){ (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o), m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m) })(window,document,'script','https://www.google-analytics.com/analytics.js','ga'); ga('create', 'KEY HERE', 'auto'); ga('send', 'pageview'); </script>
func Head ¶
Head injects a string before the closing head tag.
Example ¶
package main import ( "fmt" "github.com/apex/up/internal/inject" ) var html = `<!doctype html> <html> <head> <meta charset="utf-8"> <title>Example</title> </head> <body> <p>Hello World</p> </body> </html> ` func main() { s := inject.Head(html, `<link rel="stylesheet" href="/style.css">`) fmt.Printf("%s\n", s) }
Output: <!doctype html> <html> <head> <meta charset="utf-8"> <title>Example</title> <link rel="stylesheet" href="/style.css"> </head> <body> <p>Hello World</p> </body> </html>
func Script ¶
Script returns an script.
Example ¶
package main import ( "fmt" "github.com/apex/up/internal/inject" ) func main() { fmt.Printf("%s\n", inject.Script(`/sloth.js`)) }
Output: <script src="/sloth.js"></script>
func ScriptInline ¶
ScriptInline returns an inline script.
Example ¶
package main import ( "fmt" "github.com/apex/up/internal/inject" ) func main() { fmt.Printf("%s\n", inject.ScriptInline(`const user = { "name": "Tobi" }`)) }
Output: <script>const user = { "name": "Tobi" }</script>
func Segment ¶
Segment inline script with key.
Example ¶
package main import ( "fmt" "github.com/apex/up/internal/inject" ) func main() { fmt.Printf("%s\n", inject.Segment(`KEY HERE`)) }
Output: <script> !function(){var analytics=window.analytics=window.analytics||[];if(!analytics.initialize)if(analytics.invoked)window.console&&console.error&&console.error("Segment snippet included twice.");else{analytics.invoked=!0;analytics.methods=["trackSubmit","trackClick","trackLink","trackForm","pageview","identify","reset","group","track","ready","alias","debug","page","once","off","on"];analytics.factory=function(t){return function(){var e=Array.prototype.slice.call(arguments);e.unshift(t);analytics.push(e);return analytics}};for(var t=0;t<analytics.methods.length;t++){var e=analytics.methods[t];analytics[e]=analytics.factory(e)}analytics.load=function(t){var e=document.createElement("script");e.type="text/javascript";e.async=!0;e.src=("https:"===document.location.protocol?"https://":"http://")+"cdn.segment.com/analytics.js/v1/"+t+"/analytics.min.js";var n=document.getElementsByTagName("script")[0];n.parentNode.insertBefore(e,n)};analytics.SNIPPET_VERSION="4.0.0"; analytics.load("KEY HERE"); analytics.page(); }}(); </script>
func Style ¶
Style returns an style.
Example ¶
package main import ( "fmt" "github.com/apex/up/internal/inject" ) func main() { fmt.Printf("%s\n", inject.Style(`/sloth.css`)) }
Output: <link rel="stylesheet" href="/sloth.css">
func StyleInline ¶
StyleInline returns an inline style.
Example ¶
package main import ( "fmt" "github.com/apex/up/internal/inject" ) func main() { fmt.Printf("%s\n", inject.StyleInline(`body { display: none }`)) }
Output: <style>body { display: none }</style>
Types ¶
type Rule ¶
type Rule struct { // Type of injection, defaults to "literal" unless File is used, // or Value contains .js or .css extensions. Type string `json:"type"` // Value is the literal, inline string, or src/href of the injected tag. Value string `json:"value"` // File is used to load source from disk instead of providing Value. Note // that if Type is not explicitly provided, then it will default to // "inline script" or "inline style" for .js and .css files respectively. File string `json:"file"` }
Rule is an injection rule.
Click to show internal directories.
Click to hide internal directories.