MCPcopy Index your code
hub / github.com/003random/getJS / ExtractSources

Function ExtractSources

extractor/extractor.go:33–68  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

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.
33func 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.
71func Filter(input <-chan url.URL, options ...func([]url.URL) []url.URL) (<-chan url.URL, error) {

Callers 2

ProcessURLsMethod · 0.92
ProcessResponseMethod · 0.92

Calls

no outgoing calls

Tested by

no test coverage detected