How to write adblock rule for xbrowser

Brief introduction

XBrowser’s rule is not completely compatible with ABP rule writing. You know,ABP was born in PC era, so we did not fully implement the Ad blocking rules of ABP because of the performance of mobile devices, but provided simple and efficient Ad blocking strategy and rule writing, basically taking into account both performance and blocking effect.

After testing, XBrowser can execute more than 30,000 records Ad-blocking rules while browsing the page without significant impact on page rendering.

Most of the ABP rules can be imported into XBrowser by providing automatic rule importing tools in XBrowser. However, it is not recommended to import ABP official complete rules directly, because most of the ABP official rules are for PC browsers. Loading too many rules will only lead to unnecessary performance consumption.

Quick example

1
www.example.com
Blocking AD host whose domain is "www.example.com"
1
/content/ads
Blocking all request whose directory part of url start with '/content/ads'
1
/runtime/js/index960.js
Blocking request whose script's path is '/runtime/js/index960.js'
1
###ad-container
Remove page elements whose ID is "ad-container"
1
##.ad-container
Remove page elements whose class name is "ad-container"
1
##div[title*="adv"]
Remove all elements which property value contain the string "adv".

More instructions and examples

Like ABP, XBrowser provide two different kinds of ad-blocking strategy.“Interception strategy” and “Element hiding strategy”You can choose appropriate blocking strategy for different types of ads.

Interception strategy

The interception strategy blocks AD by matching the URL of the advertising website before the network request, which can effectively be blocking advertising resources loaded from outside, and the performance is also the best.

“Interception strategy” provides two matching methods, one is based on domain name and directory matching and the other is based on Regular expression matching. From the performance point of view, you should first use domain name and directory matching to write rules. Regular expression matching is complementary.

Blocking by domain and directory
1
example.com
This rule will block all ad resource URL with the top-level domain name example.com, such as www.example.com, abc.example.com, etc, will be blocked.
1
/content/ads
Block any ad resource url whose path start with "/content/ads".exp:http://www.example.com/content/ads/ad.js","http://ad.example.com/content/ads/player/third/ad.js" etc
1
example.com/content/ads
Blocking all ad resource url whose top domain is example.com and path start with "/content/ads"
1
2
www.example.com/content/ads/ad.js
content/ads/ad.js
Blocking ad resource url whose domain or directory can match with above rule
Blocking by regular expression

For users who know regular expressions, they can achieve more flexible pattern matching through regular expressions.

Convention: regular expression rules start with “–”.In other words, “–” is followed by the regular expression you want to write, as shown in the example below.

1
--ads
Blocking any URL that contains the string "ads"
1
--\.swf$
Blocking any URL whose suffix is ".swf"
1
--ad(\d{1,2})?\.example\.com
Blocking all URL whose subdomain is "ad" + digital. for example ad.example.com,ad1.example.com, ad10.example.com
1
--ad\w*\.js$
Blocking all java script whose name contains the string "ad". For example:"http://www.example.com/content/sina_ad_wap.js"

Element hiding strategy

For some ads that are not loaded by external resources and scripts. In other words, we can’t block it by interception strategy.

At this point, you can remove advertising elements from the page by “Element hiding” rule.

The element hiding rule same as ABP Element hiding rule which starts with “##” and followed by a CSS selector expression. Here are some examples of hidden element rules.

1
2
###mybanner
`
Remove the page element whose id is "mybanner"
1
##.ad-container
Remove the page elements whose class name is "ad-container"
1
##div[title*="adv"]
Remove all elements which property value contain the string "adv"

Like the hidden element rule of ABP, the scope of the Hiding element rule can be controlled by adding a domain name prefix, as shown in the following example.

1
example.com###mybanner
Removes an element whose id is "mybanner" and the rule takes effect only under the example.com

Rule scope

By default, the blocking rules you write will take effect on all sites except whitelists. Sometimes we can limit the scope of the rules in order to avoid mistakenly killing other sites. these rules will only take effect under certain domain names.

To set the scope of a rule, simply add “@” after the original rules and followed by the domain name we need to qualify. Below are valid rules with a scope.

1
2
3
ad.xxx.com@example.com
ad.xxx.com/content/ads@example.com
###mybanner@example.com

In order to be compatible with the writing of ABP’s element hiding rule scopes, the following two methods are equivalent.

1
2
###mybanner@example.com
example.com###mybanner

Test your rules

In addition, we have provided an ad-blocking test case page, which you can use to test whether your ad-blocking strategy works properly.