Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/andrioid/ublproxy. andrioid.github.io/ublproxy
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127package main
import ( "bytes" "strings"
"golang.org/x/net/html")
// htmlTokens holds the classes and IDs found in an HTML document.// Used to pre-filter CSS selectors before injection — only selectors// that could match something in the document are injected.type htmlTokens struct { classes map[string]bool ids map[string]bool}
// extractHTMLTokens does a fast pass over the HTML to collect all class// names and id attribute values. It uses the tokenizer (not a full DOM// parse) so it's O(n) in document size with minimal allocation.func extractHTMLTokens(src []byte) htmlTokens { tokens := htmlTokens{ classes: make(map[string]bool), ids: make(map[string]bool), }
tokenizer := html.NewTokenizer(bytes.NewReader(src)) for { tt := tokenizer.Next() if tt == html.ErrorToken { break } if tt != html.StartTagToken && tt != html.SelfClosingTagToken { continue }
for { key, val, more := tokenizer.TagAttr() k := string(key) if k == "class" { for _, c := range strings.Fields(string(val)) { tokens.classes[c] = true } } else if k == "id" { v := strings.TrimSpace(string(val)) if v != "" { tokens.ids[v] = true } } if !more { break } } }
return tokens}
// selectorMatchesTokens returns true if the CSS selector could match// something in the document based on the extracted tokens.//// For simple selectors (.class, #id, .a.b compound), it checks whether// the referenced classes/IDs exist in the HTML. For complex selectors// (combinators, attribute selectors, pseudo-classes, tag-only), it// returns true unconditionally since we can't pre-evaluate them without// a full DOM and CSS selector engine.func selectorMatchesTokens(selector string, tokens htmlTokens) bool { // Complex selectors that we can't pre-filter — always include. // Combinators: space (descendant), > (child), + (adjacent), ~ (general sibling) if strings.ContainsAny(selector, " >+~") { return true } // Attribute selectors if strings.ContainsAny(selector, "[]") { return true } // Pseudo-classes/elements if strings.Contains(selector, ":") { return true }
// Strip leading tag name (e.g. "div.foo" -> ".foo", "aside#bar" -> "#bar") // Tags don't have . or # so find the first class/id marker. rest := selector if dotIdx := strings.IndexByte(rest, '.'); dotIdx >= 0 { rest = rest[dotIdx:] } else if hashIdx := strings.IndexByte(rest, '#'); hashIdx >= 0 { rest = rest[hashIdx:] } else { // Tag-only selector (e.g. "aside") — always include return true }
// ID selector: #foo or tag#foo if rest[0] == '#' { id := rest[1:] return tokens.ids[id] }
// Class selector(s): .foo or .foo.bar.baz // Split on '.' — first element is empty (before the leading dot). parts := strings.Split(rest, ".") for _, part := range parts { if part == "" { continue } if !tokens.classes[part] { return false } } return true}
// filterSelectors returns only the selectors that could match something// in the given HTML document. This avoids injecting tens of thousands of// CSS selectors that don't match any element on the page.func filterSelectors(selectors []string, htmlBody []byte) []string { tokens := extractHTMLTokens(htmlBody) var matched []string for _, sel := range selectors { if selectorMatchesTokens(sel, tokens) { matched = append(matched, sel) } } return matched}