1e41f4b71Sopenharmony_ci# Enabling Ads Blocking of the Web Component 2e41f4b71Sopenharmony_ci 3e41f4b71Sopenharmony_ciThe **Web** component provides features for blocking advertisement, including the common capabilities such as URL interception and element hiding. You can use [setAdsBlockRules()](../reference/apis-arkweb/js-apis-webview.md#setadsblockrules12) provided by **AdsBlockManager** to set blocking rules in EasyList and use [enableAdsBlock()](../reference/apis-arkweb/js-apis-webview.md#enableadsblock12) of the **Web** component to enable the ads blocking. 4e41f4b71Sopenharmony_ci 5e41f4b71Sopenharmony_ci## Enabling Ads Blocking 6e41f4b71Sopenharmony_ciThe following example shows how to select the EasyList rule file using the file picker and how to enable the ads blocking in an application. 7e41f4b71Sopenharmony_ci 8e41f4b71Sopenharmony_ci```ts 9e41f4b71Sopenharmony_ci// xxx.ets 10e41f4b71Sopenharmony_ciimport { webview } from '@kit.ArkWeb'; 11e41f4b71Sopenharmony_ciimport { picker, fileUri } from '@kit.CoreFileKit'; 12e41f4b71Sopenharmony_ci 13e41f4b71Sopenharmony_ci// This example demonstrates how to click a button to open the EasyList using file picker and set the file in the Web component. 14e41f4b71Sopenharmony_ci@Entry 15e41f4b71Sopenharmony_ci@Component 16e41f4b71Sopenharmony_cistruct WebComponent { 17e41f4b71Sopenharmony_ci main_url: string = 'https://www.example.com'; 18e41f4b71Sopenharmony_ci controller: webview.WebviewController = new webview.WebviewController(); 19e41f4b71Sopenharmony_ci 20e41f4b71Sopenharmony_ci @State input_text: string = 'https://www.example.com'; 21e41f4b71Sopenharmony_ci 22e41f4b71Sopenharmony_ci build() { 23e41f4b71Sopenharmony_ci Column() { 24e41f4b71Sopenharmony_ci Row() { 25e41f4b71Sopenharmony_ci Flex() { 26e41f4b71Sopenharmony_ci Button({type: ButtonType.Capsule}) { 27e41f4b71Sopenharmony_ci Text("setAdsBlockRules") 28e41f4b71Sopenharmony_ci } 29e41f4b71Sopenharmony_ci .onClick(() => { 30e41f4b71Sopenharmony_ci try { 31e41f4b71Sopenharmony_ci let documentSelectionOptions: ESObject = new picker.DocumentSelectOptions(); 32e41f4b71Sopenharmony_ci let documentPicker: ESObject = new picker.DocumentViewPicker(); 33e41f4b71Sopenharmony_ci documentPicker.select(documentSelectionOptions).then((documentSelectResult: ESObject) => { 34e41f4b71Sopenharmony_ci if (documentSelectResult && documentSelectResult.length > 0) { 35e41f4b71Sopenharmony_ci let fileRealPath = new fileUri.FileUri(documentSelectResult[0]); 36e41f4b71Sopenharmony_ci console.info('DocumentViewPicker.select successfully, uri: ' + fileRealPath); 37e41f4b71Sopenharmony_ci webview.AdsBlockManager.setAdsBlockRules(fileRealPath.path, true); 38e41f4b71Sopenharmony_ci } 39e41f4b71Sopenharmony_ci }) 40e41f4b71Sopenharmony_ci } catch (err) { 41e41f4b71Sopenharmony_ci console.error('DocumentViewPicker.select failed with err:' + err); 42e41f4b71Sopenharmony_ci } 43e41f4b71Sopenharmony_ci }) 44e41f4b71Sopenharmony_ci } 45e41f4b71Sopenharmony_ci } 46e41f4b71Sopenharmony_ci Web({ src: this.main_url, controller: this.controller }) 47e41f4b71Sopenharmony_ci .onControllerAttached(()=>{ 48e41f4b71Sopenharmony_ci this.controller.enableAdsBlock(true); 49e41f4b71Sopenharmony_ci }) 50e41f4b71Sopenharmony_ci } 51e41f4b71Sopenharmony_ci } 52e41f4b71Sopenharmony_ci} 53e41f4b71Sopenharmony_ci``` 54e41f4b71Sopenharmony_ci 55e41f4b71Sopenharmony_ciWhen the component has a built-in EasyList, the **replace** parameter of [setAdsBlockRules()](../reference/apis-arkweb/js-apis-webview.md#setadsblockrules12) can be used to set the options of the built-in rule. If **replace** is set to **true**, the built-in EasyList is not used. Otherwise, the custom rule and built-in rule work at the same time. If the built-in rule conflicts with the custom rule, you can use set **replace** to **true** to disable the built-in rule. 56e41f4b71Sopenharmony_ci 57e41f4b71Sopenharmony_ciThe custom rule is a global configuration rule for the application that it takes effect for all **Web** components in the application processes. In addition, the rule is persistent that it continues to work when the application is restarted. 58e41f4b71Sopenharmony_ci 59e41f4b71Sopenharmony_ci## Disabling Ads Blocking on Pages with Specific Domain Names 60e41f4b71Sopenharmony_ciWhen the ads blocking of a **Web** component is enabled, you may want to disable it for some specific pages. In addition to the custom EasyList, you can also use [addAdsBlockDisallowedList()](../reference/apis-arkweb/js-apis-webview.md#addadsblockdisallowedlist12) provided by **AdsBlockManager** to disable the blocking. 61e41f4b71Sopenharmony_ci 62e41f4b71Sopenharmony_ci```ts 63e41f4b71Sopenharmony_ci// xxx.ets 64e41f4b71Sopenharmony_ciimport { webview } from '@kit.ArkWeb'; 65e41f4b71Sopenharmony_ci 66e41f4b71Sopenharmony_ci// This example demonstrates how to click a button to add an array of domain names to the disallowed list. 67e41f4b71Sopenharmony_ci@Entry 68e41f4b71Sopenharmony_ci@Component 69e41f4b71Sopenharmony_cistruct WebComponent { 70e41f4b71Sopenharmony_ci main_url: string = 'https://www.example.com'; 71e41f4b71Sopenharmony_ci text_input_controller: TextInputController = new TextInputController(); 72e41f4b71Sopenharmony_ci controller: webview.WebviewController = new webview.WebviewController(); 73e41f4b71Sopenharmony_ci 74e41f4b71Sopenharmony_ci @State input_text: string = 'https://www.example.com'; 75e41f4b71Sopenharmony_ci 76e41f4b71Sopenharmony_ci build() { 77e41f4b71Sopenharmony_ci Column() { 78e41f4b71Sopenharmony_ci Row() { 79e41f4b71Sopenharmony_ci Flex() { 80e41f4b71Sopenharmony_ci TextInput({ text: this.input_text, placeholder: this.main_url, controller: this.text_input_controller}) 81e41f4b71Sopenharmony_ci .id("input_url") 82e41f4b71Sopenharmony_ci .height(40) 83e41f4b71Sopenharmony_ci .margin(5) 84e41f4b71Sopenharmony_ci .borderColor(Color.Blue) 85e41f4b71Sopenharmony_ci .onChange((value: string) => { 86e41f4b71Sopenharmony_ci this.input_text = value; 87e41f4b71Sopenharmony_ci }) 88e41f4b71Sopenharmony_ci 89e41f4b71Sopenharmony_ci Button({type: ButtonType.Capsule}) { Text("Go") } 90e41f4b71Sopenharmony_ci .onClick(() => { 91e41f4b71Sopenharmony_ci this.controller.loadUrl(this.input_text); 92e41f4b71Sopenharmony_ci }) 93e41f4b71Sopenharmony_ci 94e41f4b71Sopenharmony_ci Button({type: ButtonType.Capsule}) { Text("addAdsBlockDisallowedList") } 95e41f4b71Sopenharmony_ci .onClick(() => { 96e41f4b71Sopenharmony_ci let arrDomainSuffixes = new Array<string>(); 97e41f4b71Sopenharmony_ci arrDomainSuffixes.push('example.com'); 98e41f4b71Sopenharmony_ci arrDomainSuffixes.push('abcdefg.cn'); 99e41f4b71Sopenharmony_ci webview.AdsBlockManager.addAdsBlockDisallowedList(arrDomainSuffixes); 100e41f4b71Sopenharmony_ci }) 101e41f4b71Sopenharmony_ci } 102e41f4b71Sopenharmony_ci } 103e41f4b71Sopenharmony_ci Web({ src: this.main_url, controller: this.controller }) 104e41f4b71Sopenharmony_ci .onControllerAttached(()=>{ 105e41f4b71Sopenharmony_ci this.controller.enableAdsBlock(true); 106e41f4b71Sopenharmony_ci }) 107e41f4b71Sopenharmony_ci } 108e41f4b71Sopenharmony_ci } 109e41f4b71Sopenharmony_ci} 110e41f4b71Sopenharmony_ci``` 111e41f4b71Sopenharmony_ci 112e41f4b71Sopenharmony_ciThe domain names are set to **DisallowedList** of **AdsBlockManager** through **addAdsBlockDisallowedList()**. When the page is loaded, the system matches the suffix of the web page URL with the domain names in **DisallowedList**. If the matching is successful, ads blocking will be disabled on the page. In addition, [addAdsBlockAllowedList()](../reference/apis-arkweb/js-apis-webview.md#addadsblockallowedlist12) is provided to be used with **DisallowedList()** to set domain names and determine whether to enable ads blocking. 113e41f4b71Sopenharmony_ci 114e41f4b71Sopenharmony_ci**AdsBlockManager** caches two lists of domain names, including **DisallowedList** and **AllowList**. **DisallowedList** is used to disable ads blocking on web pages, and **AllowList** is used to enable ads 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 is enabled. Otherwise, the system continues to match the web page URL with **DisallowedList**. If the matching is successful, the ads blocking is disabled. If the accessed web page is neither list in **AllowList** nor in **DisallowedList**, the ads blocking for this web page is enabled by default. 115e41f4b71Sopenharmony_ci 116e41f4b71Sopenharmony_ciFor example, if you want to enable ads blocking for the **news.example.com** and **sport.example.com** domain in an application, but need to disable ads blocking for other web pages under the **example.com** domain, you can use **addAdsBlockDisallowedList()** to add the **example.com** domain to **DisallowedList**, and then use **addAdsBlockAllowedList()** to add the **news.example.com** and **sport.example.com** domain to **AllowedList**. 117e41f4b71Sopenharmony_ci 118e41f4b71Sopenharmony_ci```ts 119e41f4b71Sopenharmony_ci// xxx.ets 120e41f4b71Sopenharmony_ciimport { webview } from '@kit.ArkWeb'; 121e41f4b71Sopenharmony_ci 122e41f4b71Sopenharmony_ci// In the following example, **addAdsBlockAllowedList()** and **addAdsBlockDisallowedList()** are used together to set the ads blocking for web pages. 123e41f4b71Sopenharmony_ci@Entry 124e41f4b71Sopenharmony_ci@Component 125e41f4b71Sopenharmony_cistruct WebComponent { 126e41f4b71Sopenharmony_ci main_url: string = 'https://www.example.com'; 127e41f4b71Sopenharmony_ci text_input_controller: TextInputController = new TextInputController(); 128e41f4b71Sopenharmony_ci controller: webview.WebviewController = new webview.WebviewController(); 129e41f4b71Sopenharmony_ci 130e41f4b71Sopenharmony_ci @State input_text: string = 'https://www.example.com'; 131e41f4b71Sopenharmony_ci 132e41f4b71Sopenharmony_ci build() { 133e41f4b71Sopenharmony_ci Column() { 134e41f4b71Sopenharmony_ci Row() { 135e41f4b71Sopenharmony_ci Flex() { 136e41f4b71Sopenharmony_ci TextInput({ text: this.input_text, placeholder: this.main_url, controller: this.text_input_controller}) 137e41f4b71Sopenharmony_ci .id("input_url") 138e41f4b71Sopenharmony_ci .height(40) 139e41f4b71Sopenharmony_ci .margin(5) 140e41f4b71Sopenharmony_ci .borderColor(Color.Blue) 141e41f4b71Sopenharmony_ci .onChange((value: string) => { 142e41f4b71Sopenharmony_ci this.input_text = value; 143e41f4b71Sopenharmony_ci }) 144e41f4b71Sopenharmony_ci 145e41f4b71Sopenharmony_ci Button({type: ButtonType.Capsule}) { Text("Go") } 146e41f4b71Sopenharmony_ci .onClick(() => { 147e41f4b71Sopenharmony_ci this.controller.loadUrl(this.input_text); 148e41f4b71Sopenharmony_ci }) 149e41f4b71Sopenharmony_ci 150e41f4b71Sopenharmony_ci Button({type: ButtonType.Capsule}) { Text("addAdsBlockAllowedList") } 151e41f4b71Sopenharmony_ci .onClick(() => { 152e41f4b71Sopenharmony_ci let arrDisallowDomainSuffixes = new Array<string>(); 153e41f4b71Sopenharmony_ci arrDisallowDomainSuffixes.push('example.com'); 154e41f4b71Sopenharmony_ci webview.AdsBlockManager.addAdsBlockDisallowedList(arrDisallowDomainSuffixes); 155e41f4b71Sopenharmony_ci 156e41f4b71Sopenharmony_ci let arrAllowedDomainSuffixes = new Array<string>(); 157e41f4b71Sopenharmony_ci arrAllowedDomainSuffixes.push('news.example.com'); 158e41f4b71Sopenharmony_ci arrAllowedDomainSuffixes.push('sport.example.com'); 159e41f4b71Sopenharmony_ci webview.AdsBlockManager.addAdsBlockAllowedList(arrAllowedDomainSuffixes); 160e41f4b71Sopenharmony_ci }) 161e41f4b71Sopenharmony_ci } 162e41f4b71Sopenharmony_ci } 163e41f4b71Sopenharmony_ci Web({ src: this.main_url, controller: this.controller }) 164e41f4b71Sopenharmony_ci .onControllerAttached(()=>{ 165e41f4b71Sopenharmony_ci this.controller.enableAdsBlock(true); 166e41f4b71Sopenharmony_ci }) 167e41f4b71Sopenharmony_ci } 168e41f4b71Sopenharmony_ci } 169e41f4b71Sopenharmony_ci} 170e41f4b71Sopenharmony_ci``` 171e41f4b71Sopenharmony_ci 172e41f4b71Sopenharmony_ciNote that the **DisallowedList** and **AllowedList** of the **AdsBlockManager** are not persisted. Therefore, the lists are reset to empty when the application is restarted. 173e41f4b71Sopenharmony_ciIf the ads blocking is not enabled through **enableAdsBlock()**, the preceding APIs do not take effect in the **Web** component. 174e41f4b71Sopenharmony_ci 175e41f4b71Sopenharmony_ci## Collecting Ads Blocking Information 176e41f4b71Sopenharmony_ciWhen ads blocking is enabled on the **Web** component, if any ad is blocked on the accessed web page, the [onAdsBlocked()](../reference/apis-arkweb/ts-basic-components-web.md#onadsblocked12) callback of the **Web** component notifies the application. You can collect blocking information and statistics as needed. 177e41f4b71Sopenharmony_ci 178e41f4b71Sopenharmony_ci```ts 179e41f4b71Sopenharmony_ci// xxx.ets 180e41f4b71Sopenharmony_ciimport { webview } from '@kit.ArkWeb'; 181e41f4b71Sopenharmony_ci 182e41f4b71Sopenharmony_ci 183e41f4b71Sopenharmony_ci@Entry 184e41f4b71Sopenharmony_ci@Component 185e41f4b71Sopenharmony_cistruct WebComponent { 186e41f4b71Sopenharmony_ci @State totalAdsBlockCounts: number = 0; 187e41f4b71Sopenharmony_ci controller: webview.WebviewController = new webview.WebviewController(); 188e41f4b71Sopenharmony_ci 189e41f4b71Sopenharmony_ci build() { 190e41f4b71Sopenharmony_ci Column() { 191e41f4b71Sopenharmony_ci Web({ src: 'https://www.example.com', controller: this.controller }) 192e41f4b71Sopenharmony_ci .onAdsBlocked((details: AdsBlockedDetails) => { 193e41f4b71Sopenharmony_ci if (details) { 194e41f4b71Sopenharmony_ci console.log(' Blocked ' + details.adsBlocked.length + ' in ' + details.url); 195e41f4b71Sopenharmony_ci let adList: Array<string> = Array.from(new Set(details.adsBlocked)); 196e41f4b71Sopenharmony_ci this.totalAdsBlockCounts += adList.length; 197e41f4b71Sopenharmony_ci console.log('Total blocked counts :' + this.totalAdsBlockCounts); 198e41f4b71Sopenharmony_ci } 199e41f4b71Sopenharmony_ci }) 200e41f4b71Sopenharmony_ci } 201e41f4b71Sopenharmony_ci } 202e41f4b71Sopenharmony_ci} 203e41f4b71Sopenharmony_ci``` 204e41f4b71Sopenharmony_ci 205e41f4b71Sopenharmony_ciTo 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 ad blocked. 206