Skip to content

selector

Type:
<string> | <string[]>

Values: CSS selector
It defines the HTML element you want to pick from the HTML markup over the url:
import createClient from 'microlink.io'

const microlink = createClient()

const github = username =>
  microlink.extract(`https://github.com/${username}`, {
    avatar: {
      selector: 'meta[property="og:image"]:not([content=""])',
      attr: 'content',
      type: 'image'
    }
  })

const username = 'kikobeats'
const { avatar } = await github(username)

console.log(`GitHub avatar for @${username}: ${avatar.url} (${avatar.size_pretty})`)
It's equivalent to Document.querySelector() and any CSS selector can be specified, such as:
  • An HTML tag (e.g., 'img').
  • A CSS class or pseudo-class, id or data-attribute (e.g., '#avatar').
  • A combination of both (e.g., 'img:first').
When selector is omitted, attr operates on the entire page — see whole-page serialization. The same selector is what the markdown, html, text, and links methods accept as an option to scope their extraction.

Fallback selectors

A collection of selectors is an array of fallback rules: the first selector that yields a typed value wins.