Documentation ¶
Overview ¶
Package favicon finds icons for websites. It can find icons in HTML (favicons in <link> elements, Open Graph or Twitter images) and in JSON manifests, or check common paths on the server (e.g. /favicon.ico).
Package-level functions call the corresponding methods on a default Finder. For customised Finder behaviour, pass appropriate options to New().
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var IconNames = []string{
"favicon.ico",
"apple-touch-icon.png",
}
IconNames are common names of icon files hosted in server roots.
var UserAgent = "go-favicon/0.1"
UserAgent is sent in the User-Agent HTTP header.
Functions ¶
This section is empty.
Types ¶
type Filter ¶
Filter accepts/rejects/modifies Icons. If if returns nil, the Icon is ignored. Set a Finder's filters by passing WithFilter(...) to New().
type Finder ¶
type Finder struct {
// contains filtered or unexported fields
}
Finder discovers favicons for a URL. By default, a Finder looks in the following places:
- The HTML page at the given URL for...
- icons in <link> tags
- Open Graph images
- Twitter images
- The manifest file...
- defined in the HTML page -- or --
- /manifest.json
- Standard favicon paths
- /favicon.ico
- /apple-touch-icon.png
Pass the IgnoreManifest and/or IgnoreWellKnown Options to New() to reduce the number of requests made to webservers.
func New ¶
New creates a new Finder configured with the given options.
Example ¶
Find favicons using default options.
// Find icons defined in HTML, the manifest file and at default locations icons, err := favicon.Find("https://www.deanishe.net") if err != nil { panic(err) } // icons are sorted widest first for _, i := range icons { fmt.Printf("%s\n", i.FileExt) }
Output: 256x256 png 192x192 png 180x180 png 32x32 png 16x16 png 0x0 png 0x0 ico
Example (WithOptions) ¶
Find favicons using custom options. Passing IgnoreManifest and IgnoreWellKnown causes the Finder to only retrieve the initial URL (HTML page).
f := favicon.New( // Don't look for or parse a manifest.json file favicon.IgnoreManifest, // Don't request files like /favicon.ico to see if they exist favicon.IgnoreWellKnown, ) // Find icons defined in HTML, the manifest file and at default locations icons, err := f.Find("https://www.deanishe.net") if err != nil { panic(err) } // icons are sorted widest first for _, i := range icons { fmt.Printf("%s\n", i.MimeType) }
Output: 180x180 image/png 32x32 image/png 16x16 image/png
type Icon ¶
type Icon struct { URL string `json:"url"` // Never empty MimeType string `json:"mimetype"` // MIME type of icon; never empty FileExt string `json:"extension"` // File extension; may be empty }
Icon is a favicon parsed from an HTML file or JSON manifest.
TODO: Use *Icon everywhere to be consistent with higher-level APIs that return nil for "not found".
func FindReader ¶
FindReader finds a favicon in HTML. It accepts an optional base URL, which is used to resolve relative links.
type Manifest ¶
type Manifest struct {
Icons []ManifestIcon `json:"icons"`
}
Manifest is the relevant parts of a manifest.json file.
type ManifestIcon ¶
type ManifestIcon struct { URL string `json:"src"` Type string `json:"type"` RawSizes string `json:"sizes"` }
ManifestIcon is an icon from a manifest.json file.
type Option ¶
type Option func(*Finder)
Option configures Finder. Pass Options to New().
var ( // IgnoreWellKnown ignores common locations like /favicon.ico. IgnoreWellKnown Option = func(f *Finder) { f.ignoreWellKnown = true } // IgnoreManifest ignores manifest.json files. IgnoreManifest Option = func(f *Finder) { f.ignoreManifest = true } // OnlyPNG ignores non-PNG files. OnlyPNG Option = OnlyMimeType("image/png") // OnlyICO ignores non-ICO files. OnlyICO Option = WithFilter(func(icon *Icon) *Icon { if icon.MimeType == "image/x-icon" || icon.MimeType == "image/vnd.microsoft.icon" { return icon } return nil }) )
func OnlyMimeType ¶
OnlyMimeType only finds Icons that have one of the specified MIME types, e.g. "image/png" or "image/jpeg".
func WithClient ¶
WithClient configures Finder to use the given HTTP client.
func WithFilter ¶
WithFilter only returns Icons accepted by Filter functions.