ExtractSources extracts all JavaScript sources found in the provided HTTP response reader. The optional extractionPoints can be used to overwrite the default extraction points map with a set of HTML tag names, together with a list of what attributes to extract from.
(input io.Reader, extractionPoints ...map[string][]string)
| 31 | // The optional extractionPoints can be used to overwrite the default extraction points map |
| 32 | // with a set of HTML tag names, together with a list of what attributes to extract from. |
| 33 | func ExtractSources(input io.Reader, extractionPoints ...map[string][]string) (<-chan url.URL, error) { |
| 34 | doc, err := goquery.NewDocumentFromReader(input) |
| 35 | if err != nil { |
| 36 | return nil, err |
| 37 | } |
| 38 | |
| 39 | var ( |
| 40 | urls = make(chan url.URL) |
| 41 | points = ExtractionPoints |
| 42 | ) |
| 43 | |
| 44 | if len(extractionPoints) > 0 { |
| 45 | points = extractionPoints[0] |
| 46 | } |
| 47 | |
| 48 | go func() { |
| 49 | defer close(urls) |
| 50 | for tag, attributes := range points { |
| 51 | doc.Find(tag).Each(func(i int, s *goquery.Selection) { |
| 52 | for _, a := range attributes { |
| 53 | if value, exists := s.Attr(a); exists { |
| 54 | u, err := url.Parse(value) |
| 55 | if err != nil { |
| 56 | log.Println(fmt.Errorf("invalid attribute value %s cannot be parsed to a URL: %w", value, err)) |
| 57 | continue |
| 58 | } |
| 59 | |
| 60 | urls <- *u |
| 61 | } |
| 62 | } |
| 63 | }) |
| 64 | } |
| 65 | }() |
| 66 | |
| 67 | return urls, nil |
| 68 | } |
| 69 | |
| 70 | // Filter applies options to filter URLs from the input channel. |
| 71 | func Filter(input <-chan url.URL, options ...func([]url.URL) []url.URL) (<-chan url.URL, error) { |
no outgoing calls
no test coverage detected