1# Using the Ad Blocking Feature 2 3ArkWeb provides the ad blocking feature for applications, supporting default EasyList rules pushed by the cloud and custom rule files set by applications through APIs. It can intercept the advertisement resource download at the network layer or inject CSS rules into the web page to hide specific advertisement elements. 4 5The current configuration file uses EasyList. 6 7## Common EasyList Rules 8 9| Type | Description | Example| 10| ---------- | ------ | ---- | 11| URL blocking rule| Blocks all sub-resource requests whose URLs match **example.com/js/*_tv.js**. This rule is used to block a specified domain name and all its subdomain names.| \|\|example.com/js/*_tv.js | 12| URL blocking rule| Blocks third-party resources whose URLs match **"alimama.cn"** from websites whose domain names are neither **alimama.com** nor **taobao.com**. **\$third\_party** is an options syntax used to match third-party resources. A tilde (~) before a domain name indicates that the domain name is not included.| \|\|alimama.cn^$third-party,domain\=~alimama.com\|\~taobao.com | 13| Exception rule| Disables ad blocking on **example.com**. **@@** is the syntax keyword that indicates no blocking.| \@\@\|\|example.com^$document | 14| Exception rule| Disables the blocking of subresources that match **".adserver"** in the web page whose domain name is **litv.tv**.| \@\@.adserver.$domain=litv.tv | 15| Element hiding rule| Hides all elements whose class is **i528** in the web page whose domain name is **myabandonware.com** or **myware.com**. **##** is used to hide an element.| myabandonware.com, myware.com##.i528 | 16| Element hiding exception rule| Disables the hiding of elements whose IDs are **ad_1** in **sdf-event.sakura.ne.jp**.| sdf-event.sakura.ne.jp#@##ad_1 | 17 18Exception rules are usually used together with common rules. In some specific scenarios, common rules are not applicable, and exception rules do not have effects. 19For example, if you configure a blocking rule **||abc.com/js/123.js** to block websites, but some websites are blocked by mistake or cannot be blocked, you can configure exception rules for these websites. 20 21## Constraints 22 23- In the [WebviewController](../reference/apis-arkweb/arkts-apis-webview-WebviewController.md) class, the [enableAdsBlock()](../reference/apis-arkweb/arkts-apis-webview-WebviewController.md#enableadsblock12) API is added for enabling or disabling the ad blocking feature at the web instance level. 24 25- The [AdsBlockManager](../reference/apis-arkweb/arkts-apis-webview-AdsBlockManager.md) global singleton class is added to provide the capabilities of customizing ad blocking configurations and controlling website-level features. 26 27- The web instance provides [onAdsBlocked()](../reference/apis-arkweb/arkts-basic-components-web-attributes.md#onadsblocked12) callback to notify the upper-layer application of the interception information. 28 29- Only one custom configuration can be set by the [setAdsBlockRules()](../reference/apis-arkweb/arkts-apis-webview-AdsBlockManager.md#setadsblockrules12) API of [AdsBlockManager](../reference/apis-arkweb/arkts-apis-webview-AdsBlockManager.md). The configuration is persistent and does not need to be reconfigured during cold start of the application. This prevents the compilation and parsing of the ad blocking rule from being triggered again. 30 31- The data operated by the [addAdsBlockDisallowedList()](../reference/apis-arkweb/arkts-apis-webview-AdsBlockManager.md#addadsblockdisallowedlist12), [removeAdsBlockDisallowedList()](../reference/apis-arkweb/arkts-apis-webview-AdsBlockManager.md#removeadsblockdisallowedlist12), [clearAdsBlockDisallowedList()](../reference/apis-arkweb/arkts-apis-webview-AdsBlockManager.md#clearadsblockdisallowedlist12), [addAdsBlockAllowedList()](../reference/apis-arkweb/arkts-apis-webview-AdsBlockManager.md#addadsblockallowedlist12), [removeAdsBlockAllowedList()](../reference/apis-arkweb/arkts-apis-webview-AdsBlockManager.md#removeadsblockallowedlist12) and [clearAdsBlockAllowedList()](../reference/apis-arkweb/arkts-apis-webview-AdsBlockManager.md#clearadsblockallowedlist12) APIs of the [AdsBlockManager](../reference/apis-arkweb/arkts-apis-webview-AdsBlockManager.md) API is not persistent. The data needs to be reset during cold start of the application. 32 33- If the ad blocking feature is enabled for a web instance but the [addAdsBlockDisallowedList()](../reference/apis-arkweb/arkts-apis-webview-AdsBlockManager.md#addadsblockdisallowedlist12), [removeAdsBlockDisallowedList()](../reference/apis-arkweb/arkts-apis-webview-AdsBlockManager.md#removeadsblockdisallowedlist12), [clearAdsBlockDisallowedList()](../reference/apis-arkweb/arkts-apis-webview-AdsBlockManager.md#clearadsblockdisallowedlist12), [addAdsBlockAllowedList()](../reference/apis-arkweb/arkts-apis-webview-AdsBlockManager.md#addadsblockallowedlist12), [removeAdsBlockAllowedList()](../reference/apis-arkweb/arkts-apis-webview-AdsBlockManager.md#removeadsblockallowedlist12), and [clearAdsBlockAllowedList()](../reference/apis-arkweb/arkts-apis-webview-AdsBlockManager.md#clearadsblockallowedlist12) APIs of the [AdsBlockManager](../reference/apis-arkweb/arkts-apis-webview-AdsBlockManager.md) API are not called to set disallowlist and allowlist, the ad blocking feature is enabled for all websites. 34 35- When both allowlist and disallowlist are used, allowlist has a higher priority than disallowlist. That is, allowlist is used for matching first. If the matching is successful, disallowlist is not used, and the ad blocking feature is enabled for the website. 36 37- If the ad blocking feature is not enabled for the application, the **Web** component does not request the default built-in EasyList file from the server. 38 39- The disallowlist and allowlist use suffix matching. For example, if the domain name of an application is **xxyy.com**, the website whose URL is **wwsstt.xxyy.com** can be matched. 40 41## Application Scenarios 42 43### Enabling Ad Blocking 44You can use [setAdsBlockRules()](../reference/apis-arkweb/arkts-apis-webview-AdsBlockManager.md#setadsblockrules12) provided by **AdsBlockManager** to set blocking rules in EasyList and use [enableAdsBlock()](../reference/apis-arkweb/arkts-apis-webview-WebviewController.md#enableadsblock12) of the **Web** component to enable the ad blocking feature. 45 46The following example shows how to select the EasyList file using the file picker and how to enable the ad blocking feature in an application. 47 48```ts 49// xxx.ets 50import { webview } from '@kit.ArkWeb'; 51import { picker, fileUri } from '@kit.CoreFileKit'; 52 53// This example demonstrates how to click a button to open the EasyList using file picker and set the file in the Web component. 54@Entry 55@Component 56struct WebComponent { 57 main_url: string = 'https://www.example.com'; 58 controller: webview.WebviewController = new webview.WebviewController(); 59 60 @State input_text: string = 'https://www.example.com'; 61 62 build() { 63 Column() { 64 Row() { 65 Flex() { 66 Button({type: ButtonType.Capsule}) { 67 Text("setAdsBlockRules") 68 } 69 .onClick(() => { 70 try { 71 let documentSelectionOptions: ESObject = new picker.DocumentSelectOptions(); 72 let documentPicker: ESObject = new picker.DocumentViewPicker(); 73 documentPicker.select(documentSelectionOptions).then((documentSelectResult: ESObject) => { 74 if (documentSelectResult && documentSelectResult.length > 0) { 75 let fileRealPath = new fileUri.FileUri(documentSelectResult[0]); 76 console.info('DocumentViewPicker.select successfully, uri: ' + fileRealPath); 77 webview.AdsBlockManager.setAdsBlockRules(fileRealPath.path, true); 78 } 79 }) 80 } catch (err) { 81 console.error('DocumentViewPicker.select failed with err:' + err); 82 } 83 }) 84 } 85 } 86 Web({ src: this.main_url, controller: this.controller }) 87 .onControllerAttached(()=>{ 88 this.controller.enableAdsBlock(true); 89 }) 90 } 91 } 92} 93``` 94 95When the component has a built-in EasyList, you can set the **replace** parameter of the [setAdsBlockRules()](../reference/apis-arkweb/arkts-apis-webview-AdsBlockManager.md#setadsblockrules12) API to disable the built-in EasyList. When the **replace** parameter is set to **false**, the built-in and custom rules work at the same time. If the built-in rules conflict with the custom rules, set the **replace** parameter to **true**. 96 97The custom rule file is a global configuration file for the application that it takes effect for all **Web** components in the application processes. In addition, the file is persistent that it continues to work when the application is restarted. 98 99### Disabling Ad Blocking on Pages with Specific Domain Names 100When the ad blocking feature of a **Web** component is enabled, in addition to the custom EasyList, you can also use [addAdsBlockDisallowedList()](../reference/apis-arkweb/arkts-apis-webview-AdsBlockManager.md#addadsblockdisallowedlist12) provided by **AdsBlockManager** to disable the ad blocking feature. 101 102```ts 103// xxx.ets 104import { webview } from '@kit.ArkWeb'; 105 106// This example demonstrates how to click a button to add an array of domain names to the disallowed list. 107@Entry 108@Component 109struct WebComponent { 110 main_url: string = 'https://www.example.com'; 111 text_input_controller: TextInputController = new TextInputController(); 112 controller: webview.WebviewController = new webview.WebviewController(); 113 114 @State input_text: string = 'https://www.example.com'; 115 116 build() { 117 Column() { 118 Row() { 119 Flex() { 120 TextInput({ text: this.input_text, placeholder: this.main_url, controller: this.text_input_controller}) 121 .id("input_url") 122 .height(40) 123 .margin(5) 124 .borderColor(Color.Blue) 125 .onChange((value: string) => { 126 this.input_text = value; 127 }) 128 129 Button({type: ButtonType.Capsule}) { Text("Go") } 130 .onClick(() => { 131 this.controller.loadUrl(this.input_text); 132 }) 133 134 Button({type: ButtonType.Capsule}) { Text("addAdsBlockDisallowedList") } 135 .onClick(() => { 136 let arrDomainSuffixes = new Array<string>(); 137 arrDomainSuffixes.push('example.com'); 138 arrDomainSuffixes.push('abcdefg.cn'); 139 webview.AdsBlockManager.addAdsBlockDisallowedList(arrDomainSuffixes); 140 }) 141 } 142 } 143 Web({ src: this.main_url, controller: this.controller }) 144 .onControllerAttached(()=>{ 145 this.controller.enableAdsBlock(true); 146 }) 147 } 148 } 149} 150``` 151 152Add the domain name to **DisallowedList** of **AdsBlockManager** through [addAdsBlockDisallowedList()](../reference/apis-arkweb/arkts-apis-webview-AdsBlockManager.md#addadsblockdisallowedlist12). When the page is loaded, the system matches the suffix of the web page URL with the domain name in **DisallowedList**. If the matching is successful, the system does not block ads on the page. In addition, [addAdsBlockAllowedList()](../reference/apis-arkweb/arkts-apis-webview-AdsBlockManager.md#addadsblockallowedlist12) is provided to be used with **DisallowedList()** to set domain names and determine whether to enable ad blocking. 153 154**AdsBlockManager** caches two lists of domain names, including **DisallowedList** and **AllowList**. **DisallowedList** is used to disable ad blocking on web pages, and **AllowList** is used to enable ad blocking disabled by **DisallowedList**. **AllowList** has a higher priority. When a web page is loaded, the system matches the web page URL with **AllowList**. If the matching is successful, the ad blocking feature is enabled. Otherwise, the system continues to match the web page URL with **DisallowedList**. If the matching is successful, the ad blocking feature is disabled. If the accessed web page is neither list in **AllowList** nor in **DisallowedList**, the ad blocking feature for this web page is enabled by default. 155 156For example, if you want to enable ad blocking for** news.example.com** and **sport.example.com** in an application, but not for other web pages under the **example.com** domain, you can use **addAdsBlockDisallowedList()** to add **example.com** to **DisallowedList**, and then use **addAdsBlockAllowedList()** to add **news.example.com** and **sport.example.com** to **AllowedList**. 157 158```ts 159// xxx.ets 160import { webview } from '@kit.ArkWeb'; 161 162// In the following example, **addAdsBlockAllowedList()** and **addAdsBlockDisallowedList()** are used together to set the ad blocking feature for web pages. 163@Entry 164@Component 165struct WebComponent { 166 main_url: string = 'https://www.example.com'; 167 text_input_controller: TextInputController = new TextInputController(); 168 controller: webview.WebviewController = new webview.WebviewController(); 169 170 @State input_text: string = 'https://www.example.com'; 171 172 build() { 173 Column() { 174 Row() { 175 Flex() { 176 TextInput({ text: this.input_text, placeholder: this.main_url, controller: this.text_input_controller}) 177 .id("input_url") 178 .height(40) 179 .margin(5) 180 .borderColor(Color.Blue) 181 .onChange((value: string) => { 182 this.input_text = value; 183 }) 184 185 Button({type: ButtonType.Capsule}) { Text("Go") } 186 .onClick(() => { 187 this.controller.loadUrl(this.input_text); 188 }) 189 190 Button({type: ButtonType.Capsule}) { Text("addAdsBlockAllowedList") } 191 .onClick(() => { 192 let arrDisallowDomainSuffixes = new Array<string>(); 193 arrDisallowDomainSuffixes.push('example.com'); 194 webview.AdsBlockManager.addAdsBlockDisallowedList(arrDisallowDomainSuffixes); 195 196 let arrAllowedDomainSuffixes = new Array<string>(); 197 arrAllowedDomainSuffixes.push('news.example.com'); 198 arrAllowedDomainSuffixes.push('sport.example.com'); 199 webview.AdsBlockManager.addAdsBlockAllowedList(arrAllowedDomainSuffixes); 200 }) 201 } 202 } 203 Web({ src: this.main_url, controller: this.controller }) 204 .onControllerAttached(()=>{ 205 this.controller.enableAdsBlock(true); 206 }) 207 } 208 } 209} 210``` 211 212Note that the **DisallowedList** and **AllowedList** of the **AdsBlockManager** are not persisted. Therefore, the lists are reset to empty when the application is restarted. 213If the ad blocking feature of the **Web** component is not enabled through [enableAdsBlock()](../reference/apis-arkweb/js-apis-webview.md#enableadsblock12), the preceding APIs do not take effect in the **Web** component. 214 215### Collecting Ad Blocking Information 216When ad blocking is enabled on the **Web** component, if any ad is blocked on the accessed web page, the [onAdsBlocked()](../reference/apis-arkweb/arkts-basic-components-web-attributes.md#onadsblocked12) callback of the **Web** component notifies the application. You can collect blocking information and statistics as needed. 217 218```ts 219// xxx.ets 220import { webview } from '@kit.ArkWeb'; 221 222 223@Entry 224@Component 225struct WebComponent { 226 @State totalAdsBlockCounts: number = 0; 227 controller: webview.WebviewController = new webview.WebviewController(); 228 229 build() { 230 Column() { 231 Web({ src: 'https://www.example.com', controller: this.controller }) 232 .onAdsBlocked((details: AdsBlockedDetails) => { 233 if (details) { 234 console.log(' Blocked ' + details.adsBlocked.length + ' in ' + details.url); 235 let adList: Array<string> = Array.from(new Set(details.adsBlocked)); 236 this.totalAdsBlockCounts += adList.length; 237 console.log('Total blocked counts :' + this.totalAdsBlockCounts); 238 } 239 }) 240 } 241 } 242} 243``` 244 245To reduce the frequency of notifications and minimize the impact on the page loading process, only the first notification is made when the page is fully loaded. Subsequent blocking events are reported at intervals of 1 second, and no notifications are sent if there is no ads blocked. 246