inject

package
v1.7.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 27, 2021 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

Package inject provides script and style injection utilities.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Body

func Body(html, s string) string

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

func Comment(s string) string

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

func GoogleAnalytics(trackingID string) string

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(html, s string) string

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

func Script(src string) string

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

func ScriptInline(s string) string

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

func Segment(key string) string

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

func Style(href string) string

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

func StyleInline(s string) string

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>

func Var

func Var(kind, name string, v interface{}) string

Var injection.

Example
package main

import (
	"fmt"

	"github.com/apex/up/internal/inject"
)

func main() {
	user := map[string]string{
		"name": "Tobi",
	}

	fmt.Printf("%s\n", inject.Var("const", "user", user))
}
Output:

<script>const user = {"name":"Tobi"}</script>

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.

func (*Rule) Apply

func (r *Rule) Apply(html string) string

Apply rule to html.

func (*Rule) Default

func (r *Rule) Default() error

Default applies defaults.

func (*Rule) Validate

func (r *Rule) Validate() error

Validate returns an error if incorrect.

type Rules

type Rules map[string][]*Rule

Rules is a set of rules mapped by location.

func (Rules) Apply

func (r Rules) Apply(html string) string

Apply rules to html.

func (Rules) Default

func (r Rules) Default() error

Default rules.

func (Rules) Validate

func (r Rules) Validate() error

Validate rules.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL