1# Class (AdsBlockManager) 2<!--Kit: ArkWeb--> 3<!--Subsystem: Web--> 4<!--Owner: @aohui--> 5<!--Designer: @yaomingliu--> 6<!--Tester: @ghiker--> 7<!--Adviser: @HelloCrease--> 8 9通过AdsBlockManager可以向Web组件中设置自定义的广告过滤配置、关闭特定网站的广告过滤功能,其中每个应用中的所有Web组件都共享一个AdsBlockManager实例。 10 11> **说明:** 12> 13> - 本模块首批接口从API version 9开始支持。后续版本如有新增内容,则采用上角标单独标记该内容的起始版本。 14> 15> - 本Class首批接口从API version 12开始支持。 16> 17> - 示例效果请以真机运行为准,当前DevEco Studio预览器不支持。 18> 19> - 静态方法必须在用户界面(UI)线程上使用。 20 21## 导入模块 22 23```ts 24import { webview } from '@kit.ArkWeb'; 25``` 26 27## setAdsBlockRules<sup>12+</sup> 28 29static setAdsBlockRules(rulesFile: string, replace: boolean): void 30 31向Web组件中设置自定义的符合通用easylist语法规则的广告过滤配置文件。 32 33> **说明:** 34> 35> 此接口设置的广告过滤规则,内部解析成功后会持久化存储,应用重启后不需要重复设置。 36 37**系统能力:** SystemCapability.Web.Webview.Core 38 39**参数:** 40 41| 参数名 | 类型 | 必填 | 说明 | 42| ---------- | ------ | ---- | -------------------------------- | 43| rulesFile | string | 是 | 指定了符合 easylist 通用语法的规则文件路径,应用需要有此文件的读权限。 | 44| replace | boolean | 是 | true表示强制替换掉内置的默认规则,false表示设置的自定义规则将与内置规则共同工作。 | 45 46**错误码:** 47 48> **说明:** 49> 50> 从API version 18开始,在不支持广告过滤功能的设备上调用该API会抛出801异常。 51 52以下错误码的详细介绍请参见[webview错误码](errorcode-webview.md)。 53 54| 错误码ID | 错误信息 | 55| -------- | ----------------------- | 56| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. | 57| 801 | Capability not supported. | 58 59**示例:** 60 61```ts 62// xxx.ets 63import { webview } from '@kit.ArkWeb'; 64import { picker, fileUri } from '@kit.CoreFileKit'; 65 66// 演示点击按钮,通过filepicker打开一个easylist规则文件并设置到Web组件中 67@Entry 68@Component 69struct WebComponent { 70 controller: webview.WebviewController = new webview.WebviewController(); 71 72 build() { 73 Row() { 74 Flex() { 75 Button({ type: ButtonType.Capsule }) { 76 Text("setAdsBlockRules") 77 } 78 .onClick(() => { 79 try { 80 let documentSelectionOptions: ESObject = new picker.DocumentSelectOptions(); 81 let documentPicker: ESObject = new picker.DocumentViewPicker(); 82 documentPicker.select(documentSelectionOptions).then((documentSelectResult: ESObject) => { 83 if (documentSelectResult && documentSelectResult.length > 0) { 84 let fileRealPath = new fileUri.FileUri(documentSelectResult[0]); 85 console.info('DocumentViewPicker.select successfully, uri: ' + fileRealPath); 86 webview.AdsBlockManager.setAdsBlockRules(fileRealPath.path, true); 87 } 88 }) 89 } catch (err) { 90 console.error('DocumentViewPicker.select failed with err:' + err); 91 } 92 }) 93 } 94 } 95 } 96} 97``` 98 99## addAdsBlockDisallowedList<sup>12+</sup> 100 101static addAdsBlockDisallowedList(domainSuffixes: Array\<string\>): void 102 103向AdsBlockManager的DisallowedList中添加一组域名。广告过滤功能开启时,将禁用这些网站的广告过滤功能。 104 105> **说明:** 106> 107> 此接口设置的域名不会持久化,应用重启需要重新设置。 108> 109> 广告过滤特性会使用后缀匹配的方式判断domainSuffix和当前站点的url是否能匹配,例如,当前Web组件打开的网站是https://www.example.com,设置的DisallowList中有'example.com'或者'www.example.com',后缀匹配成功,此网站将禁用广告过滤,访问'https://m.example.com'也将禁用广告过滤。 110 111**系统能力:** SystemCapability.Web.Webview.Core 112 113**参数:** 114 115| 参数名 | 类型 | 必填 | 说明 | 116| ---------- | ------ | ---- | -------------------------------- | 117| domainSuffixes | Array\<string\> | 是 | 一组域名列表,例如['example.com', 'abcd.efg.com'] | 118 119**错误码:** 120 121> **说明:** 122> 123> 从API version 18开始,在不支持广告过滤功能的设备上调用该API会抛出801异常。 124 125以下错误码的详细介绍请参见[webview错误码](errorcode-webview.md)。 126 127| 错误码ID | 错误信息 | 128| -------- | ----------------------- | 129| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. | 130| 801 | Capability not supported. | 131 132**示例:** 133 134```ts 135// xxx.ets 136import { webview } from '@kit.ArkWeb'; 137 138// 演示通过一个按钮的点击向Web组件设置广告过滤的域名策略 139@Entry 140@Component 141struct WebComponent { 142 main_url: string = 'https://www.example.com'; 143 text_input_controller: TextInputController = new TextInputController(); 144 controller: webview.WebviewController = new webview.WebviewController(); 145 @State input_text: string = 'https://www.example.com'; 146 147 build() { 148 Column() { 149 Row() { 150 Flex() { 151 TextInput({ text: this.input_text, placeholder: this.main_url, controller: this.text_input_controller}) 152 .id("input_url") 153 .height(40) 154 .margin(5) 155 .borderColor(Color.Blue) 156 .onChange((value: string) => { 157 this.input_text = value; 158 }) 159 160 Button({type: ButtonType.Capsule}) { Text("Go") } 161 .onClick(() => { 162 this.controller.loadUrl(this.input_text); 163 }) 164 165 Button({type: ButtonType.Capsule}) { Text("addAdsBlockDisallowedList") } 166 .onClick(() => { 167 let arrDomainSuffixes = new Array<string>(); 168 arrDomainSuffixes.push('example.com'); 169 arrDomainSuffixes.push('abcdefg.cn'); 170 webview.AdsBlockManager.addAdsBlockDisallowedList(arrDomainSuffixes); 171 }) 172 } 173 } 174 Web({ src: this.main_url, controller: this.controller }) 175 .onControllerAttached(()=>{ 176 this.controller.enableAdsBlock(true); 177 }) 178 } 179 } 180} 181``` 182 183## removeAdsBlockDisallowedList<sup>12+</sup> 184 185static removeAdsBlockDisallowedList(domainSuffixes: Array\<string\>): void 186 187从AdsBlockManager的DisallowedList中删除一组域名。 188 189> **说明:** 190> 191> AdsBlockManager的DisallowedList不会持久化,应用重启需要重新设置。删除不存在的条目不会触发异常。 192 193**系统能力:** SystemCapability.Web.Webview.Core 194 195**参数:** 196 197| 参数名 | 类型 | 必填 | 说明 | 198| ---------- | ------ | ---- | -------------------------------- | 199| domainSuffixes | Array\<string\> | 是 | 一组域名列表,例如['example.com', 'abcd.efg.com'] | 200 201**错误码:** 202 203> **说明:** 204> 205> 从API version 18开始,在不支持广告过滤功能的设备上调用该API会抛出801异常。 206 207以下错误码的详细介绍请参见[webview错误码](errorcode-webview.md)。 208 209| 错误码ID | 错误信息 | 210| -------- | ----------------------- | 211| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. | 212| 801 | Capability not supported. | 213 214**示例:** 215 216```ts 217// xxx.ets 218import { webview } from '@kit.ArkWeb'; 219 220// 演示通过一个按钮的点击从AdsBlockManager的DisallowedList中删除域名元素 221@Entry 222@Component 223struct WebComponent { 224 main_url: string = 'https://www.example.com'; 225 text_input_controller: TextInputController = new TextInputController(); 226 controller: webview.WebviewController = new webview.WebviewController(); 227 @State input_text: string = 'https://www.example.com'; 228 229 build() { 230 Column() { 231 Row() { 232 Flex() { 233 TextInput({ text: this.input_text, placeholder: this.main_url, controller: this.text_input_controller}) 234 .id("input_url") 235 .height(40) 236 .margin(5) 237 .borderColor(Color.Blue) 238 .onChange((value: string) => { 239 this.input_text = value; 240 }) 241 242 Button({type: ButtonType.Capsule}) { Text("Go") } 243 .onClick(() => { 244 this.controller.loadUrl(this.input_text); 245 }) 246 247 Button({type: ButtonType.Capsule}) { Text("removeAdsBlockDisallowedList") } 248 .onClick(() => { 249 let arrDomainSuffixes = new Array<string>(); 250 arrDomainSuffixes.push('example.com'); 251 arrDomainSuffixes.push('abcdefg.cn'); 252 webview.AdsBlockManager.removeAdsBlockDisallowedList(arrDomainSuffixes); 253 }) 254 } 255 } 256 Web({ src: this.main_url, controller: this.controller }) 257 .onControllerAttached(()=>{ 258 this.controller.enableAdsBlock(true); 259 }) 260 } 261 } 262} 263``` 264 265## clearAdsBlockDisallowedList<sup>12+</sup> 266 267static clearAdsBlockDisallowedList(): void 268 269清空AdsBlockManager的DisallowedList。 270 271**系统能力:** SystemCapability.Web.Webview.Core 272 273**错误码:** 274 275> **说明:** 276> 277> 从API version 18开始,在不支持广告过滤功能的设备上调用该API会抛出801异常。 278 279以下错误码的详细介绍请参见[webview错误码](errorcode-webview.md)。 280 281| 错误码ID | 错误信息 | 282| -------- | ----------------------- | 283| 801 | Capability not supported. | 284 285**示例:** 286 287```ts 288// xxx.ets 289import { webview } from '@kit.ArkWeb'; 290 291@Entry 292@Component 293struct WebComponent { 294 main_url: string = 'https://www.example.com'; 295 text_input_controller: TextInputController = new TextInputController(); 296 controller: webview.WebviewController = new webview.WebviewController(); 297 @State input_text: string = 'https://www.example.com'; 298 299 build() { 300 Column() { 301 Row() { 302 Flex() { 303 TextInput({ text: this.input_text, placeholder: this.main_url, controller: this.text_input_controller}) 304 .id("input_url") 305 .height(40) 306 .margin(5) 307 .borderColor(Color.Blue) 308 .onChange((value: string) => { 309 this.input_text = value; 310 }) 311 312 Button({type: ButtonType.Capsule}) { Text("Go") } 313 .onClick(() => { 314 this.controller.loadUrl(this.input_text); 315 }) 316 317 Button({type: ButtonType.Capsule}) { Text("clearAdsBlockDisallowedList") } 318 .onClick(() => { 319 webview.AdsBlockManager.clearAdsBlockDisallowedList(); 320 }) 321 } 322 } 323 Web({ src: this.main_url, controller: this.controller }) 324 .onControllerAttached(()=>{ 325 this.controller.enableAdsBlock(true); 326 }) 327 } 328 } 329} 330``` 331 332## addAdsBlockAllowedList<sup>12+</sup> 333 334static addAdsBlockAllowedList(domainSuffixes: Array\<string\>): void 335 336向AdsBlockManager的AllowedList中添加一组域名,主要用于重新开启DisallowList中的部分网站的广告过滤。 337 338> **说明:** 339> 340> 此接口设置的域名不会持久化,应用重启需要重新设置。 341> 342> AllowedList的优先级比DisAllowList高,例如,DisallowList中配置了['example.com'],禁用了所有example.com域名下的网页,此时如果需要开启'news.example.com'下的广告过滤,可以使用addAdsBlockAllowedList(['news.example.com'])。 343 344**系统能力:** SystemCapability.Web.Webview.Core 345 346**参数:** 347 348| 参数名 | 类型 | 必填 | 说明 | 349| ---------- | ------ | ---- | -------------------------------- | 350| domainSuffixes | Array\<string\> | 是 | 一组域名列表,例如['example.com', 'abcd.efg.com'] | 351 352**错误码:** 353 354> **说明:** 355> 356> 从API version 18开始,在不支持广告过滤功能的设备上调用该API会抛出801异常。 357 358以下错误码的详细介绍请参见[webview错误码](errorcode-webview.md)。 359 360| 错误码ID | 错误信息 | 361| -------- | ----------------------- | 362| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. | 363| 801 | Capability not supported. | 364 365**示例:** 366 367```ts 368// xxx.ets 369import { webview } from '@kit.ArkWeb'; 370 371// 演示通过一个按钮的点击向Web组件设置广告过滤的域名策略 372@Entry 373@Component 374struct WebComponent { 375 main_url: string = 'https://www.example.com'; 376 text_input_controller: TextInputController = new TextInputController(); 377 controller: webview.WebviewController = new webview.WebviewController(); 378 @State input_text: string = 'https://www.example.com'; 379 380 build() { 381 Column() { 382 Row() { 383 Flex() { 384 TextInput({ text: this.input_text, placeholder: this.main_url, controller: this.text_input_controller}) 385 .id("input_url") 386 .height(40) 387 .margin(5) 388 .borderColor(Color.Blue) 389 .onChange((value: string) => { 390 this.input_text = value; 391 }) 392 393 Button({type: ButtonType.Capsule}) { Text("Go") } 394 .onClick(() => { 395 this.controller.loadUrl(this.input_text); 396 }) 397 398 Button({type: ButtonType.Capsule}) { Text("addAdsBlockAllowedList") } 399 .onClick(() => { 400 let arrDisallowDomainSuffixes = new Array<string>(); 401 arrDisallowDomainSuffixes.push('example.com'); 402 webview.AdsBlockManager.addAdsBlockDisallowedList(arrDisallowDomainSuffixes); 403 404 let arrAllowedDomainSuffixes = new Array<string>(); 405 arrAllowedDomainSuffixes.push('news.example.com'); 406 webview.AdsBlockManager.addAdsBlockAllowedList(arrAllowedDomainSuffixes); 407 }) 408 } 409 } 410 Web({ src: this.main_url, controller: this.controller }) 411 .onControllerAttached(()=>{ 412 this.controller.enableAdsBlock(true) 413 }) 414 } 415 } 416} 417``` 418 419## removeAdsBlockAllowedList<sup>12+</sup> 420 421static removeAdsBlockAllowedList(domainSuffixes: Array\<string\>): void 422 423从AdsBlockManager的AllowedList中删除一组域名。 424 425> **说明:** 426> 427> AdsBlockManager的AllowedList不会持久化,应用重启需要重新设置。删除不存在的条目不会触发异常。 428 429**系统能力:** SystemCapability.Web.Webview.Core 430 431**参数:** 432 433| 参数名 | 类型 | 必填 | 说明 | 434| ---------- | ------ | ---- | -------------------------------- | 435| domainSuffixes | Array\<string\> | 是 | 一组域名列表,例如['example.com', 'abcd.efg.com'] | 436 437**错误码:** 438 439> **说明:** 440> 441> 从API version 18开始,在不支持广告过滤功能的设备上调用该API会抛出801异常。 442 443以下错误码的详细介绍请参见[webview错误码](errorcode-webview.md)。 444 445| 错误码ID | 错误信息 | 446| -------- | ----------------------- | 447| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. | 448| 801 | Capability not supported. | 449 450**示例:** 451 452```ts 453// xxx.ets 454import { webview } from '@kit.ArkWeb'; 455 456// 演示通过一个按钮的点击从AdsBlockManager的DisallowedList中删除域名元素 457@Entry 458@Component 459struct WebComponent { 460 main_url: string = 'https://www.example.com'; 461 text_input_controller: TextInputController = new TextInputController(); 462 controller: webview.WebviewController = new webview.WebviewController(); 463 @State input_text: string = 'https://www.example.com'; 464 465 build() { 466 Column() { 467 Row() { 468 Flex() { 469 TextInput({ text: this.input_text, placeholder: this.main_url, controller: this.text_input_controller}) 470 .id("input_url") 471 .height(40) 472 .margin(5) 473 .borderColor(Color.Blue) 474 .onChange((value: string) => { 475 this.input_text = value; 476 }) 477 478 Button({type: ButtonType.Capsule}) { Text("Go") } 479 .onClick(() => { 480 this.controller.loadUrl(this.input_text); 481 }) 482 483 Button({type: ButtonType.Capsule}) { Text("removeAdsBlockAllowedList") } 484 .onClick(() => { 485 let arrDomainSuffixes = new Array<string>(); 486 arrDomainSuffixes.push('example.com'); 487 arrDomainSuffixes.push('abcdefg.cn'); 488 webview.AdsBlockManager.removeAdsBlockAllowedList(arrDomainSuffixes); 489 }) 490 } 491 } 492 Web({ src: this.main_url, controller: this.controller }) 493 .onControllerAttached(()=>{ 494 this.controller.enableAdsBlock(true); 495 }) 496 } 497 } 498} 499``` 500 501## clearAdsBlockAllowedList<sup>12+</sup> 502 503static clearAdsBlockAllowedList(): void 504 505清空AdsBlockManager的AllowedList。 506 507**系统能力:** SystemCapability.Web.Webview.Core 508 509**错误码:** 510 511> **说明:** 512> 513> 从API version 18开始,在不支持广告过滤功能的设备上调用该API会抛出801异常。 514 515以下错误码的详细介绍请参见[webview错误码](errorcode-webview.md)。 516 517| 错误码ID | 错误信息 | 518| -------- | ----------------------- | 519| 801 | Capability not supported. | 520 521**示例:** 522 523```ts 524// xxx.ets 525import { webview } from '@kit.ArkWeb'; 526 527@Entry 528@Component 529struct WebComponent { 530 main_url: string = 'https://www.example.com'; 531 text_input_controller: TextInputController = new TextInputController(); 532 controller: webview.WebviewController = new webview.WebviewController(); 533 @State input_text: string = 'https://www.example.com'; 534 535 536 build() { 537 Column() { 538 Row() { 539 Flex() { 540 TextInput({ text: this.input_text, placeholder: this.main_url, controller: this.text_input_controller}) 541 .id("input_url") 542 .height(40) 543 .margin(5) 544 .borderColor(Color.Blue) 545 .onChange((value: string) => { 546 this.input_text = value; 547 }) 548 549 Button({type: ButtonType.Capsule}) { Text("Go") } 550 .onClick(() => { 551 this.controller.loadUrl(this.input_text); 552 }) 553 554 Button({type: ButtonType.Capsule}) { Text("clearAdsBlockAllowedList") } 555 .onClick(() => { 556 webview.AdsBlockManager.clearAdsBlockAllowedList(); 557 }) 558 } 559 } 560 Web({ src: this.main_url, controller: this.controller }) 561 .onControllerAttached(()=>{ 562 this.controller.enableAdsBlock(true); 563 }) 564 } 565 } 566} 567```