Skip to content

bringmetheaugust/goDOM

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

38 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

goDOM

goDOM logo

Easy, yeah?

Doc reference Golang version project version

Made by front-ender for front-enders.
Package provide method to parse HTML and get browser-like DOM and DOM API.
It also has jQuery-like API.

⚠️It's only for reading DOM, searching elements and getting their data. Package doesn't have methods to mutate DOM.
⚠️Before using it You should remember that sites can detect You as a bot and return unexpected HTML response.

Installation

go get github.com/bringmetheaugust/goDOM

Examples

Using DOM-like API

package motherfckrs

import "github.com/bringmetheaugust/goDOM"

func main() {
    // First, we'll get document
    bytes :=                                // HTML markup as bytes (from HTTP request, files, etc.)
    document, _, err := goDom.Create(bytes) // create document (DOM with DOM API, like in browser)
    if err != nil {return}                  // also check if markup is valid

    // Want to find some element by `id`?
    el, err := document.GetElementById("lol") // <a id="lol" class="pipi" href="http://lol.com">
    if err != nil {return}                    // check if element exists
    print(el.ClassList)                       // ["pipi"]
    print(el.Attributes)                      // {"id": "lol", class: "pipi", "href": "http://lol.com"}
    attr, _ := el.GetAttribute("href")        // "http://lol.com"

    // Or get a lot of elements by query selector?
    elements, err := document.QuerySelectorAll(".weee") // all elements in DOM which have class "weee"
    if err != nil {return}                              // check if elements are existed
    for _, el := range elements {                       // loop slice with existed elements
        // your best code here
    }

Using jQuery-like API

package motherfckrs

import "github.com/bringmetheaugust/goDOM"

func main() {
    // First, we'll get document
    bytes :=                                // HTML markup as bytes (from HTTP request, files, etc.)
    _, jQ, err := goDom.Create(bytes)       // create jQ (jQuery with jQUery-like API)
    if err != nil {return}                  // also check if markup is valid

    // Want to find some element by `id` ?
    attr, err  := jQ("#lol").Attr("href")   // "http://lol.com" from <a id="lol" class="pipi" href="http://lol.com">

    // Or get `data-lol` attributes from elements with class `.wee` which have inside itself links with class `.piu`?
    jQ(".wee").Has("a.piu").Each(func(q) {
        a, _ := q.Attr("data-lol")
    })

More real examples here.

Docs

Document

Element

jQuery

Something about jQuery API

This package uses jQuery API as the origin jQuery library (using JavaScript).
For example in Golang with best practice, we should return data and/or errors almost from each function. How it should look like:

    a, err := jQ(".li") // get elements with `li` classes
    if err != nil { return } // check if elements exist
    b, err := a.Has("a.my-link") // filter if they have links with class `my-link` inside itself
    if err != nil { return } // check if elements exist
    c, err := b.Find("div[data-lol=lala]") // find `div` elements with attribute `data-lol=lala` inside 
    if err != nil {return} // check if elements exist
    c.Each(func (q) { print(q) })

As You know, this package provides the original jQuery API, so every method (except Attr) always returns another jQuery element, even if elements are not found and a slice of elements is empty. So we can use jQuery as in origin jQuery library like this:

    jQ(".li").Has("a.my-link").Find("div[data-lol=lala]").Each(func (q) { print(q) })

Development

via Makefile

make install

without Makefile

sh ./scripts/install