AdguardTeam / SafariConverterLib

Swift library that converts AdGuard rules to Safari content blocking rules
GNU General Public License v3.0
46 stars 15 forks source link
adguard content-blocker open-source

Swift Safari Converter

Content Blocker converter swift code

Safari Content Blocker

Converts filter rules in AdGuard format to the format supported by Safari.

How to build:

    swift build

Tests:

    swift test

How to use converter:

    let result: ConversionResult? = ContentBlockerConverter.convertArray(
        rules: [String],
        safariVersion: SafariVersions = .DEFAULT,
        optimize: Bool = false,
        advancedBlocking: Bool = false
        advancedBlockingFormat: AdvancedBlockingFormat = .json
    );

Please note, that safariVersion must be an instance of enum SafariVersions.

The result contains following properties:

How to use converter from command line:

    ConverterTool [--safari-version <safari-version>] [--optimize <optimize>] [--advanced-blocking <advanced-blocking>] [--advanced-blocking-format <advanced-blocking-format>] [<rules>]

Please note that minimal supported Safari version is 13

e.g.

    cat rules.txt | ./ConverterTool --safari-version 13 --optimize false --advanced-blocking true --advanced-blocking-format txt

The tool then reads stdin line by line for rule until an empty line.

How to release on GitHub

Push a new tag in v*.*.* format, then provided github action is intended to build and publish new release with an asset binary.

Supported AdGuard rules types:

Basic content blocker format:

Extended Advanced blocking types:

Third-party dependencies

Use as node module

Requirements:

After installation the build process occurs and binary file will be copied to bin directory

API

jsonFromRules(rules, advancedBlocking, safariVersion, converterToolPath) - method to convert rules into JSON

getConverterVersion - returns Safari Converter Lib version

Limitations

denyallow rules

A rule with the denyallow modifier will be converted into a blocking rule and additional exception rules.

For example: