Skip to content

Commit

Permalink
part of an attempt
Browse files Browse the repository at this point in the history
  • Loading branch information
hanubeki committed Aug 21, 2024
1 parent 60cfe58 commit b1ab649
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 7 deletions.
15 changes: 10 additions & 5 deletions generate/cosmetic/filter/combine.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package filter

type CombineResult struct {
Domains []string
Selectors []string
Exceptions []string
InjectedCSS []string
Domains []string
Selectors []string
Exceptions []string
InjectedCSS []string
InjectException []string
}

func Combine(filters []Rule) (m map[string]CombineResult) {
Expand All @@ -24,7 +25,11 @@ func Combine(filters []Rule) (m map[string]CombineResult) {
}
}
if f.InjectedCSS != "" && !contains(out.InjectedCSS, f.InjectedCSS) {
out.InjectedCSS = append(out.InjectedCSS, f.InjectedCSS)
if f.isException {
out.InjectException = append(out.InjectedCSS, f.InjectedCSS)
} else {
out.InjectedCSS = append(out.InjectedCSS, f.InjectedCSS)
}
}

m[d] = out
Expand Down
7 changes: 6 additions & 1 deletion generate/cosmetic/filter/filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,11 @@ func ParseLine(line string) (f Rule, ok bool) {
// Example: domain1.com,domain2.com#$#.cookie { display: none!important; }
isCSSInjection = true
isCSSException = false
} else if split = strings.SplitN(line, "#@$#", 2); len(split) == 2 {
// A CSS injection
// Example: domain1.com,domain2.com#$#.cookie { display: none!important; }
isCSSInjection = true
isCSSException = true
} else {
// The statement in this line is not recognized, ignore it
return f, false
Expand Down Expand Up @@ -112,6 +117,6 @@ func ParseLine(line string) (f Rule, ok bool) {
JoinedDomains: joinedDomains,
CSSSelector: selector,
isException: isCSSException,
InjectedCSS: injectedStyle,
InjectedCSS: injectedStyle
}, true
}
25 changes: 25 additions & 0 deletions generate/cosmetic/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"log"
"os"
"sort"
"regexp"
"strings"
"text/template"
"time"
Expand All @@ -22,6 +23,17 @@ func joinSorted(f []string, comma string) string {
return strings.Join(f, comma)
}

func joinSortedMeta(f []string, comma string) string {
out := ""
tmp := make([]string, len(f))
copy(tmp, f)
sort.Strings(tmp)
for k, v := range tmp {
tmp[k] = regexp.QuoteMeta(v)
}
return strings.Join(tmp, comma)
}

func toJSObject(x interface{}) string {
b, err := json.Marshal(x)
if err != nil {
Expand Down Expand Up @@ -124,6 +136,8 @@ func main() {
duplicateCount[joined] = duplicateCount[joined] + 1
joined = joinSorted(f.InjectedCSS, "")
duplicateCount[joined] = duplicateCount[joined] + 1
joined = "(" + joinSortedMeta(f.InjectException, "|") + ")"
duplicateCount[joined] = duplicateCount[joined] + 1
}

var deduplicatedStrings []string
Expand All @@ -146,6 +160,7 @@ func main() {
compiledSelectorRules = map[string]interface{}{}
compiledSelectorExceptions = map[string]interface{}{}
compiledInjectionRules = map[string]interface{}{}
compiledInjectionExceptions = map[string]interface{}{}
)
for domain, filter := range lookupTable {
if len(filter.Selectors) > 0 {
Expand Down Expand Up @@ -174,6 +189,15 @@ func main() {
compiledInjectionRules[domain] = joined
}
}

if len(filter.InjectException) > 0 {
joined := "(" + joinSortedMeta(filter.InjectException, "|") + ")"
if duplicateCount[joined] > 1 {
compiledInjectionExceptions[domain] = deduplicatedIndexMapping[joined]
} else {
compiledInjectionExceptions[domain] = joined
}
}
}

fmt.Printf("Combined them for %d domains\n", len(compiledSelectorRules))
Expand All @@ -194,6 +218,7 @@ func main() {
"rules": toJSObject(compiledSelectorRules),
"exceptions": toJSObject(compiledSelectorExceptions),
"injectionRules": toJSObject(compiledInjectionRules),
"injectionExceptions": toJSObject(compiledInjectionExceptions),
"deduplicatedStrings": toJSObject(deduplicatedStrings),
"statistics": fmt.Sprintf("blockers for %d domains, exceptions for %d domains, injected CSS rules for %d domains", len(compiledSelectorRules), len(compiledSelectorExceptions), len(compiledInjectionRules)),
"isLite": topDomains != nil,
Expand Down
3 changes: 2 additions & 1 deletion generate/cosmetic/script-template.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,13 @@

let deduplicatedStrings = {{.deduplicatedStrings }};
let injectionRules = {{.injectionRules }};
let injectionExceptions = {{.injectionExceptions }};
let rules = {{.rules }};
let exceptions = {{.exceptions }};
let defaultRules = rules[""];
let defaultExceptions = exceptions[""];
let defaultInjections = injectionRules[""];

let defaultInjectionExceptions = injectionExceptions[""];

function findRules(rules, host) {
let domainSplit = host.split(".");
Expand Down

0 comments on commit b1ab649

Please sign in to comment.