ts-basic-components-web.md 101.9 KB
Newer Older
Z
zengyawen 已提交
1 2
# Web

H
geshi  
HelloCrease 已提交
3
> **说明:** 
4 5 6
>
> - 该组件从API Version 8开始支持。后续版本如有新增内容,则采用上角标单独标记该内容的起始版本。
> - 示例效果请以真机运行为准,当前IDE预览器不支持。
Z
zengyawen 已提交
7

L
lixingchi1 已提交
8
提供具有网页显示能力的Web组件。
Z
zengyawen 已提交
9

L
liwenzhen 已提交
10
## 权限列表
Z
zhou-liting125 已提交
11
访问在线网页时需添加网络权限:ohos.permission.INTERNET。
L
liwenzhen 已提交
12

Z
zengyawen 已提交
13 14 15 16 17 18
## 子组件



## 接口

Z
zhou-liting125 已提交
19
Web(options: { src: string, controller?: WebController })
Z
zhou-liting125 已提交
20 21

**参数:**
22 23 24 25
| 参数名        | 参数类型                            | 必填   | 默认值  | 参数描述    |
| ---------- | ------------------------------- | ---- | ---- | ------- |
| src        | string                          | 是    | -    | 网页资源地址。 |
| controller | [WebController](#webcontroller) | 否    | -    | 控制器。    |
Z
zengyawen 已提交
26

Z
zhou-liting125 已提交
27
**示例:**
Z
zhou-liting125 已提交
28 29
  ```ts
  // xxx.ets
Z
zhou-liting125 已提交
30
  @Entry
L
update  
laosan_ted 已提交
31 32 33 34 35
  @Component
  struct WebComponent {
    controller:WebController = new WebController();
    build() {
      Column() {
Z
zhou-liting125 已提交
36
        Web({ src:'www.example.com', controller:this.controller })
L
update  
laosan_ted 已提交
37 38 39 40
      }
    }
  }
  ```
Z
zengyawen 已提交
41

H
geshi  
HelloCrease 已提交
42
> **说明:**
Z
zengyawen 已提交
43
>
Z
zhou-liting125 已提交
44
> 不支持转场动画。
Z
zengyawen 已提交
45

L
update  
laosan_ted 已提交
46
## 属性
Z
zhou-liting125 已提交
47 48 49 50 51 52 53 54 55

web组件的网络属性。

### domStorageAccess

domStorageAccess(domStorageAccess: boolean)

设置是否开启文档对象模型存储接口(DOM Storage API)权限,默认未开启。

Z
zhou-liting125 已提交
56
**参数:**
57 58 59
| 参数名              | 参数类型    | 必填   | 默认值  | 参数描述                                 |
| ---------------- | ------- | ---- | ---- | ------------------------------------ |
| domStorageAccess | boolean | 是    | -    | 设置是否开启文档对象模型存储接口(DOM Storage API)权限。 |
Z
zhou-liting125 已提交
60

Z
zhou-liting125 已提交
61
**示例:**
Z
zhou-liting125 已提交
62 63
  ```ts
  // xxx.ets
Z
zhou-liting125 已提交
64
  @Entry
L
update  
laosan_ted 已提交
65 66 67 68 69
  @Component
  struct WebComponent {
    controller:WebController = new WebController();
    build() {
      Column() {
Z
zhou-liting125 已提交
70
        Web({ src:'www.example.com', controller:this.controller })
L
update  
laosan_ted 已提交
71 72 73 74 75 76
        .domStorageAccess(true)
      }
    }
  }
  ```

Z
zhou-liting125 已提交
77 78 79 80
### fileAccess

fileAccess(fileAccess: boolean)

81
设置是否开启通过[$rawfile(filepath/filename)](../../ui/ts-resource-access.md)访问应用中rawfile路径的文件, 默认启用。
Z
zhou-liting125 已提交
82

Z
zhou-liting125 已提交
83
**参数:**
84 85
| 参数名        | 参数类型    | 必填   | 默认值  | 参数描述                                     |
| ---------- | ------- | ---- | ---- | ---------------------------------------- |
86
| fileAccess | boolean | 是    | true | 设置是否开启通过[$rawfile(filepath/filename)](../../ui/ts-resource-access.md)访问应用中rawfile路径的文件,默认启用。 |
Z
zhou-liting125 已提交
87

Z
zhou-liting125 已提交
88
**示例:**
Z
zhou-liting125 已提交
89 90
  ```ts
  // xxx.ets
Z
zhou-liting125 已提交
91
  @Entry
L
update  
laosan_ted 已提交
92 93 94 95 96
  @Component
  struct WebComponent {
    controller:WebController = new WebController();
    build() {
      Column() {
Z
zhou-liting125 已提交
97
        Web({ src:'www.example.com', controller:this.controller })
L
update  
laosan_ted 已提交
98 99 100 101 102 103
        .fileAccess(true)
      }
    }
  }
  ```

Z
zhou-liting125 已提交
104 105 106 107
### fileFromUrlAccess<sup>9+</sup>

fileFromUrlAccess(fileFromUrlAccess: boolean)

108
设置是否允许通过网页中的JavaScript脚本访问[$rawfile(filepath/filename)](../../ui/ts-resource-access.md)的内容,默认未启用。
Z
zhou-liting125 已提交
109

Z
zhou-liting125 已提交
110
**参数:**
111 112 113
| 参数名               | 参数类型    | 必填   | 默认值   | 参数描述                                     |
| ----------------- | ------- | ---- | ----- | ---------------------------------------- |
| fileFromUrlAccess | boolean | 是    | false | 设置是否允许通过网页中的JavaScript脚本访问[$rawfile(filepath/filename)](../../ui/ts-resource-access.md)的内容,默认未启用。 |
Z
zhou-liting125 已提交
114

Z
zhou-liting125 已提交
115
**示例:**
Z
zhou-liting125 已提交
116 117
  ```ts
  // xxx.ets
Z
zhou-liting125 已提交
118
  @Entry
L
update  
laosan_ted 已提交
119 120 121 122 123
  @Component
  struct WebComponent {
    controller:WebController = new WebController();
    build() {
      Column() {
Z
zhou-liting125 已提交
124
        Web({ src:'www.example.com', controller:this.controller })
L
update  
laosan_ted 已提交
125 126 127 128 129 130
        .fileFromUrlAccess(true)
      }
    }
  }
  ```

Z
zhou-liting125 已提交
131 132 133 134 135 136
### imageAccess

imageAccess(imageAccess: boolean)

设置是否允许自动加载图片资源,默认允许。

Z
zhou-liting125 已提交
137
**参数:**
138 139 140
| 参数名         | 参数类型    | 必填   | 默认值  | 参数描述            |
| ----------- | ------- | ---- | ---- | --------------- |
| imageAccess | boolean | 是    | -    | 设置是否允许自动加载图片资源。 |
Z
zhou-liting125 已提交
141

Z
zhou-liting125 已提交
142
**示例:**
Z
zhou-liting125 已提交
143 144
  ```ts
  // xxx.ets
Z
zhou-liting125 已提交
145
  @Entry
L
update  
laosan_ted 已提交
146 147 148 149 150
  @Component
  struct WebComponent {
    controller:WebController = new WebController();
    build() {
      Column() {
Z
zhou-liting125 已提交
151
        Web({ src:'www.example.com', controller:this.controller })
L
update  
laosan_ted 已提交
152 153 154 155 156 157
        .imageAccess(true)
      }
    }
  }
  ```

Z
zhou-liting125 已提交
158 159 160 161 162 163 164
### javaScriptProxy

javaScriptProxy(javaScriptProxy: { object: object, name: string, methodList: Array\<string\>,
    controller: WebController })

注入JavaScript对象到window对象中,并在window对象中调用该对象的方法。所有参数不支持更新。

Z
zhou-liting125 已提交
165
**参数:**
166 167 168 169 170 171
| 参数名        | 参数类型            | 必填   | 默认值  | 参数描述                      |
| ---------- | --------------- | ---- | ---- | ------------------------- |
| object     | object          | 是    | -    | 参与注册的对象。只能声明方法,不能声明属性。    |
| name       | string          | 是    | -    | 注册对象的名称,与window中调用的对象名一致。 |
| methodList | Array\<string\> | 是    | -    | 参与注册的应用侧JavaScript对象的方法。  |

Z
zhou-liting125 已提交
172
**示例:**
Z
zhou-liting125 已提交
173 174
  ```ts
  // xxx.ets
Z
zhou-liting125 已提交
175
  @Entry
L
update  
laosan_ted 已提交
176 177 178 179 180 181 182 183 184 185 186
  @Component
  struct WebComponent {
    controller:WebController = new WebController();
    testObj = {
      test: (data1, data2, data3) => {
        console.log("data1:" + data1);
        console.log("data2:" + data2);
        console.log("data3:" + data3);
        return "AceString";
      },
      toString: () => {
Z
zhou-liting125 已提交
187
        console.log('toString' + "interface instead.");
L
update  
laosan_ted 已提交
188 189 190 191
      }
    }
    build() {
      Column() {
Z
zhou-liting125 已提交
192
        Web({ src:'www.example.com', controller:this.controller })
L
laosan_ted 已提交
193
        .javaScriptAccess(true)
L
update  
laosan_ted 已提交
194
        .javaScriptProxy({
L
laosan_ted 已提交
195
          object: this.testObj,
L
update  
laosan_ted 已提交
196 197 198 199 200 201 202 203 204
          name: "objName",
          methodList: ["test", "toString"],
          controller: this.controller,
        })
      }
    }
  }
  ```

Z
zhou-liting125 已提交
205 206 207 208 209 210
### javaScriptAccess

javaScriptAccess(javaScriptAccess: boolean)

设置是否允许执行JavaScript脚本,默认允许执行。

Z
zhou-liting125 已提交
211
**参数:**
212 213 214
| 参数名              | 参数类型    | 必填   | 默认值  | 参数描述                |
| ---------------- | ------- | ---- | ---- | ------------------- |
| javaScriptAccess | boolean | 是    | -    | 是否允许执行JavaScript脚本。 |
Z
zhou-liting125 已提交
215

Z
zhou-liting125 已提交
216
**示例:**
Z
zhou-liting125 已提交
217 218
  ```ts
  // xxx.ets
Z
zhou-liting125 已提交
219
  @Entry
L
update  
laosan_ted 已提交
220 221 222 223 224
  @Component
  struct WebComponent {
    controller:WebController = new WebController();
    build() {
      Column() {
Z
zhou-liting125 已提交
225
        Web({ src:'www.example.com', controller:this.controller })
L
update  
laosan_ted 已提交
226 227 228 229 230 231
        .javaScriptAccess(true)
      }
    }
  }
  ```

Z
zhou-liting125 已提交
232 233 234 235 236 237
### mixedMode

mixedMode(mixedMode: MixedMode)

设置是否允许加载超文本传输协议(HTTP)和超文本传输安全协议(HTTPS)混合内容,默认不允许加载HTTP和HTTPS混合内容。

Z
zhou-liting125 已提交
238
**参数:**
239 240 241
| 参数名       | 参数类型                        | 必填   | 默认值  | 参数描述      |
| --------- | --------------------------- | ---- | ---- | --------- |
| mixedMode | [MixedMode](#mixedmode枚举说明) | 是    | -    | 要设置的混合内容。 |
Z
zhou-liting125 已提交
242

Z
zhou-liting125 已提交
243
**示例:**
Z
zhou-liting125 已提交
244 245
  ```ts
  // xxx.ets
Z
zhou-liting125 已提交
246
  @Entry
L
update  
laosan_ted 已提交
247 248 249
  @Component
  struct WebComponent {
    controller:WebController = new WebController();
L
laosan_ted 已提交
250
    @State mode:MixedMode = MixedMode.All;
L
update  
laosan_ted 已提交
251 252
    build() {
      Column() {
Z
zhou-liting125 已提交
253
        Web({ src:'www.example.com', controller:this.controller })
L
laosan_ted 已提交
254
        .mixedMode(this.mode)
L
update  
laosan_ted 已提交
255 256 257 258 259
      }
    }
  }
  ```

Z
zhou-liting125 已提交
260 261 262 263 264 265
### onlineImageAccess

onlineImageAccess(onlineImageAccess: boolean)

设置是否允许从网络加载图片资源(通过HTTP和HTTPS访问的资源),默认允许访问。

Z
zhou-liting125 已提交
266
**参数:**
267 268 269
| 参数名               | 参数类型    | 必填   | 默认值  | 参数描述             |
| ----------------- | ------- | ---- | ---- | ---------------- |
| onlineImageAccess | boolean | 是    | -    | 设置是否允许从网络加载图片资源。 |
Z
zhou-liting125 已提交
270

Z
zhou-liting125 已提交
271
**示例:**
Z
zhou-liting125 已提交
272 273
  ```ts
  // xxx.ets
Z
zhou-liting125 已提交
274
  @Entry
L
update  
laosan_ted 已提交
275 276 277 278 279
  @Component
  struct WebComponent {
    controller:WebController = new WebController();
    build() {
      Column() {
Z
zhou-liting125 已提交
280
        Web({ src:'www.example.com', controller:this.controller })
L
update  
laosan_ted 已提交
281 282 283 284 285 286
        .onlineImageAccess(true)
      }
    }
  }
  ```

Z
zhou-liting125 已提交
287 288 289 290 291 292
### zoomAccess

zoomAccess(zoomAccess: boolean)

设置是否支持手势进行缩放,默认允许执行缩放。

Z
zhou-liting125 已提交
293
**参数:**
294 295 296
| 参数名        | 参数类型    | 必填   | 默认值  | 参数描述          |
| ---------- | ------- | ---- | ---- | ------------- |
| zoomAccess | boolean | 是    | -    | 设置是否支持手势进行缩放。 |
Z
zhou-liting125 已提交
297

Z
zhou-liting125 已提交
298
**示例:**
Z
zhou-liting125 已提交
299 300
  ```ts
  // xxx.ets
Z
zhou-liting125 已提交
301
  @Entry
L
update  
laosan_ted 已提交
302 303 304 305 306
  @Component
  struct WebComponent {
    controller:WebController = new WebController();
    build() {
      Column() {
Z
zhou-liting125 已提交
307
        Web({ src:'www.example.com', controller:this.controller })
L
update  
laosan_ted 已提交
308 309 310 311 312 313
        .zoomAccess(true)
      }
    }
  }
  ```

Z
zhou-liting125 已提交
314 315 316 317 318 319
### overviewModeAccess

overviewModeAccess(overviewModeAccess: boolean)

设置是否使用概览模式加载网页,默认使用该方式。

Z
zhou-liting125 已提交
320
**参数:**
321 322 323
| 参数名                | 参数类型    | 必填   | 默认值  | 参数描述            |
| ------------------ | ------- | ---- | ---- | --------------- |
| overviewModeAccess | boolean | 是    | -    | 设置是否使用概览模式加载网页。 |
Z
zhou-liting125 已提交
324

Z
zhou-liting125 已提交
325
**示例:**
Z
zhou-liting125 已提交
326 327
  ```ts
  // xxx.ets
Z
zhou-liting125 已提交
328
  @Entry
L
update  
laosan_ted 已提交
329 330 331 332 333
  @Component
  struct WebComponent {
    controller:WebController = new WebController();
    build() {
      Column() {
Z
zhou-liting125 已提交
334
        Web({ src:'www.example.com', controller:this.controller })
L
update  
laosan_ted 已提交
335 336 337 338 339 340
        .overviewModeAccess(true)
      }
    }
  }
  ```

Z
zhou-liting125 已提交
341 342 343 344 345 346
### databaseAccess

databaseAccess(databaseAccess: boolean)

设置是否开启数据库存储API权限,默认不开启。

Z
zhou-liting125 已提交
347
**参数:**
348 349 350
| 参数名            | 参数类型    | 必填   | 默认值  | 参数描述              |
| -------------- | ------- | ---- | ---- | ----------------- |
| databaseAccess | boolean | 是    | -    | 设置是否开启数据库存储API权限。 |
Z
zhou-liting125 已提交
351

Z
zhou-liting125 已提交
352 353
  ```ts
  // xxx.ets
Z
zhou-liting125 已提交
354
  @Entry
L
update  
laosan_ted 已提交
355 356 357 358 359
  @Component
  struct WebComponent {
    controller:WebController = new WebController();
    build() {
      Column() {
Z
zhou-liting125 已提交
360
        Web({ src:'www.example.com', controller:this.controller })
L
update  
laosan_ted 已提交
361 362 363 364 365 366
        .databaseAccess(true)
      }
    }
  }
  ```

Z
zhou-liting125 已提交
367 368 369 370 371 372
### cacheMode

cacheMode(cacheMode: CacheMode)

设置缓存模式。

Z
zhou-liting125 已提交
373
**参数:**
374 375 376
| 参数名       | 参数类型                        | 必填   | 默认值  | 参数描述      |
| --------- | --------------------------- | ---- | ---- | --------- |
| cacheMode | [CacheMode](#cachemode枚举说明) | 是    | -    | 要设置的缓存模式。 |
Z
zhou-liting125 已提交
377

Z
zhou-liting125 已提交
378
**示例:**
Z
zhou-liting125 已提交
379 380
  ```ts
  // xxx.ets
Z
zhou-liting125 已提交
381
  @Entry
L
update  
laosan_ted 已提交
382 383 384
  @Component
  struct WebComponent {
    controller:WebController = new WebController();
L
laosan_ted 已提交
385
    @State mode:CacheMode = CacheMode.None;
L
update  
laosan_ted 已提交
386 387
    build() {
      Column() {
Z
zhou-liting125 已提交
388
        Web({ src:'www.example.com', controller:this.controller })
L
laosan_ted 已提交
389
        .cacheMode(this.mode)
L
update  
laosan_ted 已提交
390 391 392 393 394
      }
    }
  }
  ```

Z
zhou-liting125 已提交
395 396 397 398 399 400
### textZoomAtio

textZoomAtio(textZoomAtio: number)

设置页面的文本缩放百分比,默认为100%。

Z
zhou-liting125 已提交
401
**参数:**
402 403 404
| 参数名          | 参数类型   | 必填   | 默认值  | 参数描述            |
| ------------ | ------ | ---- | ---- | --------------- |
| textZoomAtio | number | 是    | -    | 要设置的页面的文本缩放百分比。 |
Z
zhou-liting125 已提交
405

Z
zhou-liting125 已提交
406
**示例:**
Z
zhou-liting125 已提交
407 408
  ```ts
  // xxx.ets
Z
zhou-liting125 已提交
409
  @Entry
L
update  
laosan_ted 已提交
410 411 412
  @Component
  struct WebComponent {
    controller:WebController = new WebController();
L
laosan_ted 已提交
413
    @State atio:number = 150;
L
update  
laosan_ted 已提交
414 415
    build() {
      Column() {
Z
zhou-liting125 已提交
416
        Web({ src:'www.example.com', controller:this.controller })
L
laosan_ted 已提交
417
        .textZoomAtio(this.atio)
L
update  
laosan_ted 已提交
418 419 420 421 422
      }
    }
  }
  ```

Z
zhou-liting125 已提交
423 424 425 426 427 428
### userAgent

userAgent(userAgent: string)

设置用户代理。

Z
zhou-liting125 已提交
429
**参数:**
430 431 432
| 参数名       | 参数类型   | 必填   | 默认值  | 参数描述      |
| --------- | ------ | ---- | ---- | --------- |
| userAgent | string | 是    | -    | 要设置的用户代理。 |
Z
zengyawen 已提交
433

Z
zhou-liting125 已提交
434
**示例:**
Z
zhou-liting125 已提交
435 436
  ```ts
  // xxx.ets
Z
zhou-liting125 已提交
437
  @Entry
L
update  
laosan_ted 已提交
438 439 440
  @Component
  struct WebComponent {
    controller:WebController = new WebController();
L
laosan_ted 已提交
441
    @State userAgent:string = 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.71 Safari/537.36';
L
update  
laosan_ted 已提交
442 443
    build() {
      Column() {
Z
zhou-liting125 已提交
444
        Web({ src:'www.example.com', controller:this.controller })
L
laosan_ted 已提交
445
        .userAgent(this.userAgent)
L
update  
laosan_ted 已提交
446 447 448 449 450
      }
    }
  }
  ```

H
geshi  
HelloCrease 已提交
451
>  **说明:**
Z
zengyawen 已提交
452
>
453
>  通用属性仅支持[width](ts-universal-attributes-size.md#属性)、[height](ts-universal-attributes-size.md#属性)、[padding](ts-universal-attributes-size.md#属性)、[margin](ts-universal-attributes-size.md#属性)、[border](ts-universal-attributes-border.md#属性)。
Z
zengyawen 已提交
454 455 456

## 事件

L
liwenzhen 已提交
457
不支持通用事件。
Z
zengyawen 已提交
458

L
update  
laosan_ted 已提交
459 460 461 462 463 464
### onAlert

onAlert(callback: (event?: { url: string; message: string; result: JsResult }) => boolean)

网页触发alert()告警弹窗时触发回调。

Z
zhou-liting125 已提交
465
**参数:**
466 467 468 469 470
| 参数名     | 参数类型                  | 参数描述            |
| ------- | --------------------- | --------------- |
| url     | string                | 当前显示弹窗所在网页的URL。 |
| message | string                | 弹窗中显示的信息。       |
| result  | [JsResult](#jsresult) | 通知Web组件用户操作行为。  |
L
update  
laosan_ted 已提交
471

Z
zhou-liting125 已提交
472
**返回值:**
473 474 475
| 类型      | 说明                                       |
| ------- | ---------------------------------------- |
| boolean | 当回调返回false时,触发默认弹窗。当回调返回true时,系统应用可以调用系统弹窗能力(包括确认和取消),并且需要根据用户的确认或取消操作调用JsResult通知Web组件最终是否离开当前页面。 |
L
update  
laosan_ted 已提交
476

Z
zhou-liting125 已提交
477
**示例:**
Z
zhou-liting125 已提交
478 479
  ```ts
  // xxx.ets
Z
zhou-liting125 已提交
480
  @Entry
L
update  
laosan_ted 已提交
481 482 483 484 485
  @Component
  struct WebComponent {
    controller:WebController = new WebController();
    build() {
      Column() {
Z
zhou-liting125 已提交
486
        Web({ src:'www.example.com', controller:this.controller })
L
laosan_ted 已提交
487 488 489 490 491 492 493 494 495 496 497 498 499 500 501
          .onAlert((event) => {
            AlertDialog.show({
              title: 'title',
              message: 'text',
              confirm: {
                value: 'onAlert',
                action: () => {
                  event.result.handleConfirm()
                }
              },
              cancel: () => {
                event.result.handleCancel()
              }
            })
            return true;
L
update  
laosan_ted 已提交
502 503 504 505 506 507 508 509 510 511 512 513
          })
      }
    }
  }
  ```

### onBeforeUnload

onBeforeUnload(callback: (event?: { url: string; message: string; result: JsResult }) => boolean)

刷新或关闭场景下,在即将离开当前页面时触发此回调。

Z
zhou-liting125 已提交
514
**参数:**
515 516 517 518 519
| 参数名     | 参数类型                  | 参数描述            |
| ------- | --------------------- | --------------- |
| url     | string                | 当前显示弹窗所在网页的URL。 |
| message | string                | 弹窗中显示的信息。       |
| result  | [JsResult](#jsresult) | 通知Web组件用户操作行为。  |
L
update  
laosan_ted 已提交
520

Z
zhou-liting125 已提交
521
**返回值:**
522 523 524
| 类型      | 说明                                       |
| ------- | ---------------------------------------- |
| boolean | 当回调返回false时,触发默认弹窗。当回调返回true时,系统应用可以调用系统弹窗能力(包括确认和取消),并且需要根据用户的确认或取消操作调用JsResult通知Web组件最终是否离开当前页面。 |
L
update  
laosan_ted 已提交
525

Z
zhou-liting125 已提交
526
**示例:**
Z
zhou-liting125 已提交
527 528
  ```ts
  // xxx.ets
Z
zhou-liting125 已提交
529
  @Entry
L
update  
laosan_ted 已提交
530 531
  @Component
  struct WebComponent {
L
laosan_ted 已提交
532 533
    controller: WebController = new WebController();
  
L
update  
laosan_ted 已提交
534 535
    build() {
      Column() {
Z
zhou-liting125 已提交
536
        Web({ src: 'www.example.com', controller: this.controller })
L
laosan_ted 已提交
537 538 539 540 541 542 543
          .onBeforeUnload((event) => {
            console.log("event.url:" + event.url);
            console.log("event.message:" + event.message);
            console.log("event.result:" + event.result);
            return false;
          })
      }
L
update  
laosan_ted 已提交
544 545 546 547 548 549 550 551 552 553
    }
  }
  ```

### onConfirm

onConfirm(callback: (event?: { url: string; message: string; result: JsResult }) => boolean)

网页调用confirm()告警时触发此回调。

Z
zhou-liting125 已提交
554
**参数:**
555 556 557 558 559
| 参数名     | 参数类型                  | 参数描述            |
| ------- | --------------------- | --------------- |
| url     | string                | 当前显示弹窗所在网页的URL。 |
| message | string                | 弹窗中显示的信息。       |
| result  | [JsResult](#jsresult) | 通知Web组件用户操作行为。  |
L
update  
laosan_ted 已提交
560

Z
zhou-liting125 已提交
561
**返回值:**
562 563 564
| 类型      | 说明                                       |
| ------- | ---------------------------------------- |
| boolean | 当回调返回false时,触发默认弹窗。当回调返回true时,系统应用可以调用系统弹窗能力(包括确认和取消),并且需要根据用户的确认或取消操作调用JsResult通知Web组件。 |
L
update  
laosan_ted 已提交
565

Z
zhou-liting125 已提交
566
**示例:**
Z
zhou-liting125 已提交
567 568
  ```ts
  // xxx.ets
Z
zhou-liting125 已提交
569
  @Entry
L
update  
laosan_ted 已提交
570 571
  @Component
  struct WebComponent {
L
laosan_ted 已提交
572 573
    controller: WebController = new WebController();
  
L
update  
laosan_ted 已提交
574 575
    build() {
      Column() {
Z
zhou-liting125 已提交
576
        Web({ src: 'www.example.com', controller: this.controller })
L
laosan_ted 已提交
577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594
          .onConfirm((event) => {
            console.log("event.url:" + event.url);
            console.log("event.message:" + event.message);
            console.log("event.result:" + event.result);
            AlertDialog.show({
              title: 'title',
              message: 'text',
              confirm: {
                value: 'onConfirm',
                action: () => {
                  event.result.handleConfirm()
                }
              },
              cancel: () => {
                event.result.handleCancel()
              }
            })
            return true;
L
update  
laosan_ted 已提交
595
          })
L
laosan_ted 已提交
596
      }
L
update  
laosan_ted 已提交
597 598 599 600 601 602 603 604
    }
  }
  ```

### onPrompt<sup>9+</sup>

onPrompt(callback: (event?: { url: string; message: string; value: string; result: JsResult }) => boolean)

Z
zhou-liting125 已提交
605
**参数:**
606 607 608 609 610
| 参数名     | 参数类型                  | 参数描述            |
| ------- | --------------------- | --------------- |
| url     | string                | 当前显示弹窗所在网页的URL。 |
| message | string                | 弹窗中显示的信息。       |
| result  | [JsResult](#jsresult) | 通知Web组件用户操作行为。  |
L
update  
laosan_ted 已提交
611

Z
zhou-liting125 已提交
612
**返回值:**
613 614 615
| 类型      | 说明                                       |
| ------- | ---------------------------------------- |
| boolean | 当回调返回false时,触发默认弹窗。当回调返回true时,系统应用可以调用系统弹窗能力(包括确认和取消),并且需要根据用户的确认或取消操作调用JsResult通知Web组件。 |
L
update  
laosan_ted 已提交
616

Z
zhou-liting125 已提交
617
**示例:**
Z
zhou-liting125 已提交
618 619
  ```ts
  // xxx.ets
Z
zhou-liting125 已提交
620
  @Entry
L
update  
laosan_ted 已提交
621 622 623 624 625
  @Component
  struct WebComponent {
    controller:WebController = new WebController();
    build() {
      Column() {
Z
zhou-liting125 已提交
626
        Web({ src:'www.example.com', controller:this.controller })
L
update  
laosan_ted 已提交
627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643
        .onPrompt((event) => {
          console.log("url:" + event.url);
          console.log("message:" + event.message);
          console.log("value:" + event.value);
          console.log("result:" + event.result);
          return true;
        })
    }
  }
  ```

### onConsole

onConsole(callback: (event?: { message: ConsoleMessage }) => boolean)

通知宿主应用JavaScript console消息。

Z
zhou-liting125 已提交
644
**参数:**
645 646 647
| 参数名     | 参数类型                              | 参数描述      |
| ------- | --------------------------------- | --------- |
| message | [ConsoleMessage](#consolemessage) | 触发的控制台信息。 |
L
update  
laosan_ted 已提交
648

Z
zhou-liting125 已提交
649
**返回值:**
650 651 652
| 类型      | 说明                                  |
| ------- | ----------------------------------- |
| boolean | 当返回true时,该条消息将不会再打印至控制台,反之仍会打印至控制台。 |
L
update  
laosan_ted 已提交
653

Z
zhou-liting125 已提交
654
**示例:**
Z
zhou-liting125 已提交
655 656
  ```ts
  // xxx.ets
Z
zhou-liting125 已提交
657
  @Entry
L
update  
laosan_ted 已提交
658 659
  @Component
  struct WebComponent {
L
laosan_ted 已提交
660 661
    controller: WebController = new WebController();
  
L
update  
laosan_ted 已提交
662 663
    build() {
      Column() {
Z
zhou-liting125 已提交
664
        Web({ src: 'www.example.com', controller: this.controller })
L
laosan_ted 已提交
665 666 667 668 669 670 671 672
          .onConsole((event) => {
            console.log('getMessage:' + event.message.getMessage());
            console.log('getSourceId:' + event.message.getSourceId());
            console.log('getLineNumber:' + event.message.getLineNumber());
            console.log('getMessageLevel:' + event.message.getMessageLevel());
            return false;
          })
      }
L
update  
laosan_ted 已提交
673 674 675 676 677 678 679 680
    }
  }
  ```

### onDownloadStart

onDownloadStart(callback: (event?: { url: string, userAgent: string, contentDisposition: string, mimetype: string, contentLength: number }) => void)

Z
zhou-liting125 已提交
681
**参数:**
682 683 684 685 686 687
| 参数名                | 参数类型          | 参数描述                                |
| ------------------ | ------------- | ----------------------------------- |
| url                | string        | 文件下载的URL。                           |
| contentDisposition | string        | 服务器返回的 Content-Disposition响应头,可能为空。 |
| mimetype           | string        | 服务器返回内容媒体类型(MIME)信息。                |
| contentLength      | contentLength | 服务器返回文件的长度。                         |
L
update  
laosan_ted 已提交
688

Z
zhou-liting125 已提交
689
**示例:**
Z
zhou-liting125 已提交
690 691
  ```ts
  // xxx.ets
Z
zhou-liting125 已提交
692
  @Entry
L
update  
laosan_ted 已提交
693 694
  @Component
  struct WebComponent {
L
laosan_ted 已提交
695 696
    controller: WebController = new WebController();
  
L
update  
laosan_ted 已提交
697 698
    build() {
      Column() {
Z
zhou-liting125 已提交
699
        Web({ src: 'www.example.com', controller: this.controller })
L
laosan_ted 已提交
700 701 702 703 704 705 706 707
          .onDownloadStart((event) => {
            console.log('url:' + event.url);
            console.log('userAgent:' + event.userAgent);
            console.log('contentDisposition:' + event.contentDisposition);
            console.log('contentLength:' + event.contentLength);
            console.log('mimetype:' + event.mimetype);
          })
      }
L
update  
laosan_ted 已提交
708 709 710 711 712 713 714 715 716 717
    }
  }
  ```

### onErrorReceive

onErrorReceive(callback: (event?: { request: WebResourceRequest, error: WebResourceError }) => void)

网页加载遇到错误时触发该回调。出于性能考虑,建议此回调中尽量执行简单逻辑。

Z
zhou-liting125 已提交
718
**参数:**
719 720 721 722
| 参数名     | 参数类型                                     | 参数描述            |
| ------- | ---------------------------------------- | --------------- |
| request | [WebResourceRequest](#webresourcerequest) | 网页请求的封装信息。      |
| error   | [WebResourceError](#webresourceerror)    | 网页加载资源错误的封装信息 。 |
L
update  
laosan_ted 已提交
723

Z
zhou-liting125 已提交
724
**示例:**
Z
zhou-liting125 已提交
725 726
  ```ts
  // xxx.ets
Z
zhou-liting125 已提交
727
  @Entry
L
update  
laosan_ted 已提交
728 729
  @Component
  struct WebComponent {
L
laosan_ted 已提交
730 731
    controller: WebController = new WebController();
  
L
update  
laosan_ted 已提交
732 733
    build() {
      Column() {
Z
zhou-liting125 已提交
734
        Web({ src: 'www.example.com', controller: this.controller })
L
laosan_ted 已提交
735 736 737 738 739 740 741 742 743 744 745 746 747 748 749
          .onErrorReceive((event) => {
            console.log('getErrorInfo:' + event.error.getErrorInfo());
            console.log('getErrorCode:' + event.error.getErrorCode());
            console.log('url:' + event.request.getRequestUrl());
            console.log('isMainFrame:' + event.request.isMainFrame());
            console.log('isRedirect:' + event.request.isRedirect());
            console.log('isRequestGesture:' + event.request.isRequestGesture());
            console.log('getRequestHeader_headerKey:' + event.request.getRequestHeader().toString());
            let result = event.request.getRequestHeader();
            console.log('The request header result size is ' + result.length);
            for (let i of result) {
              console.log('The request header key is : ' + i.headerKey + ', value is : ' + i.headerValue);
            }
          })
      }
L
update  
laosan_ted 已提交
750 751 752 753 754 755 756 757 758 759
    }
  }
  ```

### onHttpErrorReceive

onHttpErrorReceive(callback: (event?: { request: WebResourceRequest, response: WebResourceResponse }) => void)

网页加载资源遇到的HTTP错误(响应码>=400)时触发该回调。

Z
zhou-liting125 已提交
760
**参数:**
761 762 763 764
| 参数名     | 参数类型                                     | 参数描述            |
| ------- | ---------------------------------------- | --------------- |
| request | [WebResourceRequest](#webresourcerequest) | 网页请求的封装信息。      |
| error   | [WebResourceError](#webresourceerror)    | 网页加载资源错误的封装信息 。 |
L
update  
laosan_ted 已提交
765

Z
zhou-liting125 已提交
766
**示例:**
Z
zhou-liting125 已提交
767 768
  ```ts
  // xxx.ets
Z
zhou-liting125 已提交
769
  @Entry
L
update  
laosan_ted 已提交
770 771
  @Component
  struct WebComponent {
L
laosan_ted 已提交
772 773
    controller: WebController = new WebController();
  
L
update  
laosan_ted 已提交
774 775
    build() {
      Column() {
Z
zhou-liting125 已提交
776
        Web({ src: 'www.example.com', controller: this.controller })
L
laosan_ted 已提交
777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798
          .onHttpErrorReceive((event) => {
            console.log('url:' + event.request.getRequestUrl());
            console.log('isMainFrame:' + event.request.isMainFrame());
            console.log('isRedirect:' + event.request.isRedirect());
            console.log('isRequestGesture:' + event.request.isRequestGesture());
            console.log('getResponseData:' + event.response.getResponseData());
            console.log('getResponseEncoding:' + event.response.getResponseEncoding());
            console.log('getResponseMimeType:' + event.response.getResponseMimeType());
            console.log('getResponseCode:' + event.response.getResponseCode());
            console.log('getReasonMessage:' + event.response.getReasonMessage());
            let result = event.request.getRequestHeader();
            console.log('The request header result size is ' + result.length);
            for (let i of result) {
              console.log('The request header key is : ' + i.headerKey + ' , value is : ' + i.headerValue);
            }
            let resph = event.response.getResponseHeader();
            console.log('The response header result size is ' + resph.length);
            for (let i of resph) {
              console.log('The response header key is : ' + i.headerKey + ' , value is : ' + i.headerValue);
            }
          })
      }
L
update  
laosan_ted 已提交
799 800 801 802 803 804 805 806
    }
  }
  ```

### onPageBegin

onPageBegin(callback: (event?: { url: string }) => void)

807

L
update  
laosan_ted 已提交
808 809
网页开始加载时触发该回调,且只在主frame触发,iframe或者frameset的内容加载时不会触发此回调。

Z
zhou-liting125 已提交
810
**参数:**
811 812 813
| 参数名  | 参数类型   | 参数描述      |
| ---- | ------ | --------- |
| url  | string | 页面的URL地址。 |
L
update  
laosan_ted 已提交
814

Z
zhou-liting125 已提交
815
**示例:**
Z
zhou-liting125 已提交
816 817
  ```ts
  // xxx.ets
Z
zhou-liting125 已提交
818
  @Entry
L
update  
laosan_ted 已提交
819 820
  @Component
  struct WebComponent {
L
laosan_ted 已提交
821 822
    controller: WebController = new WebController();
  
L
update  
laosan_ted 已提交
823 824
    build() {
      Column() {
Z
zhou-liting125 已提交
825
        Web({ src: 'www.example.com', controller: this.controller })
L
laosan_ted 已提交
826 827 828 829
          .onPageBegin((event) => {
            console.log('url:' + event.url);
          })
      }
L
update  
laosan_ted 已提交
830 831 832 833 834 835 836 837
    }
  }
  ```

### onPageEnd

onPageEnd(callback: (event?: { url: string }) => void)

838

L
update  
laosan_ted 已提交
839 840
网页加载完成时触发该回调,且只在主frame触发。

Z
zhou-liting125 已提交
841
**参数:**
842 843 844
| 参数名  | 参数类型   | 参数描述      |
| ---- | ------ | --------- |
| url  | string | 页面的URL地址。 |
L
update  
laosan_ted 已提交
845

Z
zhou-liting125 已提交
846
**示例:**
Z
zhou-liting125 已提交
847 848
  ```ts
  // xxx.ets
Z
zhou-liting125 已提交
849
  @Entry
L
update  
laosan_ted 已提交
850 851
  @Component
  struct WebComponent {
L
laosan_ted 已提交
852 853
    controller: WebController = new WebController();
  
L
update  
laosan_ted 已提交
854 855
    build() {
      Column() {
Z
zhou-liting125 已提交
856
        Web({ src: 'www.example.com', controller: this.controller })
L
laosan_ted 已提交
857 858 859 860
          .onPageEnd((event) => {
            console.log('url:' + event.url);
          })
      }
L
update  
laosan_ted 已提交
861 862 863 864 865 866 867 868 869 870
    }
  }
  ```

### onProgressChange

onProgressChange(callback: (event?: { newProgress: number }) => void)

网页加载进度变化时触发该回调。

Z
zhou-liting125 已提交
871
**参数:**
872 873 874
| 参数名         | 参数类型   | 参数描述                  |
| ----------- | ------ | --------------------- |
| newProgress | number | 新的加载进度,取值范围为0到100的整数。 |
L
update  
laosan_ted 已提交
875

Z
zhou-liting125 已提交
876
  **示例:**
Z
zhou-liting125 已提交
877 878
  ```ts
  // xxx.ets
Z
zhou-liting125 已提交
879
  @Entry
L
update  
laosan_ted 已提交
880 881
  @Component
  struct WebComponent {
L
laosan_ted 已提交
882 883
    controller: WebController = new WebController();
  
L
update  
laosan_ted 已提交
884 885
    build() {
      Column() {
Z
zhou-liting125 已提交
886
        Web({ src: 'www.example.com', controller: this.controller })
L
laosan_ted 已提交
887 888 889 890
          .onProgressChange((event) => {
            console.log('newProgress:' + event.newProgress)
          })
      }
L
update  
laosan_ted 已提交
891 892 893 894 895 896 897 898 899 900
    }
  }
  ```

### onTitleReceive

onTitleReceive(callback: (event?: { title: string }) => void)

网页document标题更改时触发该回调。

Z
zhou-liting125 已提交
901
**参数:**
902 903 904
| 参数名   | 参数类型   | 参数描述          |
| ----- | ------ | ------------- |
| title | string | document标题内容。 |
L
update  
laosan_ted 已提交
905

Z
zhou-liting125 已提交
906
  **示例:**
Z
zhou-liting125 已提交
907 908
  ```ts
  // xxx.ets
Z
zhou-liting125 已提交
909
  @Entry
L
update  
laosan_ted 已提交
910 911
  @Component
  struct WebComponent {
L
laosan_ted 已提交
912 913
    controller: WebController = new WebController();
  
L
update  
laosan_ted 已提交
914 915
    build() {
      Column() {
Z
zhou-liting125 已提交
916
        Web({ src: 'www.example.com', controller: this.controller })
L
laosan_ted 已提交
917 918 919 920
          .onTitleReceive((event) => {
            console.log('title:' + event.title)
          })
      }
L
update  
laosan_ted 已提交
921 922 923 924 925 926 927 928 929 930
    }
  }
  ```

### onRefreshAccessedHistory

onRefreshAccessedHistory(callback: (event?: { url: string, isRefreshed: boolean }) => void)

加载网页页面完成时触发该回调,用于应用更新其访问的历史链接。

Z
zhou-liting125 已提交
931
**参数:**
932 933 934 935
| 参数名         | 参数类型    | 参数描述                              |
| ----------- | ------- | --------------------------------- |
| url         | string  | 访问的url。                           |
| isRefreshed | boolean | true表示该页面是被重新加载的,false表示该页面是新加载的。 |
L
update  
laosan_ted 已提交
936

Z
zhou-liting125 已提交
937
  **示例:**
Z
zhou-liting125 已提交
938 939
  ```ts
  // xxx.ets
Z
zhou-liting125 已提交
940
  @Entry
L
update  
laosan_ted 已提交
941 942
  @Component
  struct WebComponent {
L
laosan_ted 已提交
943 944
    controller: WebController = new WebController();
  
L
update  
laosan_ted 已提交
945 946
    build() {
      Column() {
Z
zhou-liting125 已提交
947
        Web({ src: 'www.example.com', controller: this.controller })
L
laosan_ted 已提交
948 949 950 951
          .onRefreshAccessedHistory((event) => {
            console.log('url:' + event.url + ' isReload:' + event.refreshed);
          })
      }
L
update  
laosan_ted 已提交
952 953 954 955 956 957 958 959 960 961
    }
  }
  ```

### onRenderExited

onRenderExited(callback: (event?: { renderExitReason: RenderExitReason }) => void)

应用渲染进程异常退出时触发该回调。

Z
zhou-liting125 已提交
962
**参数:**
963 964 965
| 参数名              | 参数类型                                     | 参数描述             |
| ---------------- | ---------------------------------------- | ---------------- |
| renderExitReason | [RenderExitReason](#renderexitreason枚举说明) | 渲染进程进程异常退出的具体原因。 |
L
update  
laosan_ted 已提交
966

Z
zhou-liting125 已提交
967
  **示例:**
Z
zhou-liting125 已提交
968 969
  ```ts
  // xxx.ets
Z
zhou-liting125 已提交
970
  @Entry
L
update  
laosan_ted 已提交
971 972
  @Component
  struct WebComponent {
L
laosan_ted 已提交
973 974
    controller: WebController = new WebController();
  
L
update  
laosan_ted 已提交
975 976
    build() {
      Column() {
L
laosan_ted 已提交
977 978 979 980 981
        Web({ src: 'chrome://crash/', controller: this.controller })
          .onRenderExited((event) => {
            console.log('reason:' + event.renderExitReason);
          })
      }
L
update  
laosan_ted 已提交
982 983 984 985 986 987 988 989
    }
  }
  ```

### onShowFileSelector<sup>9+</sup>

onShowFileSelector(callback: (event?: { result: FileSelectorResult, fileSelector: FileSelectorParam }) => void)

Z
zhou-liting125 已提交
990
调用此函数以处理具有“文件”输入类型的HTML表单,以响应用户按下的“选择文件”按钮。
L
update  
laosan_ted 已提交
991

Z
zhou-liting125 已提交
992
**参数:**
993 994 995 996
| 参数名          | 参数类型                                     | 参数描述              |
| ------------ | ---------------------------------------- | ----------------- |
| result       | [FileSelectorResult](#fileselectorresult9) | 用于通知Web组件文件选择的结果。 |
| fileSelector | [FileSelectorParam](#fileselectorparam9) | 文件选择器的相关信息。       |
L
update  
laosan_ted 已提交
997

Z
zhou-liting125 已提交
998
  **示例:**
Z
zhou-liting125 已提交
999 1000
  ```ts
  // xxx.ets
Z
zhou-liting125 已提交
1001
  @Entry
L
update  
laosan_ted 已提交
1002 1003
  @Component
  struct WebComponent {
L
laosan_ted 已提交
1004 1005
    controller: WebController = new WebController();

L
update  
laosan_ted 已提交
1006 1007
    build() {
      Column() {
Z
zhou-liting125 已提交
1008
        Web({ src: 'www.example.com', controller: this.controller })
L
laosan_ted 已提交
1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024
          .onShowFileSelector((event) => {
            AlertDialog.show({
              title: event.fileSelector.getTitle(),
              message: 'isCapture:' + event.fileSelector.isCapture() + " mode:" + event.fileSelector.getMode() + 'acceptType:' + event.fileSelector.getAcceptType(),
              confirm: {
                value: 'upload',
                action: () => {
                  let fileList: Array<string> = [
                    '/data/storage/el2/base/test',
                  ]
                  event.result.handleFileList(fileList)
                }
              },
              cancel: () => {
                let fileList: Array<string> = []
                event.result.handleFileList(fileList)
L
update  
laosan_ted 已提交
1025
              }
L
laosan_ted 已提交
1026 1027
            })
            return true;
L
update  
laosan_ted 已提交
1028
          })
L
laosan_ted 已提交
1029
      }
L
update  
laosan_ted 已提交
1030 1031 1032 1033 1034 1035 1036 1037 1038 1039
    }
  }
  ```

### onUrlLoadIntercept

onUrlLoadIntercept(callback: (event?: { data:string | WebResourceRequest }) => boolean)

当Web组件加载url之前触发该回调,用于是否阻止此次访问。

Z
zhou-liting125 已提交
1040
**参数:**
1041 1042 1043
| 参数名  | 参数类型                                     | 参数描述      |
| ---- | ---------------------------------------- | --------- |
| data | string / [WebResourceRequest](#webresourcerequest) | url的相关信息。 |
L
update  
laosan_ted 已提交
1044

Z
zhou-liting125 已提交
1045
**返回值:**
1046 1047 1048
| 类型      | 说明                       |
| ------- | ------------------------ |
| boolean | 返回true表示阻止此次加载,否则允许此次加载。 |
L
update  
laosan_ted 已提交
1049

Z
zhou-liting125 已提交
1050
  **示例:**
Z
zhou-liting125 已提交
1051 1052
  ```ts
  // xxx.ets
Z
zhou-liting125 已提交
1053
  @Entry
L
update  
laosan_ted 已提交
1054 1055
  @Component
  struct WebComponent {
L
laosan_ted 已提交
1056 1057
    controller: WebController = new WebController();
  
L
update  
laosan_ted 已提交
1058 1059
    build() {
      Column() {
Z
zhou-liting125 已提交
1060
        Web({ src: 'www.example.com', controller: this.controller })
L
laosan_ted 已提交
1061 1062 1063 1064 1065
          .onUrlLoadIntercept((event) => {
            console.log('onUrlLoadIntercept ' + event.data.toString())
            return true;
          })
      }
L
update  
laosan_ted 已提交
1066 1067 1068 1069 1070 1071 1072 1073 1074 1075
    }
  }
  ```

### onInterceptRequest<sup>9+</sup>

onInterceptRequest9+(callback: (event?: { request: WebResourceRequest}) => WebResourceResponse)

当Web组件加载url之前触发该回调,用于拦截url并返回响应数据。

Z
zhou-liting125 已提交
1076
**参数:**
1077 1078 1079
| 参数名     | 参数类型                                     | 参数描述        |
| ------- | ---------------------------------------- | ----------- |
| request | [WebResourceRequest](#webresourcerequest) | url请求的相关信息。 |
L
update  
laosan_ted 已提交
1080

Z
zhou-liting125 已提交
1081
**返回值:**
1082 1083 1084
| 类型                                       | 说明                          |
| ---------------------------------------- | --------------------------- |
| [WebResourceResponse](#webresourceresponse) | 返回响应数据为空表示按原来方式加载,否则加载响应数据。 |
L
update  
laosan_ted 已提交
1085

Z
zhou-liting125 已提交
1086
  **示例:**
Z
zhou-liting125 已提交
1087 1088
  ```ts
  // xxx.ets
Z
zhou-liting125 已提交
1089
  @Entry
L
update  
laosan_ted 已提交
1090 1091 1092
  @Component
  struct WebComponent {
    controller:WebController = new WebController();
L
laosan_ted 已提交
1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103
    responseweb: WebResourceResponse = new WebResourceResponse();
    heads:Header[] = new Array();
    @State webdata: string = "<!DOCTYPE html>\n" +
    "<html>\n"+
    "<head>\n"+
    "<title>intercept test</title>\n"+
    "</head>\n"+
    "<body>\n"+
    "<h1>intercept test</h1>\n"+
    "</body>\n"+
    "</html>"
L
update  
laosan_ted 已提交
1104 1105
    build() {
      Column() {
Z
zhou-liting125 已提交
1106
        Web({ src:'www.example.com', controller:this.controller })
L
laosan_ted 已提交
1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126
          .onInterceptRequest((event) => {
            console.log('url:' + event.request.getRequestUrl());
            var head1:Header = {
              headerKey:"Connection",
              headerValue:"keep-alive"
            }
            var head2:Header = {
              headerKey:"Cache-Control",
              headerValue:"no-cache"
            }
            var length = this.heads.push(head1);
            length = this.heads.push(head2);
            this.responseweb.setResponseHeader(this.heads);
            this.responseweb.setResponseData(this.webdata);
            this.responseweb.setResponseEncoding('utf-8');
            this.responseweb.setResponseMimeType('text/html');
            this.responseweb.setResponseCode(200);
            this.responseweb.setReasonMessage('OK');
            return this.responseweb;
          })
L
update  
laosan_ted 已提交
1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137
      }
    }
  }
  ```

### onHttpAuthRequest<sup>9+</sup>

onHttpAuthRequest(callback: (event?: { handler: HttpAuthHandler, host: string, realm: string}) => boolean)

通知收到http auth认证请求。

Z
zhou-liting125 已提交
1138
**参数:**
1139 1140 1141 1142 1143
| 参数名     | 参数类型                                 | 参数描述             |
| ------- | ------------------------------------ | ---------------- |
| handler | [HttpAuthHandler](#httpauthhandler9) | 通知Web组件用户操作行为。   |
| host    | string                               | HTTP身份验证凭据应用的主机。 |
| realm   | string                               | HTTP身份验证凭据应用的域。  |
L
update  
laosan_ted 已提交
1144

Z
zhou-liting125 已提交
1145
**返回值:**
1146 1147 1148
| 类型      | 说明                    |
| ------- | --------------------- |
| boolean | 返回false表示此次认证失败,否则成功。 |
L
update  
laosan_ted 已提交
1149

Z
zhou-liting125 已提交
1150
  **示例:**
Z
zhou-liting125 已提交
1151 1152
  ```ts
  // xxx.ets
Z
zhou-liting125 已提交
1153
  @Entry
L
update  
laosan_ted 已提交
1154 1155 1156
  @Component
  struct WebComponent {
    controller:WebController = new WebController();
Z
zhou-liting125 已提交
1157
    httpAuth:boolean = false;
L
update  
laosan_ted 已提交
1158 1159
    build() {
      Column() {
Z
zhou-liting125 已提交
1160
        Web({ src:'www.example.com', controller:this.controller })
L
update  
laosan_ted 已提交
1161 1162 1163 1164 1165 1166 1167
        .onHttpAuthRequest((event) => {
          AlertDialog.show({
            title: 'title',
            message: 'text',
            confirm: {
              value: 'onConfirm',
              action: () => {
Z
zhou-liting125 已提交
1168
                this.httpAuth = event.handler.isHttpAuthInfoSaved();
L
update  
laosan_ted 已提交
1169 1170
                if (this.httpAuth == false) {
                  web.WebDataBase.saveHttpAuthCredentials(
Z
zhou-liting125 已提交
1171 1172 1173 1174
                   event.host,
                   event.realm,
                   "2222",
                   "2222"
L
update  
laosan_ted 已提交
1175
                  )
Z
zhou-liting125 已提交
1176
                  event.handler.cancel();
L
update  
laosan_ted 已提交
1177 1178 1179 1180
                }
              }
            },
            cancel: () => {
Z
zhou-liting125 已提交
1181
             event.handler.cancel();
L
update  
laosan_ted 已提交
1182 1183 1184 1185 1186 1187 1188
            }
          })
          return true;
        })
    }
  }
  ```
1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227
### onPermissionRequest<sup>9+</sup>

onPermissionRequest(callback: (event?: { request: PermissionRequest }) => void)

通知收到获取权限请求。

**参数:**
| 参数名     | 参数类型                                 | 参数描述             |
| ------- | ------------------------------------ | ---------------- |
| request | [PermissionRequest](#permissionrequest9) | 通知Web组件用户操作行为。   |

  **示例:**
  ```ts
  // xxx.ets
  @Entry
  @Component
  struct WebComponent {
    controller:WebController = new WebController();
    build() {
      Column() {
        Web({ src:'www.example.com', controller:this.controller })
        .onPermissionRequest((event) => {
          AlertDialog.show({
            title: 'title',
            message: 'text',
            confirm: {
              value: 'onConfirm',
              action: () => {
                event.request.grant(event.request.getAccessibleResource());
              }
            },
            cancel: () => {
             event.request.deny();
            }
          })
        })
    }
  }
  ```
1228

Z
zhou-liting125 已提交
1229
## ConsoleMessage
1230

L
update  
laosan_ted 已提交
1231
Web组件获取控制台信息对象。示例代码参考[onConsole事件](#onconsole)
1232

Z
zhou-liting125 已提交
1233
### getLineNumber
1234

Z
zhou-liting125 已提交
1235 1236 1237 1238
getLineNumber(): number

获取ConsoleMessage的行数。

Z
zhou-liting125 已提交
1239
**返回值:** 
1240 1241 1242
| 类型     | 说明                   |
| ------ | -------------------- |
| number | 返回ConsoleMessage的行数。 |
Z
zhou-liting125 已提交
1243 1244 1245 1246 1247 1248 1249

### getMessage

getMessage(): string

获取ConsoleMessage的日志信息。

Z
zhou-liting125 已提交
1250
**返回值:** 
1251 1252 1253
| 类型     | 说明                     |
| ------ | ---------------------- |
| string | 返回ConsoleMessage的日志信息。 |
Z
zhou-liting125 已提交
1254 1255 1256 1257

### getMessageLevel

getMessageLevel(): MessageLevel
1258

Z
zhou-liting125 已提交
1259 1260
获取ConsoleMessage的信息级别。

Z
zhou-liting125 已提交
1261
**返回值:** 
1262 1263 1264
| 类型                                | 说明                     |
| --------------------------------- | ---------------------- |
| [MessageLevel](#messagelevel枚举说明) | 返回ConsoleMessage的信息级别。 |
Z
zhou-liting125 已提交
1265 1266 1267 1268 1269 1270 1271

### getSourceId

getSourceId(): string

获取网页源文件路径和名字。

Z
zhou-liting125 已提交
1272
**返回值:** 
1273 1274 1275
| 类型     | 说明            |
| ------ | ------------- |
| string | 返回网页源文件路径和名字。 |
Z
zhou-liting125 已提交
1276 1277

## JsResult
L
lixingchi1 已提交
1278

L
update  
laosan_ted 已提交
1279
Web组件返回的弹窗确认或弹窗取消功能对象。示例代码参考[onAlert事件](#onalert)
L
add web  
liujinwei 已提交
1280

Z
zhou-liting125 已提交
1281 1282 1283
### handleCancel

handleCancel(): void
L
add web  
liujinwei 已提交
1284

Z
zhou-liting125 已提交
1285
通知Web组件用户取消弹窗操作。
L
add web  
liujinwei 已提交
1286

Z
zhou-liting125 已提交
1287
### handleConfirm
L
add web  
liujinwei 已提交
1288

Z
zhou-liting125 已提交
1289
handleConfirm(): void
L
add web  
liujinwei 已提交
1290

Z
zhou-liting125 已提交
1291
通知Web组件用户确认弹窗操作。
L
add web  
liujinwei 已提交
1292

Z
zhou-liting125 已提交
1293 1294 1295 1296 1297 1298
### handlePromptConfirm<sup>9+</sup>

handlePromptConfirm(result: string): void

通知Web组件用户确认弹窗操作及对话框内容。

Z
zhou-liting125 已提交
1299
**参数:**
1300 1301 1302
| 参数名    | 参数类型   | 必填   | 默认值  | 参数描述        |
| ------ | ------ | ---- | ---- | ----------- |
| result | string | 是    | -    | 用户输入的对话框内容。 |
Z
zhou-liting125 已提交
1303 1304 1305

## WebResourceError

L
update  
laosan_ted 已提交
1306
web组件资源管理错误信息对象。示例代码参考[onErrorReceive事件](#onerrorreceive)
Z
zhou-liting125 已提交
1307 1308 1309 1310 1311 1312 1313

### getErrorCode

getErrorCode(): number

获取加载资源的错误码。

Z
zhou-liting125 已提交
1314
**返回值:** 
1315 1316 1317
| 类型     | 说明          |
| ------ | ----------- |
| number | 返回加载资源的错误码。 |
Z
zhou-liting125 已提交
1318 1319 1320 1321 1322 1323 1324

### getErrorInfo

getErrorInfo(): string

获取加载资源的错误信息。

Z
zhou-liting125 已提交
1325
**返回值:** 
1326 1327 1328
| 类型     | 说明           |
| ------ | ------------ |
| string | 返回加载资源的错误信息。 |
Z
zhou-liting125 已提交
1329 1330 1331

## WebResourceRequest

L
update  
laosan_ted 已提交
1332
web组件获取资源请求对象。示例代码参考[onErrorReceive事件](#onerrorreceive)
Z
zhou-liting125 已提交
1333 1334 1335 1336 1337 1338 1339

### getRequestHeader

getResponseHeader() : Array\<Header\>

获取资源请求头信息。

Z
zhou-liting125 已提交
1340
**返回值:** 
1341 1342 1343
| 类型                         | 说明         |
| -------------------------- | ---------- |
| Array\<[Header](#header)\> | 返回资源请求头信息。 |
Z
zhou-liting125 已提交
1344 1345 1346 1347 1348 1349 1350

### getRequestUrl

getRequestUrl(): string

获取资源请求的URL信息。

Z
zhou-liting125 已提交
1351
**返回值:** 
1352 1353 1354
| 类型     | 说明            |
| ------ | ------------- |
| string | 返回资源请求的URL信息。 |
Z
zhou-liting125 已提交
1355 1356 1357 1358 1359 1360 1361

### isMainFrame

isMainFrame(): boolean

判断资源请求是否为主frame。

Z
zhou-liting125 已提交
1362
**返回值:** 
1363 1364 1365
| 类型      | 说明               |
| ------- | ---------------- |
| boolean | 返回资源请求是否为主frame。 |
Z
zhou-liting125 已提交
1366 1367 1368 1369 1370 1371 1372

### isRedirect

isRedirect(): boolean

判断资源请求是否被服务端重定向。

Z
zhou-liting125 已提交
1373
**返回值:** 
1374 1375 1376
| 类型      | 说明               |
| ------- | ---------------- |
| boolean | 返回资源请求是否被服务端重定向。 |
Z
zhou-liting125 已提交
1377 1378 1379 1380 1381 1382 1383

### isRequestGesture

isRequestGesture(): boolean

获取资源请求是否与手势(如点击)相关联。

Z
zhou-liting125 已提交
1384
**返回值:** 
1385 1386 1387
| 类型      | 说明                   |
| ------- | -------------------- |
| boolean | 返回资源请求是否与手势(如点击)相关联。 |
Z
zhou-liting125 已提交
1388 1389

## Header
L
liwenzhen 已提交
1390 1391 1392

Web组件返回的请求/响应头对象。

1393 1394 1395 1396
| 名称          | 类型     | 描述            |
| ----------- | ------ | ------------- |
| headerKey   | string | 请求/响应头的key。   |
| headerValue | string | 请求/响应头的value。 |
L
liwenzhen 已提交
1397

L
add web  
liujinwei 已提交
1398

Z
zhou-liting125 已提交
1399
## WebResourceResponse
L
add web  
liujinwei 已提交
1400

Z
zhou-liting125 已提交
1401
web组件资源响应对象。示例代码参考[onHttpErrorReceive事件](#onhttperrorreceive)
L
add web  
liujinwei 已提交
1402

Z
zhou-liting125 已提交
1403
### getReasonMessage
T
Ted 已提交
1404

Z
zhou-liting125 已提交
1405
getReasonMessage(): string
T
Ted 已提交
1406

Z
zhou-liting125 已提交
1407
获取资源响应的状态码描述。
T
Ted 已提交
1408

Z
zhou-liting125 已提交
1409
**返回值:** 
1410 1411 1412
| 类型     | 说明            |
| ------ | ------------- |
| string | 返回资源响应的状态码描述。 |
T
Ted 已提交
1413

Z
zhou-liting125 已提交
1414
### getResponseCode
T
Ted 已提交
1415

Z
zhou-liting125 已提交
1416 1417 1418 1419
getResponseCode(): number

获取资源响应的状态码。

Z
zhou-liting125 已提交
1420
**返回值:** 
1421 1422 1423
| 类型     | 说明          |
| ------ | ----------- |
| number | 返回资源响应的状态码。 |
Z
zhou-liting125 已提交
1424 1425 1426 1427 1428 1429 1430

### getResponseData

getResponseData(): string

获取资源响应数据。

Z
zhou-liting125 已提交
1431
**返回值:** 
1432 1433 1434
| 类型     | 说明        |
| ------ | --------- |
| string | 返回资源响应数据。 |
Z
zhou-liting125 已提交
1435 1436 1437 1438 1439 1440 1441

### getResponseEncoding

getResponseEncoding(): string

获取资源响应的编码。

Z
zhou-liting125 已提交
1442
**返回值:** 
1443 1444 1445
| 类型     | 说明         |
| ------ | ---------- |
| string | 返回资源响应的编码。 |
Z
zhou-liting125 已提交
1446 1447 1448 1449 1450 1451 1452

### getResponseHeader

getResponseHeader() : Array\<Header\>

获取资源响应头。

Z
zhou-liting125 已提交
1453
**返回值:** 
1454 1455 1456
| 类型                         | 说明       |
| -------------------------- | -------- |
| Array\<[Header](#header)\> | 返回资源响应头。 |
Z
zhou-liting125 已提交
1457 1458 1459 1460 1461 1462 1463

### getResponseMimeType

getResponseMimeType(): string

获取资源响应的媒体(MIME)类型。

Z
zhou-liting125 已提交
1464
**返回值:** 
1465 1466 1467
| 类型     | 说明                 |
| ------ | ------------------ |
| string | 返回资源响应的媒体(MIME)类型。 |
Z
zhou-liting125 已提交
1468 1469 1470 1471 1472 1473 1474

### setResponseData<sup>9+</sup>

setResponseData(data: string)

设置资源响应数据。

Z
zhou-liting125 已提交
1475
**参数:**
1476 1477 1478
| 参数名  | 参数类型   | 必填   | 默认值  | 参数描述        |
| ---- | ------ | ---- | ---- | ----------- |
| data | string | 是    | -    | 要设置的资源响应数据。 |
Z
zhou-liting125 已提交
1479 1480 1481 1482 1483 1484 1485

### setResponseEncoding<sup>9+</sup>

setResponseEncoding(encoding: string)

设置资源响应的编码。

Z
zhou-liting125 已提交
1486
**参数:**
1487 1488 1489
| 参数名      | 参数类型   | 必填   | 默认值  | 参数描述         |
| -------- | ------ | ---- | ---- | ------------ |
| encoding | string | 是    | -    | 要设置的资源响应的编码。 |
Z
zhou-liting125 已提交
1490 1491 1492 1493 1494 1495 1496

### setResponseMimeType<sup>9+</sup>

setResponseMimeType(mimeType: string)

设置资源响应的媒体(MIME)类型。

Z
zhou-liting125 已提交
1497
**参数:**
1498 1499 1500
| 参数名      | 参数类型   | 必填   | 默认值  | 参数描述                 |
| -------- | ------ | ---- | ---- | -------------------- |
| mimeType | string | 是    | -    | 要设置的资源响应的媒体(MIME)类型。 |
Z
zhou-liting125 已提交
1501 1502 1503 1504 1505 1506 1507

### setReasonMessage<sup>9+</sup>

setReasonMessage(reason: string)

设置资源响应的状态码描述。

Z
zhou-liting125 已提交
1508
**参数:**
1509 1510 1511
| 参数名    | 参数类型   | 必填   | 默认值  | 参数描述            |
| ------ | ------ | ---- | ---- | --------------- |
| reason | string | 是    | -    | 要设置的资源响应的状态码描述。 |
Z
zhou-liting125 已提交
1512 1513 1514 1515 1516 1517 1518

### setResponseHeader<sup>9+</sup>

setResponseHeader(header: Array\<Header\>)

设置资源响应头。

Z
zhou-liting125 已提交
1519
**参数:**
1520 1521 1522
| 参数名    | 参数类型                       | 必填   | 默认值  | 参数描述       |
| ------ | -------------------------- | ---- | ---- | ---------- |
| header | Array\<[Header](#header)\> | 是    | -    | 要设置的资源响应头。 |
Z
zhou-liting125 已提交
1523 1524 1525 1526 1527 1528 1529

### setResponseCode<sup>9+</sup>

setResponseCode(code: number)

设置资源响应的状态码。

Z
zhou-liting125 已提交
1530
**参数:**
1531 1532 1533
| 参数名  | 参数类型   | 必填   | 默认值  | 参数描述          |
| ---- | ------ | ---- | ---- | ------------- |
| code | number | 是    | -    | 要设置的资源响应的状态码。 |
Z
zhou-liting125 已提交
1534 1535

## FileSelectorResult<sup>9+</sup>
T
Ted 已提交
1536

Z
zhou-liting125 已提交
1537
通知Web组件的文件选择结果。示例代码参考[onShowFileSelector事件](#onshowfileselector9)
T
Ted 已提交
1538

Z
zhou-liting125 已提交
1539
### handleFileList<sup>9+</sup>
1540

Z
zhou-liting125 已提交
1541
handleFileList(fileList: Array\<string\>): void
1542

Z
zhou-liting125 已提交
1543
通知Web组件进行文件选择操作。
T
Ted 已提交
1544

Z
zhou-liting125 已提交
1545
**参数:**
1546 1547 1548
| 参数名      | 参数类型            | 必填   | 默认值  | 参数描述         |
| -------- | --------------- | ---- | ---- | ------------ |
| fileList | Array\<string\> | 是    | -    | 需要进行操作的文件列表。 |
1549

Z
zhou-liting125 已提交
1550 1551
## FileSelectorParam<sup>9+</sup>

Z
zhou-liting125 已提交
1552
web组件获取文件对象。示例代码参考[onShowFileSelector事件](#onshowfileselector9)
Z
zhou-liting125 已提交
1553 1554 1555 1556 1557 1558 1559

### getTitle<sup>9+</sup>

getTitle(): string

获取文件选择器标题。

Z
zhou-liting125 已提交
1560
**返回值:** 
1561 1562 1563
| 类型     | 说明         |
| ------ | ---------- |
| string | 返回文件选择器标题。 |
Z
zhou-liting125 已提交
1564 1565 1566 1567 1568 1569 1570

### getMode<sup>9+</sup>

getMode(): FileSelectorMode

获取文件选择器的模式。

Z
zhou-liting125 已提交
1571
**返回值:** 
1572 1573 1574
| 类型                                       | 说明          |
| ---------------------------------------- | ----------- |
| [FileSelectorMode](#fileselectormode枚举说明) | 返回文件选择器的模式。 |
Z
zhou-liting125 已提交
1575 1576 1577 1578 1579 1580 1581

### getAcceptType<sup>9+</sup>

getAcceptType(): Array\<string\>

获取文件过滤类型。

Z
zhou-liting125 已提交
1582
**返回值:** 
1583 1584 1585
| 类型              | 说明        |
| --------------- | --------- |
| Array\<string\> | 返回文件过滤类型。 |
Z
zhou-liting125 已提交
1586 1587 1588 1589 1590 1591 1592

### isCapture<sup>9+</sup>

isCapture(): boolean

获取是否调用多媒体能力。

Z
zhou-liting125 已提交
1593
**返回值:** 
1594 1595 1596
| 类型      | 说明           |
| ------- | ------------ |
| boolean | 返回是否调用多媒体能力。 |
1597 1598 1599

## HttpAuthHandler<sup>9+</sup>

Z
zhou-liting125 已提交
1600
Web组件返回的http auth认证请求确认或取消和使用缓存密码认证功能对象。示例代码参考[onHttpAuthRequest事件](#onhttpauthrequest9)
1601 1602 1603 1604 1605 1606 1607 1608

### cancel<sup>9+</sup>

cancel(): void

通知Web组件用户取消HTTP认证操作。

### confirm<sup>9+</sup>
1609

1610 1611 1612 1613
confirm(userName: string, pwd: string): boolean

使用用户名和密码进行HTTP认证操作。

Z
zhou-liting125 已提交
1614
**参数:**
1615

1616 1617 1618 1619
| 参数名      | 参数类型   | 必填   | 默认值  | 参数描述       |
| -------- | ------ | ---- | ---- | ---------- |
| userName | string | 是    | -    | HTTP认证用户名。 |
| pwd      | string | 是    | -    | HTTP认证密码。  |
1620

Z
zhou-liting125 已提交
1621
**返回值:**
1622 1623 1624
| 类型      | 说明                    |
| ------- | --------------------- |
| boolean | 认证成功返回true,失败返回false。 |
1625 1626 1627 1628 1629 1630 1631

### isHttpAuthInfoSaved<sup>9+</sup>

isHttpAuthInfoSaved(): boolean

通知Web组件用户使用服务器缓存的账号密码认证。

Z
zhou-liting125 已提交
1632
**返回值:**
1633 1634 1635
| 类型      | 说明                        |
| ------- | ------------------------- |
| boolean | 存在密码认证成功返回true,其他返回false。 |
1636

1637 1638
## PermissionRequest<sup>9+</sup>

X
xiongjun_gitee 已提交
1639
Web组件返回授权或拒绝权限功能的对象。示例代码参考[onPermissionRequest事件](#onpermissionrequest9)
1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662

### deny<sup>9+</sup>

deny(): void

拒绝网页所请求的权限。

### getOrigin<sup>9+</sup>

getOrigin(): string

获取网页来源。

**返回值:**

| 类型      | 说明                    |
| ------- | --------------------- |
| string  | 当前请求权限网页的来源。 |

### getAccessibleResource<sup>9+</sup>

getAccessibleResource(): Array\<string\>

X
xiongjun_gitee 已提交
1663
获取网页所请求的权限资源列表,资源列表类型参考[ProtectedResourceType](#protectedresourcetype9枚举说明)
1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674

**返回值:**

| 类型            | 说明                     |
| --------------- | ----------------------- |
| Array\<string\> | 网页所请求的权限资源列表。 |

### grant<sup>9+</sup>

grant(resources: Array\<string\>): void

X
xiongjun_gitee 已提交
1675
对网页访问的给定权限进行授权。
1676 1677 1678 1679 1680 1681 1682

**参数:**

| 参数名     | 参数类型        | 必填 | 默认值 | 参数描述                |
| --------- | --------------- | ---- | ----- | ---------------------- |
| resources | Array\<string\> | 是   | -     | 网页所请求的权限资源列表。|

Z
zengyawen 已提交
1683 1684
## WebController

L
liwenzhen 已提交
1685
通过WebController可以控制Web组件各种行为。一个WebController对象只能控制一个Web组件,且必须在Web组件和WebController绑定后,才能调用WebController上的方法。
Z
zengyawen 已提交
1686 1687 1688 1689 1690 1691 1692

### 创建对象

```
webController: WebController = new WebController()
```

L
lixingchi1 已提交
1693 1694 1695 1696
### accessBackward

accessBackward(): boolean

L
liwenzhen 已提交
1697
当前页面是否可后退,即当前页面是否有返回历史记录。
L
lixingchi1 已提交
1698

Z
zhou-liting125 已提交
1699
**返回值:**
1700 1701 1702
| 类型      | 说明                    |
| ------- | --------------------- |
| boolean | 可以后退返回true,否则返回false。 |
L
update  
laosan_ted 已提交
1703

Z
zhou-liting125 已提交
1704
**示例:**
Z
zhou-liting125 已提交
1705 1706
  ```ts
  // xxx.ets
Z
zhou-liting125 已提交
1707
  @Entry
L
update  
laosan_ted 已提交
1708 1709
  @Component
  struct WebComponent {
L
laosan_ted 已提交
1710 1711
    controller: WebController = new WebController();
  
L
update  
laosan_ted 已提交
1712 1713 1714
    build() {
      Column() {
        Button('accessBackward')
L
laosan_ted 已提交
1715 1716 1717 1718
          .onClick(() => {
            let result = this.controller.accessBackward();
            console.log('result:' + result);
          })
Z
zhou-liting125 已提交
1719
        Web({ src: 'www.example.com', controller: this.controller })
L
laosan_ted 已提交
1720
      }
L
update  
laosan_ted 已提交
1721 1722 1723 1724
    }
  }
  ```

L
lixingchi1 已提交
1725 1726 1727 1728
### accessForward

accessForward(): boolean

L
liwenzhen 已提交
1729
当前页面是否可前进,即当前页面是否有前进历史记录。
L
lixingchi1 已提交
1730

Z
zhou-liting125 已提交
1731
**返回值:**
1732 1733 1734
| 类型      | 说明                    |
| ------- | --------------------- |
| boolean | 可以前进返回true,否则返回false。 |
L
update  
laosan_ted 已提交
1735

Z
zhou-liting125 已提交
1736
**示例:**
Z
zhou-liting125 已提交
1737 1738
  ```ts
  // xxx.ets
Z
zhou-liting125 已提交
1739
  @Entry
L
update  
laosan_ted 已提交
1740 1741
  @Component
  struct WebComponent {
L
laosan_ted 已提交
1742 1743
    controller: WebController = new WebController();
  
L
update  
laosan_ted 已提交
1744 1745 1746
    build() {
      Column() {
        Button('accessForward')
L
laosan_ted 已提交
1747 1748 1749 1750
          .onClick(() => {
            let result = this.controller.accessForward();
            console.log('result:' + result);
          })
Z
zhou-liting125 已提交
1751
        Web({ src: 'www.example.com', controller: this.controller })
L
laosan_ted 已提交
1752
      }
L
update  
laosan_ted 已提交
1753 1754 1755 1756
    }
  }
  ```

L
lixingchi1 已提交
1757
### accessStep
Z
zengyawen 已提交
1758

L
lixingchi1 已提交
1759
accessStep(step: number): boolean
Z
zengyawen 已提交
1760

L
liwenzhen 已提交
1761
当前页面是否可前进或者后退给定的step步。
Z
zengyawen 已提交
1762

Z
zhou-liting125 已提交
1763
**参数:**
Z
zengyawen 已提交
1764

1765 1766 1767
| 参数名  | 参数类型   | 必填   | 默认值  | 参数描述                  |
| ---- | ------ | ---- | ---- | --------------------- |
| step | number | 是    | -    | 要跳转的步数,正数代表前进,负数代表后退。 |
L
lixingchi1 已提交
1768

Z
zhou-liting125 已提交
1769
**返回值:**
1770 1771 1772
| 类型      | 说明        |
| ------- | --------- |
| boolean | 页面是否前进或后退 |
1773

Z
zhou-liting125 已提交
1774
**示例:**
Z
zhou-liting125 已提交
1775 1776
  ```ts
  // xxx.ets
Z
zhou-liting125 已提交
1777
  @Entry
L
update  
laosan_ted 已提交
1778 1779
  @Component
  struct WebComponent {
L
laosan_ted 已提交
1780
    controller: WebController = new WebController();
L
update  
laosan_ted 已提交
1781
    @State steps: number = 2;
L
laosan_ted 已提交
1782
  
L
update  
laosan_ted 已提交
1783 1784 1785
    build() {
      Column() {
        Button('accessStep')
L
laosan_ted 已提交
1786 1787 1788 1789
          .onClick(() => {
            let result = this.controller.accessStep(this.steps);
            console.log('result:' + result);
          })
Z
zhou-liting125 已提交
1790
        Web({ src: 'www.example.com', controller: this.controller })
L
laosan_ted 已提交
1791
      }
L
update  
laosan_ted 已提交
1792 1793 1794 1795
    }
  }
  ```

L
lixingchi1 已提交
1796 1797
### backward

L
update  
laosan_ted 已提交
1798
backward(): void
L
lixingchi1 已提交
1799

L
liwenzhen 已提交
1800
按照历史栈,后退一个页面。一般结合accessBackward一起使用。
L
lixingchi1 已提交
1801

Z
zhou-liting125 已提交
1802
**示例:**
Z
zhou-liting125 已提交
1803 1804
  ```ts
  // xxx.ets
Z
zhou-liting125 已提交
1805
  @Entry
L
update  
laosan_ted 已提交
1806 1807
  @Component
  struct WebComponent {
L
laosan_ted 已提交
1808 1809
    controller: WebController = new WebController();
  
L
update  
laosan_ted 已提交
1810 1811 1812
    build() {
      Column() {
        Button('backward')
L
laosan_ted 已提交
1813 1814 1815
          .onClick(() => {
            this.controller.backward();
          })
Z
zhou-liting125 已提交
1816
        Web({ src: 'www.example.com', controller: this.controller })
L
laosan_ted 已提交
1817
      }
L
update  
laosan_ted 已提交
1818 1819 1820
    }
  }
  ```
L
lixingchi1 已提交
1821 1822 1823

### forward

L
update  
laosan_ted 已提交
1824
forward(): void
L
lixingchi1 已提交
1825

L
liwenzhen 已提交
1826
按照历史栈,前进一个页面。一般结合accessForward一起使用。
L
lixingchi1 已提交
1827

Z
zhou-liting125 已提交
1828
**示例:**
Z
zhou-liting125 已提交
1829 1830
  ```ts
  // xxx.ets
Z
zhou-liting125 已提交
1831
  @Entry
L
update  
laosan_ted 已提交
1832 1833
  @Component
  struct WebComponent {
L
laosan_ted 已提交
1834 1835
    controller: WebController = new WebController();
  
L
update  
laosan_ted 已提交
1836 1837 1838
    build() {
      Column() {
        Button('forward')
L
laosan_ted 已提交
1839 1840 1841
          .onClick(() => {
            this.controller.forward();
          })
Z
zhou-liting125 已提交
1842
        Web({ src: 'www.example.com', controller: this.controller })
L
laosan_ted 已提交
1843
      }
L
update  
laosan_ted 已提交
1844 1845 1846 1847
    }
  }
  ```

1848 1849 1850 1851 1852 1853
### backOrForward<sup>9+</sup>

backOrForward(step: number): void

按照历史栈,前进或者后退指定步长的页面,当历史栈中不存在对应步长的页面时,不会进行页面跳转。

Z
zhou-liting125 已提交
1854
**参数:**
1855 1856 1857
| 参数名  | 参数类型   | 必填   | 默认值  | 参数描述        |
| ---- | ------ | ---- | ---- | ----------- |
| step | number | 是    | -    | 需要前进或后退的步长。 |
1858

Z
zhou-liting125 已提交
1859
**示例:**
Z
zhou-liting125 已提交
1860 1861
  ```ts
  // xxx.ets
Z
zhou-liting125 已提交
1862
  @Entry
L
update  
laosan_ted 已提交
1863 1864 1865 1866 1867 1868 1869 1870 1871
  @Component
  struct WebComponent {
    controller:WebController = new WebController();
    build() {
      Column() {
        Button('backOrForward')
        .onClick(() => {
          this.controller.backOrForward();
        })
Z
zhou-liting125 已提交
1872
        Web({ src:'www.example.com', controller:this.controller })
L
update  
laosan_ted 已提交
1873 1874 1875 1876 1877 1878 1879 1880 1881 1882
    }
  }
  ```

### deleteJavaScriptRegister

deleteJavaScriptRegister(name: string)

删除通过registerJavaScriptProxy注册到window上的指定name的应用侧JavaScript对象。

Z
zhou-liting125 已提交
1883
**参数:**
1884 1885 1886
| 参数名  | 参数类型   | 必填   | 默认值  | 参数描述                                     |
| ---- | ------ | ---- | ---- | ---------------------------------------- |
| name | string | 是    | -    | 注册对象的名称,可在网页侧JavaScript中通过此名称调用应用侧JavaScript对象。 |
L
update  
laosan_ted 已提交
1887

Z
zhou-liting125 已提交
1888
**示例:**
Z
zhou-liting125 已提交
1889 1890
  ```ts
  // xxx.ets
Z
zhou-liting125 已提交
1891
  @Entry
L
update  
laosan_ted 已提交
1892 1893
  @Component
  struct WebComponent {
L
laosan_ted 已提交
1894
    controller: WebController = new WebController();
L
update  
laosan_ted 已提交
1895
    @State name: string = 'Object';
L
laosan_ted 已提交
1896
  
L
update  
laosan_ted 已提交
1897 1898 1899
    build() {
      Column() {
        Button('deleteJavaScriptRegister')
L
laosan_ted 已提交
1900 1901 1902
          .onClick(() => {
            this.controller.deleteJavaScriptRegister(this.name);
          })
Z
zhou-liting125 已提交
1903
        Web({ src: 'www.example.com', controller: this.controller })
L
laosan_ted 已提交
1904
      }
L
update  
laosan_ted 已提交
1905 1906 1907 1908
    }
  }
  ```

L
lixingchi1 已提交
1909 1910 1911 1912
### getHitTest

getHitTest(): HitTestType

L
liwenzhen 已提交
1913
获取当前被点击区域的元素类型。	
L
lixingchi1 已提交
1914

Z
zhou-liting125 已提交
1915
**返回值:**
1916 1917 1918
| 类型                              | 说明          |
| ------------------------------- | ----------- |
| [HitTestType](#hittesttype枚举说明) | 被点击区域的元素类型。 |
L
lixingchi1 已提交
1919

Z
zhou-liting125 已提交
1920
**示例:**
Z
zhou-liting125 已提交
1921 1922
  ```ts
  // xxx.ets
Z
zhou-liting125 已提交
1923
  @Entry
L
update  
laosan_ted 已提交
1924 1925
  @Component
  struct WebComponent {
L
laosan_ted 已提交
1926 1927
    controller: WebController = new WebController();
  
L
update  
laosan_ted 已提交
1928 1929 1930
    build() {
      Column() {
        Button('getHitTest')
L
laosan_ted 已提交
1931 1932 1933 1934
          .onClick(() => {
            let hitType = this.controller.getHitTest();
            console.log("hitType: " + hitType);
          })
Z
zhou-liting125 已提交
1935
        Web({ src: 'www.example.com', controller: this.controller })
L
laosan_ted 已提交
1936
      }
L
update  
laosan_ted 已提交
1937 1938 1939 1940
    }
  }
  ```

1941 1942 1943 1944 1945
### getHitTestValue<sup>9+</sup>
getHitTestValue(): HitTestValue

获取当前被点击区域的元素信息。

Z
zhou-liting125 已提交
1946
**返回值:**
1947 1948 1949
| 类型                             | 说明         |
| ------------------------------ | ---------- |
| [HitTestValue](#hittestvalue9) | 点击区域的元素信息。 |
1950

Z
zhou-liting125 已提交
1951
**示例:**
Z
zhou-liting125 已提交
1952 1953
  ```ts
  // xxx.ets
Z
zhou-liting125 已提交
1954
  @Entry
L
update  
laosan_ted 已提交
1955 1956
  @Component
  struct WebComponent {
L
laosan_ted 已提交
1957 1958
    controller: WebController = new WebController();
  
L
update  
laosan_ted 已提交
1959 1960 1961
    build() {
      Column() {
        Button('getHitTestValue')
L
laosan_ted 已提交
1962 1963 1964 1965 1966
          .onClick(() => {
            let hitValue = this.controller.getHitTestValue();
            console.log("hitType: " + hitValue.getType());
            console.log("extra: " + hitValue.getExtra());
          })
Z
zhou-liting125 已提交
1967
        Web({ src: 'www.example.com', controller: this.controller })
L
laosan_ted 已提交
1968
      }
L
update  
laosan_ted 已提交
1969 1970 1971 1972
    }
  }
  ```

1973 1974 1975 1976 1977
### getWebId<sup>9+</sup>
getWebId(): number

获取当前Web组件的索引值,用于多个Web组件的管理。

Z
zhou-liting125 已提交
1978
**返回值:**
1979 1980 1981
| 类型     | 说明           |
| ------ | ------------ |
| number | 当前Web组件的索引值。 |
1982

Z
zhou-liting125 已提交
1983
**示例:**
Z
zhou-liting125 已提交
1984 1985
  ```ts
  // xxx.ets
Z
zhou-liting125 已提交
1986
  @Entry
L
update  
laosan_ted 已提交
1987 1988
  @Component
  struct WebComponent {
L
laosan_ted 已提交
1989 1990
    controller: WebController = new WebController();
  
L
update  
laosan_ted 已提交
1991 1992 1993
    build() {
      Column() {
        Button('getWebId')
L
laosan_ted 已提交
1994 1995 1996 1997
          .onClick(() => {
            let id = this.controller.getWebId();
            console.log("id: " + id);
          })
Z
zhou-liting125 已提交
1998
        Web({ src: 'www.example.com', controller: this.controller })
L
laosan_ted 已提交
1999
      }
L
update  
laosan_ted 已提交
2000 2001 2002 2003
    }
  }
  ```

2004 2005 2006 2007
### getTitle<sup>9+</sup>
getTitle(): string

获取当前网页的标题。
Z
zhou-liting125 已提交
2008 2009

**返回值:**
2010 2011 2012
| 类型     | 说明       |
| ------ | -------- |
| string | 当前网页的标题。 |
2013

Z
zhou-liting125 已提交
2014
**示例:**
Z
zhou-liting125 已提交
2015 2016
  ```ts
  // xxx.ets
Z
zhou-liting125 已提交
2017
  @Entry
L
update  
laosan_ted 已提交
2018 2019
  @Component
  struct WebComponent {
L
laosan_ted 已提交
2020 2021
    controller: WebController = new WebController();
  
L
update  
laosan_ted 已提交
2022 2023 2024
    build() {
      Column() {
        Button('getTitle')
L
laosan_ted 已提交
2025 2026 2027 2028
          .onClick(() => {
            let title = this.controller.getTitle();
            console.log("title: " + title);
          })
Z
zhou-liting125 已提交
2029
        Web({ src: 'www.example.com', controller: this.controller })
L
laosan_ted 已提交
2030
      }
L
update  
laosan_ted 已提交
2031 2032 2033 2034
    }
  }
  ```

2035 2036 2037 2038
### getPageHeight<sup>9+</sup>
getPageHeight(): number

获取当前网页的页面高度。
Z
zhou-liting125 已提交
2039 2040

**返回值:**
2041 2042 2043
| 类型     | 说明         |
| ------ | ---------- |
| number | 当前网页的页面高度。 |
2044

Z
zhou-liting125 已提交
2045
**示例:**
Z
zhou-liting125 已提交
2046 2047
  ```ts
  // xxx.ets
Z
zhou-liting125 已提交
2048
  @Entry
L
update  
laosan_ted 已提交
2049 2050
  @Component
  struct WebComponent {
L
laosan_ted 已提交
2051 2052
    controller: WebController = new WebController();
  
L
update  
laosan_ted 已提交
2053 2054 2055
    build() {
      Column() {
        Button('getPageHeight')
L
laosan_ted 已提交
2056 2057 2058 2059
          .onClick(() => {
            let pageHeight = this.controller.getPageHeight();
            console.log("pageHeight: " + pageHeight);
          })
Z
zhou-liting125 已提交
2060
        Web({ src: 'www.example.com', controller: this.controller })
L
laosan_ted 已提交
2061
      }
L
update  
laosan_ted 已提交
2062 2063 2064
    }
  }
  ```
2065 2066 2067 2068 2069

### getDefaultUserAgent<sup>9+</sup>
getDefaultUserAgent(): string

获取当前默认用户代理。
L
update  
laosan_ted 已提交
2070

Z
zhou-liting125 已提交
2071
**返回值:**
2072 2073 2074
| 类型     | 说明      |
| ------ | ------- |
| string | 默认用户代理。 |
L
lixingchi1 已提交
2075

Z
zhou-liting125 已提交
2076
**示例:**
Z
zhou-liting125 已提交
2077 2078
  ```ts
  // xxx.ets
Z
zhou-liting125 已提交
2079
  @Entry
L
update  
laosan_ted 已提交
2080 2081
  @Component
  struct WebComponent {
L
laosan_ted 已提交
2082 2083
    controller: WebController = new WebController();
  
L
update  
laosan_ted 已提交
2084 2085 2086
    build() {
      Column() {
        Button('getDefaultUserAgent')
L
laosan_ted 已提交
2087 2088 2089 2090
          .onClick(() => {
            let userAgent = this.controller.getDefaultUserAgent();
            console.log("userAgent: " + userAgent);
          })
Z
zhou-liting125 已提交
2091
        Web({ src: 'www.example.com', controller: this.controller })
L
laosan_ted 已提交
2092
      }
L
update  
laosan_ted 已提交
2093 2094 2095 2096
    }
  }
  ```

L
lixingchi1 已提交
2097 2098
### loadData

Z
zhou-liting125 已提交
2099
loadData(options: { data: string, mimeType: string, encoding: string, baseUrl?: string, historyUrl?: string })
L
lixingchi1 已提交
2100

L
liwenzhen 已提交
2101 2102
baseUrl为空时,通过”data“协议加载指定的一段字符串。

L
liwenzhen 已提交
2103
当baseUrl为”data“协议时,编码后的data字符串将被Web组件作为”data"协议加载。
L
liwenzhen 已提交
2104

L
liwenzhen 已提交
2105
当baseUrl为“http/https"协议时,编码后的data字符串将被Web组件以类似loadUrl的方式以非编码字符串处理。
L
lixingchi1 已提交
2106

Z
zhou-liting125 已提交
2107
**参数:**
2108 2109 2110 2111 2112 2113 2114
| 参数名        | 参数类型   | 必填   | 默认值  | 参数描述                                     |
| ---------- | ------ | ---- | ---- | ---------------------------------------- |
| data       | string | 是    | -    | 按照”Base64“或者”URL"编码后的一段字符串。              |
| mimeType   | string | 是    | -    | 媒体类型(MIME)。                              |
| encoding   | string | 是    | -    | 编码类型,具体为“Base64"或者”URL编码。                |
| baseUrl    | string | 否    | -    | 指定的一个URL路径(“http”/“https”/"data"协议),并由Web组件赋值给window.origin。 |
| historyUrl | string | 否    | -    | 历史记录URL。非空时,可被历史记录管理,实现前后后退功能。当baseUrl为空时,此属性无效。 |
Z
zengyawen 已提交
2115

Z
zhou-liting125 已提交
2116
**示例:**
Z
zhou-liting125 已提交
2117 2118
  ```ts
  // xxx.ets
Z
zhou-liting125 已提交
2119
  @Entry
L
update  
laosan_ted 已提交
2120 2121
  @Component
  struct WebComponent {
L
laosan_ted 已提交
2122 2123
    controller: WebController = new WebController();
  
L
update  
laosan_ted 已提交
2124 2125 2126
    build() {
      Column() {
        Button('loadData')
L
laosan_ted 已提交
2127 2128 2129 2130 2131 2132 2133
          .onClick(() => {
            this.controller.loadData({
              data: "<html><body bgcolor=\"white\">Source:<pre>source</pre></body></html>",
              mimeType: "text/html",
              encoding: "UTF-8"
            });
          })
Z
zhou-liting125 已提交
2134
        Web({ src: 'www.example.com', controller: this.controller })
L
laosan_ted 已提交
2135
      }
L
update  
laosan_ted 已提交
2136 2137 2138 2139
    }
  }
  ```

Z
zengyawen 已提交
2140 2141
### loadUrl

Z
zhou-liting125 已提交
2142
loadUrl(options: { url: string | Resource, headers?: Array\<Header\> })
L
liwenzhen 已提交
2143 2144

使用指定的http头加载指定的URL。
Z
zengyawen 已提交
2145

L
liwenzhen 已提交
2146 2147 2148
通过loadUrl注入的对象只在当前document有效,即通过loadUrl导航到新的页面会无效。

而通过registerJavaScriptProxy注入的对象,在loadUrl导航到新的页面也会有效。
Z
zengyawen 已提交
2149

Z
zhou-liting125 已提交
2150
**参数:**
2151 2152 2153 2154
| 参数名     | 参数类型                       | 必填   | 默认值  | 参数描述           |
| ------- | -------------------------- | ---- | ---- | -------------- |
| url     | string                     | 是    | -    | 需要加载的 URL。     |
| headers | Array\<[Header](#header)\> | 否    | []   | URL的附加HTTP请求头。 |
L
lixingchi1 已提交
2155

Z
zhou-liting125 已提交
2156
**示例:**
Z
zhou-liting125 已提交
2157 2158
  ```ts
  // xxx.ets
Z
zhou-liting125 已提交
2159
  @Entry
L
update  
laosan_ted 已提交
2160 2161
  @Component
  struct WebComponent {
L
laosan_ted 已提交
2162 2163
    controller: WebController = new WebController();
  
L
update  
laosan_ted 已提交
2164 2165 2166
    build() {
      Column() {
        Button('loadUrl')
L
laosan_ted 已提交
2167
          .onClick(() => {
Z
zhou-liting125 已提交
2168
            this.controller.loadUrl({ url: 'www.example.com' });
L
laosan_ted 已提交
2169
          })
Z
zhou-liting125 已提交
2170
        Web({ src: 'www.example.com', controller: this.controller })
L
laosan_ted 已提交
2171
      }
L
update  
laosan_ted 已提交
2172 2173 2174 2175
    }
  }
  ```

L
lixingchi1 已提交
2176 2177 2178 2179
### onActive

onActive(): void

L
liwenzhen 已提交
2180
调用此接口通知Web组件进入前台激活状态。
L
lixingchi1 已提交
2181

Z
zhou-liting125 已提交
2182
**示例:**
Z
zhou-liting125 已提交
2183 2184
  ```ts
  // xxx.ets
Z
zhou-liting125 已提交
2185
  @Entry
L
update  
laosan_ted 已提交
2186 2187
  @Component
  struct WebComponent {
L
laosan_ted 已提交
2188 2189
    controller: WebController = new WebController();
  
L
update  
laosan_ted 已提交
2190 2191 2192
    build() {
      Column() {
        Button('onActive')
L
laosan_ted 已提交
2193 2194 2195
          .onClick(() => {
            this.controller.onActive();
          })
Z
zhou-liting125 已提交
2196
        Web({ src: 'www.example.com', controller: this.controller })
L
laosan_ted 已提交
2197
      }
L
update  
laosan_ted 已提交
2198 2199 2200 2201
    }
  }
  ```

L
lixingchi1 已提交
2202 2203 2204 2205
### onInactive

onInactive(): void

L
liwenzhen 已提交
2206
调用此接口通知Web组件进入未激活状态。
L
lixingchi1 已提交
2207

Z
zhou-liting125 已提交
2208
**示例:**
Z
zhou-liting125 已提交
2209 2210
  ```ts
  // xxx.ets
Z
zhou-liting125 已提交
2211
  @Entry
L
update  
laosan_ted 已提交
2212 2213
  @Component
  struct WebComponent {
L
laosan_ted 已提交
2214 2215
    controller: WebController = new WebController();
  
L
update  
laosan_ted 已提交
2216 2217 2218
    build() {
      Column() {
        Button('onInactive')
L
laosan_ted 已提交
2219 2220 2221
          .onClick(() => {
            this.controller.onInactive();
          })
Z
zhou-liting125 已提交
2222
        Web({ src: 'www.example.com', controller: this.controller })
L
laosan_ted 已提交
2223
      }
L
update  
laosan_ted 已提交
2224 2225 2226 2227
    }
  }
  ```

2228 2229 2230 2231
### zoom
zoom(factor: number): void

调整当前网页的缩放比例。
L
update  
laosan_ted 已提交
2232

Z
zhou-liting125 已提交
2233
**参数:**
2234 2235 2236
| 参数名    | 参数类型   | 必填   | 参数描述                           |
| ------ | ------ | ---- | ------------------------------ |
| factor | number | 是    | 基于当前网页所需调整的相对缩放比例,正值为放大,负值为缩小。 |
2237

Z
zhou-liting125 已提交
2238
**示例:**
Z
zhou-liting125 已提交
2239 2240
  ```ts
  // xxx.ets
Z
zhou-liting125 已提交
2241
  @Entry
L
update  
laosan_ted 已提交
2242 2243
  @Component
  struct WebComponent {
L
laosan_ted 已提交
2244
    controller: WebController = new WebController();
L
update  
laosan_ted 已提交
2245
    @State factor: number = 1;
L
laosan_ted 已提交
2246
  
L
update  
laosan_ted 已提交
2247 2248 2249
    build() {
      Column() {
        Button('zoom')
L
laosan_ted 已提交
2250 2251 2252
          .onClick(() => {
            this.controller.zoom(this.factor);
          })
Z
zhou-liting125 已提交
2253
        Web({ src: 'www.example.com', controller: this.controller })
L
laosan_ted 已提交
2254
      }
L
update  
laosan_ted 已提交
2255 2256 2257 2258
    }
  }
  ```

2259 2260 2261 2262
### zoomIn<sup>9+</sup>
zoomIn(): boolean

调用此接口将当前网页进行放大,比列20%。
Z
zhou-liting125 已提交
2263 2264

**返回值:**
2265 2266 2267
| 类型      | 说明          |
| ------- | ----------- |
| boolean | 放大操作是否成功执行。 |
2268

Z
zhou-liting125 已提交
2269
**示例:**
Z
zhou-liting125 已提交
2270 2271
  ```ts
  // xxx.ets
Z
zhou-liting125 已提交
2272
  @Entry
L
update  
laosan_ted 已提交
2273 2274
  @Component
  struct WebComponent {
L
laosan_ted 已提交
2275 2276
    controller: WebController = new WebController();
  
L
update  
laosan_ted 已提交
2277 2278 2279
    build() {
      Column() {
        Button('zoomIn')
L
laosan_ted 已提交
2280 2281 2282 2283
          .onClick(() => {
            let result = this.controller.zoomIn();
            console.log("result: " + result);
          })
Z
zhou-liting125 已提交
2284
        Web({ src: 'www.example.com', controller: this.controller })
L
laosan_ted 已提交
2285
      }
L
update  
laosan_ted 已提交
2286 2287 2288 2289
    }
  }
  ```

2290 2291 2292 2293
### zoomOut<sup>9+</sup>
zoomOut(): boolean

调用此接口将当前网页进行缩小,比列20%。
Z
zhou-liting125 已提交
2294 2295

**返回值:**
2296 2297 2298
| 类型      | 说明          |
| ------- | ----------- |
| boolean | 缩小操作是否成功执行。 |
2299

Z
zhou-liting125 已提交
2300
**示例:**
Z
zhou-liting125 已提交
2301 2302
  ```ts
  // xxx.ets
Z
zhou-liting125 已提交
2303
  @Entry
L
update  
laosan_ted 已提交
2304 2305
  @Component
  struct WebComponent {
L
laosan_ted 已提交
2306 2307
    controller: WebController = new WebController();
  
L
update  
laosan_ted 已提交
2308 2309 2310
    build() {
      Column() {
        Button('zoomOut')
L
laosan_ted 已提交
2311 2312 2313 2314
          .onClick(() => {
            let result = this.controller.zoomOut();
            console.log("result: " + result);
          })
Z
zhou-liting125 已提交
2315
        Web({ src: 'www.example.com', controller: this.controller })
L
laosan_ted 已提交
2316
      }
L
update  
laosan_ted 已提交
2317 2318 2319 2320
    }
  }
  ```

L
lixingchi1 已提交
2321 2322
### refresh

Z
zhou-liting125 已提交
2323
refresh()
L
lixingchi1 已提交
2324

L
liwenzhen 已提交
2325
调用此接口通知Web组件刷新网页。
L
lixingchi1 已提交
2326

Z
zhou-liting125 已提交
2327
**示例:**
Z
zhou-liting125 已提交
2328 2329
  ```ts
  // xxx.ets
Z
zhou-liting125 已提交
2330
  @Entry
L
update  
laosan_ted 已提交
2331 2332
  @Component
  struct WebComponent {
L
laosan_ted 已提交
2333 2334
    controller: WebController = new WebController();
  
L
update  
laosan_ted 已提交
2335 2336 2337
    build() {
      Column() {
        Button('refresh')
L
laosan_ted 已提交
2338 2339 2340
          .onClick(() => {
            this.controller.refresh();
          })
Z
zhou-liting125 已提交
2341
        Web({ src: 'www.example.com', controller: this.controller })
L
laosan_ted 已提交
2342
      }
L
update  
laosan_ted 已提交
2343 2344 2345 2346
    }
  }
  ```

L
lixingchi1 已提交
2347 2348
### registerJavaScriptProxy

Z
zhou-liting125 已提交
2349
registerJavaScriptProxy(options: { object: object, name: string, methodList: Array\<string\> })
L
lixingchi1 已提交
2350

L
liwenzhen 已提交
2351
注入JavaScript对象到window对象中,并在window对象中调用该对象的方法。注册后,须调用refresh接口生效。
L
lixingchi1 已提交
2352

Z
zhou-liting125 已提交
2353
**参数:**
2354 2355 2356 2357 2358
| 参数名        | 参数类型            | 必填   | 默认值  | 参数描述                                     |
| ---------- | --------------- | ---- | ---- | ---------------------------------------- |
| object     | object          | 是    | -    | 参与注册的应用侧JavaScript对象。只能声明方法,不能声明属性 。其中方法的参数和返回类型只能为string,number,boolean |
| name       | string          | 是    | -    | 注册对象的名称,与window中调用的对象名一致。注册后window对象可以通过此名字访问应用侧JavaScript对象。 |
| methodList | Array\<string\> | 是    | -    | 参与注册的应用侧JavaScript对象的方法。                 |
L
lixingchi1 已提交
2359

Z
zhou-liting125 已提交
2360
**示例:**
Z
zhou-liting125 已提交
2361 2362
  ```ts
  // xxx.ets
Z
zhou-liting125 已提交
2363
  @Entry
L
update  
laosan_ted 已提交
2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391
  @Component
  struct Index {
    controller: WebController = new WebController()
    testObj = {
      test: (data) => {
        return "ArkUI Web Component";
      },
      toString: () => {
        console.log('Web Component toString');
      }
    }
    build() {
      Column() {
        Row() {
          Button('Register JavaScript To Window').onClick(() => {
            this.controller.registerJavaScriptProxy({
              object: this.testObj,
              name: "objName",
              methodList: ["test", "toString"],
            });
          })
        }
        Web({ src: $rawfile('index.html'), controller: this.controller })
          .javaScriptAccess(true)
      }
    }
  }
  ```
2392

L
update  
laosan_ted 已提交
2393
  ```html
L
laosan_ted 已提交
2394
  <!-- index.html -->
L
update  
laosan_ted 已提交
2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410
  <!DOCTYPE html>
  <html>
      <meta charset="utf-8">
      <body>
          Hello world!
      </body>
      <script type="text/javascript">
      function htmlTest() {
          str = objName.test("test function");
          console.log('objName.test result:'+ str);
      }
  </script>
  </html>
  
  ```

L
lixingchi1 已提交
2411 2412
### runJavaScript

Z
zhou-liting125 已提交
2413
runJavaScript(options: { script: string, callback?: (result: string) => void })
L
lixingchi1 已提交
2414

L
liwenzhen 已提交
2415
异步执行JavaScript脚本,并通过回调方式返回脚本执行的结果。runJavaScript需要在loadUrl完成后,比如onPageEnd中调用。
L
lixingchi1 已提交
2416

Z
zhou-liting125 已提交
2417
**参数:**
2418 2419 2420 2421
| 参数名      | 参数类型                     | 必填   | 默认值  | 参数描述                                     |
| -------- | ------------------------ | ---- | ---- | ---------------------------------------- |
| script   | string                   | 是    | -    | JavaScript脚本。                            |
| callback | (result: string) => void | 否    | -    | 回调执行JavaScript脚本结果。JavaScript脚本若执行失败或无返回值时,返回null。 |
L
lixingchi1 已提交
2422

Z
zhou-liting125 已提交
2423
**示例:**
Z
zhou-liting125 已提交
2424 2425
  ```ts
  // xxx.ets
Z
zhou-liting125 已提交
2426
  @Entry
L
update  
laosan_ted 已提交
2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450
  @Component
  struct WebComponent {
    controller: WebController = new WebController();
    @State webResult: string = ''
    build() {
      Column() {
        Text(this.webResult).fontSize(20)
        Web({ src: $rawfile('index.html'), controller: this.controller })
        .javaScriptAccess(true)
        .onPageEnd(e => {
          this.controller.runJavaScript({
            script: 'test()',
            callback: (result: string)=> {
              this.webResult = result
              console.info(`The test() return value is: ${result}`)
            }});
          console.info('url: ', e.url);
        })
      }
    }
  }
  ```

  ```html
L
laosan_ted 已提交
2451
  <!-- index.html -->
L
update  
laosan_ted 已提交
2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467
  <!DOCTYPE html>
  <html>
    <meta charset="utf-8">
    <body>
        Hello world!
    </body>
    <script type="text/javascript">
    function test() {
        console.log('Ark WebComponent');
        return "This value is from index.html"
    }
    </script>
  </html>

  ```

L
lixingchi1 已提交
2468 2469
### stop

Z
zhou-liting125 已提交
2470
stop()
Z
zengyawen 已提交
2471

L
lixingchi1 已提交
2472
停止页面加载。
Z
zengyawen 已提交
2473

Z
zhou-liting125 已提交
2474
**示例:**
Z
zhou-liting125 已提交
2475 2476
  ```ts
  // xxx.ets
Z
zhou-liting125 已提交
2477
  @Entry
L
update  
laosan_ted 已提交
2478 2479
  @Component
  struct WebComponent {
L
laosan_ted 已提交
2480 2481
    controller: WebController = new WebController();
  
L
update  
laosan_ted 已提交
2482 2483 2484
    build() {
      Column() {
        Button('stop')
L
laosan_ted 已提交
2485 2486 2487
          .onClick(() => {
            this.controller.stop();
          })
Z
zhou-liting125 已提交
2488
        Web({ src: 'www.example.com', controller: this.controller })
L
laosan_ted 已提交
2489
      }
L
update  
laosan_ted 已提交
2490 2491 2492 2493
    }
  }
  ```

T
Ted 已提交
2494 2495 2496 2497 2498 2499
### clearHistory

clearHistory(): void

删除所有前进后退记录。

Z
zhou-liting125 已提交
2500
**示例:**
Z
zhou-liting125 已提交
2501 2502
  ```ts
  // xxx.ets
Z
zhou-liting125 已提交
2503
  @Entry
L
update  
laosan_ted 已提交
2504 2505
  @Component
  struct WebComponent {
L
laosan_ted 已提交
2506 2507
    controller: WebController = new WebController();
  
L
update  
laosan_ted 已提交
2508 2509 2510
    build() {
      Column() {
        Button('clearHistory')
L
laosan_ted 已提交
2511 2512 2513
          .onClick(() => {
            this.controller.clearHistory();
          })
Z
zhou-liting125 已提交
2514
        Web({ src: 'www.example.com', controller: this.controller })
L
laosan_ted 已提交
2515
      }
L
update  
laosan_ted 已提交
2516 2517 2518 2519
    }
  }
  ```

Z
zhou-liting125 已提交
2520
### getCookieManager<sup>9+</sup>
T
Ted 已提交
2521 2522 2523 2524

getCookieManager(): WebCookie

获取web组件cookie管理对象。
Z
zhou-liting125 已提交
2525 2526

**返回值:**
2527 2528 2529
| 类型        | 说明                                       |
| --------- | ---------------------------------------- |
| WebCookie | web组件cookie管理对象,参考[WebCookie](#webcookie)定义。 |
2530

Z
zhou-liting125 已提交
2531
**示例:**
Z
zhou-liting125 已提交
2532 2533
  ```ts
  // xxx.ets
Z
zhou-liting125 已提交
2534
  @Entry
L
update  
laosan_ted 已提交
2535 2536
  @Component
  struct WebComponent {
L
laosan_ted 已提交
2537 2538
    controller: WebController = new WebController();
  
L
update  
laosan_ted 已提交
2539 2540 2541
    build() {
      Column() {
        Button('getCookieManager')
L
laosan_ted 已提交
2542 2543 2544
          .onClick(() => {
            let cookieManager = this.controller.getCookieManager();
          })
Z
zhou-liting125 已提交
2545
        Web({ src: 'www.example.com', controller: this.controller })
L
laosan_ted 已提交
2546
      }
L
update  
laosan_ted 已提交
2547 2548 2549 2550
    }
  }
  ```

2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630
### createWebMessagePorts<sup>9+</sup>

createWebMessagePorts(): Array<WebMessagePort>

创建Web信息端口。

**返回值:**
| 类型                              | 说明            |
| ------------------------------- | ------------- |
| Array<WebMessagePort> | web信息端口列表 |

**示例:**
  ```ts
  // xxx.ets
  @Entry
  @Component
  struct WebComponent {
    controller: WebController = new WebController();
    ports: WebMessagePorts[] = null;
    build() {
      Column() {
        Button('createWebMessagePorts')
          .onClick(() => {
            this.ports = this.controller.createWebMessagePorts();
            console.log("createWebMessagePorts size:" + this.ports.length)
          })
        Web({ src: 'www.example.com', controller: this.controller })
      }
    }
  }
  ```

### postMessage<sup>9+</sup>

postMessage(options: { message: WebMessageEvent, uri: string}): void

发送Web信息端口到Html5。

**参数:**
| 参数名        | 参数类型            | 必填   | 默认值  | 参数描述                      |
| ---------- | --------------- | ---- | ---- | ------------------------- |
| message     | WebMessageEvent          | 是    | -    |要发送的信息,包含数据和信息端口 。 |
| uri       | string          | 是    | -    | 接收该信息的URI。 |

**示例:**
  ```ts
  // xxx.ets
  @Entry
  @Component
  struct WebComponent {
    controller: WebController = new WebController();
    ports: WebMessagePorts[] = null;
    build() {
      Column() {
        Button('postMessage')
          .onClick(() => {
            var sendPortArray = new Array(this.ports[1]);
            var msgEvent = new WebMessageEvent();
            msgEvent.setData("__init_ports__");
            msgEvent.setPorts(sendPortArray);
            this.controller.postMessage(msgEvent, uri:"*");
          })
        Web({ src: 'www.example.com', controller: this.controller })
      }
    }
  }
  // xxx.js
  var h5Port;
  window.addEventListener('message', function(event){
    if (event.data == '__init_ports__') {
      if(event.ports[0] != null) {
        h5Port = event.ports[0];
        h5Port.onmessage = function(event) {
          console.log('receive message from ets, on message:' + event.data);
        }
      }
    }
  })
  ```

2631
## HitTestValue<sup>9+</sup>
Z
zhou-liting125 已提交
2632
提供点击区域的元素信息。示例代码参考[getHitTestValue](#gethittestvalue9)
2633 2634 2635 2636 2637

### getType<sup>9+</sup>
getType(): HitTestType

获取当前被点击区域的元素类型。
Z
zhou-liting125 已提交
2638 2639

**返回值:**
2640 2641 2642
| 类型                              | 说明            |
| ------------------------------- | ------------- |
| [HitTestType](#hittesttype枚举说明) | 当前被点击区域的元素类型。 |
2643 2644 2645 2646 2647

### getExtra<sup>9+</sup>
getExtra(): string

若被点击区域为图片或链接,则附加参数信息为其url地址。
Z
zhou-liting125 已提交
2648 2649

**返回值:**
2650 2651 2652
| 类型     | 说明           |
| ------ | ------------ |
| string | 点击区域的附加参数信息。 |
2653

T
Ted 已提交
2654
## WebCookie
Z
zhou-liting125 已提交
2655

T
Ted 已提交
2656
通过WebCookie可以控制Web组件中的cookie的各种行为,其中每个应用中的所有web组件共享一个WebCookie。通过controller方法中的getCookieManager方法可以获取WebCookie对象,进行后续的cookie管理操作。
Z
zhou-liting125 已提交
2657 2658

### setCookie<sup>9+</sup>
T
Ted 已提交
2659 2660 2661 2662
setCookie(url: string, value: string): boolean

设置cookie,该方法为同步方法。设置成功返回true,否则返回false。

Z
zhou-liting125 已提交
2663
**参数:**
2664 2665 2666 2667
| 参数名   | 参数类型   | 必填   | 默认值  | 参数描述              |
| ----- | ------ | ---- | ---- | ----------------- |
| url   | string | 是    | -    | 要设置的cookie所属的url。 |
| value | string | 是    | -    | cookie的值。         |
Z
zhou-liting125 已提交
2668

Z
zhou-liting125 已提交
2669
**返回值:** 
2670 2671 2672
| 类型      | 说明            |
| ------- | ------------- |
| boolean | 设置cookie是否成功。 |
T
Ted 已提交
2673

Z
zhou-liting125 已提交
2674
**示例:**
Z
zhou-liting125 已提交
2675 2676
  ```ts
  // xxx.ets
Z
zhou-liting125 已提交
2677
  @Entry
L
update  
laosan_ted 已提交
2678 2679
  @Component
  struct WebComponent {
L
laosan_ted 已提交
2680 2681
    controller: WebController = new WebController();
  
L
update  
laosan_ted 已提交
2682 2683 2684
    build() {
      Column() {
        Button('setCookie')
L
laosan_ted 已提交
2685
          .onClick(() => {
Z
zhou-liting125 已提交
2686
            let result = this.controller.getCookieManager().setCookie("www.example.com", "a=b");
L
laosan_ted 已提交
2687 2688
            console.log("result: " + result);
          })
Z
zhou-liting125 已提交
2689
        Web({ src: 'www.example.com', controller: this.controller })
L
laosan_ted 已提交
2690
      }
L
update  
laosan_ted 已提交
2691 2692 2693 2694
    }
  }
  ```

Z
zhou-liting125 已提交
2695
### saveCookieSync<sup>9+</sup>
T
Ted 已提交
2696 2697 2698
saveCookieSync(): boolean

将当前存在内存中的cookie同步到磁盘中,该方法为同步方法。
Z
zhou-liting125 已提交
2699 2700

**返回值:**
2701 2702 2703
| 类型      | 说明                   |
| ------- | -------------------- |
| boolean | 同步内存cookie到磁盘操作是否成功。 |
2704

Z
zhou-liting125 已提交
2705
**示例:**
Z
zhou-liting125 已提交
2706 2707
  ```ts
  // xxx.ets
Z
zhou-liting125 已提交
2708
  @Entry
L
update  
laosan_ted 已提交
2709 2710
  @Component
  struct WebComponent {
L
laosan_ted 已提交
2711 2712
    controller: WebController = new WebController();
  
L
update  
laosan_ted 已提交
2713 2714 2715
    build() {
      Column() {
        Button('saveCookieSync')
L
laosan_ted 已提交
2716 2717 2718 2719
          .onClick(() => {
            let result = this.controller.getCookieManager().saveCookieSync();
            console.log("result: " + result);
          })
Z
zhou-liting125 已提交
2720
        Web({ src: 'www.example.com', controller: this.controller })
L
laosan_ted 已提交
2721
      }
L
update  
laosan_ted 已提交
2722 2723 2724 2725
    }
  }
  ```

2726 2727 2728 2729 2730 2731 2732
## WebDataBase<sup>9+</sup>
web组件数据库管理对象。

### existHttpAuthCredentials<sup>9+</sup>

static existHttpAuthCredentials(): boolean

2733
判断是否存在任何已保存的HTTP身份验证凭据,该方法为同步方法。存在返回true,不存在返回false。
2734

Z
zhou-liting125 已提交
2735
**返回值:** 
2736 2737 2738
| 类型      | 说明                                       |
| ------- | ---------------------------------------- |
| boolean | 是否存在任何已保存的HTTP身份验证凭据。存在返回true,不存在返回false |
2739

Z
zhou-liting125 已提交
2740
**示例:**
Z
zhou-liting125 已提交
2741 2742
  ```ts
  // xxx.ets
L
update  
laosan_ted 已提交
2743
  import web from '@ohos.web';
L
laosan_ted 已提交
2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755
  @Entry
  @Component
  struct WebComponent {
    controller: WebController = new WebController();
  
    build() {
      Column() {
        Button('existHttpAuthCredentials')
          .onClick(() => {
            let result = web.WebDataBase.existHttpAuthCredentials();
            console.log('result: ' + result);
          })
Z
zhou-liting125 已提交
2756
        Web({ src: 'www.example.com', controller: this.controller })
L
laosan_ted 已提交
2757 2758 2759
      }
    }
  }
L
update  
laosan_ted 已提交
2760 2761
  ```

2762 2763 2764 2765 2766 2767
### deleteHttpAuthCredentials<sup>9+</sup>

static deleteHttpAuthCredentials(): void

清除所有已保存的HTTP身份验证凭据,该方法为同步方法。

Z
zhou-liting125 已提交
2768
**示例:**
Z
zhou-liting125 已提交
2769 2770
  ```ts
  // xxx.ets
L
update  
laosan_ted 已提交
2771
  import web from '@ohos.web';
L
laosan_ted 已提交
2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782
  @Entry
  @Component
  struct WebComponent {
    controller: WebController = new WebController();
  
    build() {
      Column() {
        Button('deleteHttpAuthCredentials')
          .onClick(() => {
            web.WebDataBase.deleteHttpAuthCredentials();
          })
Z
zhou-liting125 已提交
2783
        Web({ src: 'www.example.com', controller: this.controller })
L
laosan_ted 已提交
2784 2785 2786
      }
    }
  }
L
update  
laosan_ted 已提交
2787 2788
  ```

2789 2790 2791 2792
### getHttpAuthCredentials<sup>9+</sup>

static getHttpAuthCredentials(host: string, realm: string): Array\<string\>

2793
检索给定主机和域的HTTP身份验证凭据,该方法为同步方法。检索成功返回一个包含用户名和密码的组数,检索不成功返回空数组。
2794

Z
zhou-liting125 已提交
2795
**参数:**
2796 2797 2798 2799
| 参数名   | 参数类型   | 必填   | 默认值  | 参数描述             |
| ----- | ------ | ---- | ---- | ---------------- |
| host  | string | 是    | -    | HTTP身份验证凭据应用的主机。 |
| realm | string | 是    | -    | HTTP身份验证凭据应用的域。  |
Z
zhou-liting125 已提交
2800

Z
zhou-liting125 已提交
2801
**返回值:** 
2802 2803 2804
| 类型              | 说明                     |
| --------------- | ---------------------- |
| Array\<string\> | 包含用户名和密码的组数,检索失败返回空数组。 |
2805

Z
zhou-liting125 已提交
2806
**示例:**
Z
zhou-liting125 已提交
2807 2808
  ```ts
  // xxx.ets
L
update  
laosan_ted 已提交
2809
  import web from '@ohos.web';
L
laosan_ted 已提交
2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826
  @Entry
  @Component
  struct WebComponent {
    controller: WebController = new WebController();
    host: string = "www.spincast.org";
    realm: string = "protected example";
    username_password: string[];
    build() {
      Column() {
        Button('getHttpAuthCredentials')
          .onClick(() => {
            this.username_password = web.WebDataBase.getHttpAuthCredentials(this.host, this.realm);
            console.log('num: ' + this.username_password.length);
            ForEach(this.username_password, (item) => {
              console.log('username_password: ' + item);
            }, item => item)
          })
Z
zhou-liting125 已提交
2827
        Web({ src: 'www.example.com', controller: this.controller })
L
laosan_ted 已提交
2828 2829 2830
      }
    }
  }
L
update  
laosan_ted 已提交
2831 2832
  ```

2833 2834 2835 2836
### saveHttpAuthCredentials<sup>9+</sup>

static saveHttpAuthCredentials(host: string, realm: string, username: string, password: string): void

2837
保存给定主机和域的HTTP身份验证凭据,该方法为同步方法。
2838

Z
zhou-liting125 已提交
2839
**参数:**
2840 2841 2842 2843 2844 2845
| 参数名      | 参数类型   | 必填   | 默认值  | 参数描述             |
| -------- | ------ | ---- | ---- | ---------------- |
| host     | string | 是    | -    | HTTP身份验证凭据应用的主机。 |
| realm    | string | 是    | -    | HTTP身份验证凭据应用的域。  |
| username | string | 是    | -    | 用户名。             |
| password | string | 是    | -    | 密码。              |
2846

Z
zhou-liting125 已提交
2847
**示例:**
Z
zhou-liting125 已提交
2848 2849
  ```ts
  // xxx.ets
L
update  
laosan_ted 已提交
2850
  import web from '@ohos.web';
L
laosan_ted 已提交
2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862
  @Entry
  @Component
  struct WebComponent {
    controller: WebController = new WebController();
    host: string = "www.spincast.org";
    realm: string = "protected example";
    build() {
      Column() {
        Button('saveHttpAuthCredentials')
          .onClick(() => {
            web.WebDataBase.saveHttpAuthCredentials(this.host, this.realm, "Stromgol", "Laroche");
          })
Z
zhou-liting125 已提交
2863
        Web({ src: 'www.example.com', controller: this.controller })
L
laosan_ted 已提交
2864 2865 2866
      }
    }
  }
L
update  
laosan_ted 已提交
2867 2868
  ```

2869 2870 2871 2872 2873 2874
## WebStorage<sup>9+</sup>
通过WebStorage可管理Web SQL数据库接口和HTML5 Web存储接口,每个应用中的所有Web组件共享一个WebStorage。
### deleteAllData<sup>9+</sup>
static deleteAllData(): void

清除Web SQL数据库当前使用的所有存储。
L
update  
laosan_ted 已提交
2875

Z
zhou-liting125 已提交
2876
**示例:**
Z
zhou-liting125 已提交
2877 2878
  ```ts
  // xxx.ets
L
update  
laosan_ted 已提交
2879
  import web from '@ohos.web';
L
laosan_ted 已提交
2880 2881 2882 2883 2884 2885 2886 2887 2888 2889
  @Entry
  @Component
  struct WebComponent {
    controller: WebController = new WebController();
    build() {
      Column() {
        Button('deleteAllData')
          .onClick(() => {
            web.WebStorage.deleteAllData();
          })
Z
zhou-liting125 已提交
2890
        Web({ src: 'www.example.com', controller: this.controller })
L
laosan_ted 已提交
2891 2892 2893 2894
        .databaseAccess(true)
      }
    }
  }
L
update  
laosan_ted 已提交
2895 2896
  ```

2897 2898 2899 2900
### deleteOrigin<sup>9+</sup>
static deleteOrigin(origin : string): void

清除指定源所使用的存储。
Z
zhou-liting125 已提交
2901

Z
zhou-liting125 已提交
2902
**参数:**
2903 2904 2905
| 参数名    | 参数类型   | 必填   | 说明         |
| ------ | ------ | ---- | ---------- |
| origin | string | 是    | 指定源的字符串索引。 |
2906

Z
zhou-liting125 已提交
2907
**示例:**
Z
zhou-liting125 已提交
2908 2909
  ```ts
  // xxx.ets
L
update  
laosan_ted 已提交
2910
  import web from '@ohos.web';
L
laosan_ted 已提交
2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921
  @Entry
  @Component
  struct WebComponent {
    controller: WebController = new WebController();
    origin: string = "origin";
    build() {
      Column() {
        Button('getHttpAuthCredentials')
          .onClick(() => {
            web.WebStorage.deleteOrigin(this.origin);
          })
Z
zhou-liting125 已提交
2922
        Web({ src: 'www.example.com', controller: this.controller })
L
laosan_ted 已提交
2923 2924 2925 2926
        .databaseAccess(true)
      }
    }
  }
L
update  
laosan_ted 已提交
2927 2928
  ```

2929
### getOrigins<sup>9+</sup>
Z
zhou-liting125 已提交
2930
static getOrigins(callback: AsyncCallback<Array<[WebStorageOrigin]>>) : void
2931 2932

以回调方式异步获取当前使用Web SQL数据库的所有源的信息。
Z
zhou-liting125 已提交
2933

Z
zhou-liting125 已提交
2934
**参数:**
2935 2936 2937
| 参数名      | 参数类型                                     | 必填   | 说明                                  |
| -------- | ---------------------------------------- | ---- | ----------------------------------- |
| callback | AsyncCallback<Array<[WebStorageOrigin](#webstorageorigin9)>> | 是    | 以数组方式返回源的信息,信息内容参考WebStorageOrigin。 |
2938

Z
zhou-liting125 已提交
2939
**示例:**
Z
zhou-liting125 已提交
2940 2941
  ```ts
  // xxx.ets
L
update  
laosan_ted 已提交
2942
  import web from '@ohos.web';
L
laosan_ted 已提交
2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963
  @Entry
  @Component
  struct WebComponent {
    controller: WebController = new WebController();
    origin: string = "origin";
    build() {
      Column() {
        Button('getOrigins')
          .onClick(() => {
            web.WebStorage.getOrigins((error, origins) => {
              if (error) {
                console.log('error: ' + error);
                return;
              }
              for (let i = 0; i < origins.length; i++) {
                console.log('origin: ' + origins[i].origin);
                console.log('usage: ' + origins[i].usage);
                console.log('quota: ' + origins[i].quota);
              }
            })
          })
Z
zhou-liting125 已提交
2964
        Web({ src: 'www.example.com', controller: this.controller })
L
laosan_ted 已提交
2965 2966
        .databaseAccess(true)
      }
L
update  
laosan_ted 已提交
2967
    }
L
laosan_ted 已提交
2968 2969
  }
  ```
L
update  
laosan_ted 已提交
2970

2971
### getOrigins<sup>9+</sup>
Z
zhou-liting125 已提交
2972
static getOrigins() : Promise<Array<[WebStorageOrigin]>>
2973 2974

以Promise方式异步获取当前使用Web SQL数据库的所有源的信息。
Z
zhou-liting125 已提交
2975

Z
zhou-liting125 已提交
2976
**返回值:**
2977 2978 2979
| 类型                                       | 说明                                       |
| ---------------------------------------- | ---------------------------------------- |
| Promise<Array<[WebStorageOrigin](#webstorageorigin9)>> | Promise实例,用于获取当前所有源的信息,信息内容参考WebStorageOrigin。 |
2980

Z
zhou-liting125 已提交
2981
**示例:**
Z
zhou-liting125 已提交
2982 2983
  ```ts
  // xxx.ets
L
update  
laosan_ted 已提交
2984
  import web from '@ohos.web';
L
laosan_ted 已提交
2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005
  @Entry
  @Component
  struct WebComponent {
    controller: WebController = new WebController();
    origin: string = "origin";
    build() {
      Column() {
        Button('getOrigins')
          .onClick(() => {
            web.WebStorage.getOrigins()
              .then(origins => {
                for (let i = 0; i < origins.length; i++) {
                  console.log('origin: ' + origins[i].origin);
                  console.log('usage: ' + origins[i].usage);
                  console.log('quota: ' + origins[i].quota);
                }
              })
              .catch(error => {
                console.log('error: ' + error);
              })
          })
Z
zhou-liting125 已提交
3006
        Web({ src: 'www.example.com', controller: this.controller })
L
laosan_ted 已提交
3007 3008
          .databaseAccess(true)
      }
L
update  
laosan_ted 已提交
3009
    }
L
laosan_ted 已提交
3010
  }
L
update  
laosan_ted 已提交
3011 3012
  ```

3013
### getOriginQuota<sup>9+</sup>
3014
static getOriginQuota(origin : string, callback : AsyncCallback\<number>) : void
3015 3016

使用callback回调异步获取指定源的Web SQL数据库的存储配额,配额以字节为单位。
Z
zhou-liting125 已提交
3017

Z
zhou-liting125 已提交
3018
**参数:**
3019 3020 3021
| 参数名      | 参数类型                   | 必填   | 说明        |
| -------- | ---------------------- | ---- | --------- |
| origin   | string                 | 是    | 指定源的字符串索引 |
3022
| callback | AsyncCallback\<number> | 是    | 指定源的存储配额。 |
3023

Z
zhou-liting125 已提交
3024
**示例:**
Z
zhou-liting125 已提交
3025 3026
  ```ts
  // xxx.ets
L
update  
laosan_ted 已提交
3027
  import web from '@ohos.web';
L
laosan_ted 已提交
3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044
  @Entry
  @Component
  struct WebComponent {
    controller: WebController = new WebController();
    origin: string = "origin";
    build() {
      Column() {
        Button('getOriginQuota')
          .onClick(() => {
            web.WebStorage.getOriginQuota(this.origin, (error, quota) => {
              if (error) {
                console.log('error: ' + error);
                return;
              }
              console.log('quota: ' + quota);
            })
          })
Z
zhou-liting125 已提交
3045
        Web({ src: 'www.example.com', controller: this.controller })
L
laosan_ted 已提交
3046 3047
          .databaseAccess(true)
      }
L
update  
laosan_ted 已提交
3048
    }
L
laosan_ted 已提交
3049
  }
L
update  
laosan_ted 已提交
3050 3051
  ```

3052
### getOriginQuota<sup>9+</sup>
3053
static getOriginQuota(origin : string) : Promise\<number>
3054 3055

以Promise方式异步获取指定源的Web SQL数据库的存储配额,配额以字节为单位。
Z
zhou-liting125 已提交
3056

Z
zhou-liting125 已提交
3057
**参数:**
3058 3059 3060
| 参数名    | 参数类型   | 必填   | 说明         |
| ------ | ------ | ---- | ---------- |
| origin | string | 是    | 指定源的字符串索引。 |
3061

Z
zhou-liting125 已提交
3062
**返回值:**
3063 3064
| 类型               | 说明                      |
| ---------------- | ----------------------- |
3065
| Promise\<number> | Promise实例,用于获取指定源的存储配额。 |
3066

Z
zhou-liting125 已提交
3067
**示例:**
Z
zhou-liting125 已提交
3068 3069
  ```ts
  // xxx.ets
L
update  
laosan_ted 已提交
3070
  import web from '@ohos.web';
L
laosan_ted 已提交
3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087
  @Entry
  @Component
  struct WebComponent {
    controller: WebController = new WebController();
    origin: string = "origin";
    build() {
      Column() {
        Button('getOriginQuota')
          .onClick(() => {
            web.WebStorage.getOriginQuota(this.origin)
              .then(quota => {
                console.log('quota: ' + quota);
              })
              .catch(error => {
                console.log('error: ' + error);
              })
          })
Z
zhou-liting125 已提交
3088
        Web({ src: 'www.example.com', controller: this.controller })
L
laosan_ted 已提交
3089 3090 3091 3092
          .databaseAccess(true)
      }
    }
  }
L
update  
laosan_ted 已提交
3093 3094
  ```

3095
### getOriginUsage<sup>9+</sup>
3096
static getOriginUsage(origin : string, callback : AsyncCallback\<number>) : void
3097 3098

以回调方式异步获取指定源的Web SQL数据库的存储量,存储量以字节为单位。
Z
zhou-liting125 已提交
3099

Z
zhou-liting125 已提交
3100
**参数:**
3101 3102 3103
| 参数名      | 参数类型                   | 必填   | 说明         |
| -------- | ---------------------- | ---- | ---------- |
| origin   | string                 | 是    | 指定源的字符串索引。 |
3104
| callback | AsyncCallback\<number> | 是    | 指定源的存储量。   |
3105

Z
zhou-liting125 已提交
3106
**示例:**
Z
zhou-liting125 已提交
3107 3108
  ```ts
  // xxx.ets
L
update  
laosan_ted 已提交
3109
  import web from '@ohos.web';
L
laosan_ted 已提交
3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126
  @Entry
  @Component
  struct WebComponent {
    controller: WebController = new WebController();
    origin: string = "origin";
    build() {
      Column() {
        Button('getOriginUsage')
          .onClick(() => {
            web.WebStorage.getOriginUsage(this.origin, (error, usage) => {
              if (error) {
                console.log('error: ' + error);
                return;
              }
              consloe.log('usage: ' + usage);
            })
          })
Z
zhou-liting125 已提交
3127
        Web({ src: 'www.example.com', controller: this.controller })
L
laosan_ted 已提交
3128 3129
          .databaseAccess(true)
      }
L
update  
laosan_ted 已提交
3130
    }
L
laosan_ted 已提交
3131
  }
L
update  
laosan_ted 已提交
3132 3133
  ```

3134
### getOriginUsage<sup>9+</sup>
3135
static getOriginUsage(origin : string) : Promise\<number>
3136 3137

以Promise方式异步获取指定源的Web SQL数据库的存储量,存储量以字节为单位。
Z
zhou-liting125 已提交
3138

Z
zhou-liting125 已提交
3139
**参数:**
3140 3141 3142
| 参数名    | 参数类型   | 必填   | 说明         |
| ------ | ------ | ---- | ---------- |
| origin | string | 是    | 指定源的字符串索引。 |
3143

Z
zhou-liting125 已提交
3144
**返回值:**
3145 3146
| 类型               | 说明                     |
| ---------------- | ---------------------- |
3147
| Promise\<number> | Promise实例,用于获取指定源的存储量。 |
3148

Z
zhou-liting125 已提交
3149
**示例:**
Z
zhou-liting125 已提交
3150 3151
  ```ts
  // xxx.ets
L
update  
laosan_ted 已提交
3152
  import web from '@ohos.web';
L
laosan_ted 已提交
3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169
  @Entry
  @Component
  struct WebComponent {
    controller: WebController = new WebController();
    origin: string = "origin";
    build() {
      Column() {
        Button('getOriginQuota')
          .onClick(() => {
            web.WebStorage.getOriginUsage(this.origin)
              .then(usage => {
                console.log('usage: ' + usage);
              })
              .catch(error => {
                console.log('error: ' + error);
              })
          })
Z
zhou-liting125 已提交
3170
        Web({ src: 'www.example.com', controller: this.controller })
L
laosan_ted 已提交
3171 3172 3173 3174
          .databaseAccess(true)
      }
    }
  }
L
update  
laosan_ted 已提交
3175
  ```
F
Fred Ranking 已提交
3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312
### searchAllAsync<sup>9+</sup>

searchAllAsync(searchString: string): void

异步查找网页中所有匹配关键字'searchString'的内容并高亮,结果通过[onSearchResultReceive](#onsearchresultreceive9)异步返回。

**参数:**

| 参数名  | 参数类型   | 必填   | 默认值  | 参数描述                  |
| ---- | ------ | ---- | ---- | --------------------- |
| searchString | string | 是    | -    | 查找的关键字。 |

**示例:**
  ```ts
  // xxx.ets
  @Entry
  @Component
  struct WebComponent {
    controller: WebController = new WebController();
    @State searchString: string = "xxx";

    build() {
      Column() {
        Button('searchString')
          .onClick(() => {
            this.controller.searchAllAsync(this.searchString);
          })
        Button('clearMatches')
          .onClick(() => {
            this.controller.clearMatches();
          })
        Button('searchNext')
          .onClick(() => {
            this.controller.searchNext(true);
          })
        Web({ src: 'www.example.com', controller: this.controller })
     	  .onSearchResultReceive(ret => {
            console.log("on search result receive:" + "[cur]" + ret.activeMatchOrdinal +
              "[total]" + ret.numberOfMatches + "[isDone]"+ ret.isDoneCounting);
          })
      }
    }
  }
  ```

### clearMatches<sup>9+</sup>

clearMatches(): void

清除所有通过[searchAllAsync](#searchallasync9)匹配到的高亮字符查找结果。

**示例:**
  ```ts
  // xxx.ets
  @Entry
  @Component
  struct WebComponent {
    controller: WebController = new WebController();

    build() {
      Column() {
        Button('clearMatches')
          .onClick(() => {
            this.controller.clearMatches();
          })
        Web({ src: 'www.example.com', controller: this.controller })
      }
    }
  }
  ```

### searchNext<sup>9+</sup>

searchNext(forward: boolean): void

滚动到下一个匹配的查找结果并高亮。

**参数:**

| 参数名  | 参数类型   | 必填   | 默认值  | 参数描述                  |
| ---- | ------ | ---- | ---- | --------------------- |
| forward | boolean | 是    | -    | 从前向后或者逆向查找。 |


**示例:**
  ```ts
  // xxx.ets
  @Entry
  @Component
  struct WebComponent {
    controller: WebController = new WebController();

    build() {
      Column() {
        Button('searchNext')
          .onClick(() => {
            this.controller.searchNext(true);
          })
        Web({ src: 'www.example.com', controller: this.controller })
      }
    }
  }
  ```

### onSearchResultReceive<sup>9+</sup>

onSearchResultReceive(callback: (event?: {activeMatchOrdinal: number, numberOfMatches: number, isDoneCounting: boolean}) => void): WebAttribute

回调通知调用方网页页内查找的结果。

**参数:**

| 参数名                | 参数类型          | 参数描述                                |
| ------------------ | ------------- | ----------------------------------- |
| activeMatchOrdinal | number        | 当前匹配的查找项的序号(从0开始)。 |
| numberOfMatches    | number        | 所有匹配到的关键词的个数。 |
| isDoneCounting     | boolean       | 当次页内查找操作是否结束。该方法可能会回调多次,直到isDoneCounting为true为止。 |

**示例:**
  ```ts
  // xxx.ets
  @Entry
  @Component
  struct WebComponent {
    controller: WebController = new WebController();

    build() {
      Column() {
        Web({ src: 'www.example.com', controller: this.controller })
     	  .onSearchResultReceive(ret => {
            console.log("on search result receive:" + "[cur]" + ret.activeMatchOrdinal +
              "[total]" + ret.numberOfMatches + "[isDone]"+ ret.isDoneCounting);
          })
      }
    }
  }
  ```
L
update  
laosan_ted 已提交
3313

3314
## WebStorageOrigin<sup>9+</sup>
Z
zhou-liting125 已提交
3315

3316
提供Web SQL数据库的使用信息。
Z
zhou-liting125 已提交
3317

Z
zhou-liting125 已提交
3318
**参数:**
3319 3320 3321 3322 3323
| 参数名    | 参数类型   | 必填   | 说明         |
| ------ | ------ | ---- | ---------- |
| origin | string | 是    | 指定源的字符串索引。 |
| usage  | number | 是    | 指定源的存储量。   |
| quota  | number | 是    | 指定源的存储配额。  |
Z
zhou-liting125 已提交
3324 3325 3326

## MessageLevel枚举说明

3327 3328
| 名称    | 描述    |
| ----- | :---- |
Z
zhou-liting125 已提交
3329 3330 3331 3332 3333 3334 3335 3336 3337 3338
| Debug | 调试级别。 |
| Error | 错误级别。 |
| Info  | 消息级别。 |
| Log   | 日志级别。 |
| Warn  | 警告级别。 |

## RenderExitReason枚举说明

onRenderExited接口返回的渲染进程退出的具体原因。

3339 3340 3341 3342 3343 3344 3345
| 名称                         | 描述                |
| -------------------------- | ----------------- |
| ProcessAbnormalTermination | 渲染进程异常退出。         |
| ProcessWasKilled           | 收到SIGKILL,或被手动终止。 |
| ProcessCrashed             | 渲染进程崩溃退出,如段错误。    |
| ProcessOom                 | 程序内存不足。           |
| ProcessExitUnknown         | 其他原因。             |
Z
zhou-liting125 已提交
3346 3347 3348

## MixedMode枚举说明

3349 3350
| 名称         | 描述                                 |
| ---------- | ---------------------------------- |
Z
zhou-liting125 已提交
3351
| All        | 允许加载HTTP和HTTPS混合内容。所有不安全的内容都可以被加载。 |
3352 3353
| Compatible | 混合内容兼容性模式,部分不安全的内容可能被加载。           |
| None       | 不允许加载HTTP和HTTPS混合内容。               |
Z
zhou-liting125 已提交
3354 3355

## CacheMode枚举说明
3356 3357
| 名称      | 描述                                   |
| ------- | ------------------------------------ |
Z
zhou-liting125 已提交
3358
| Default | 使用未过期的cache加载资源,如果cache中无该资源则从网络中获取。 |
3359 3360 3361
| None    | 加载资源使用cache,如果cache中无该资源则从网络中获取。     |
| Online  | 加载资源不使用cache,全部从网络中获取。               |
| Only    | 只从cache中加载资源。                        |
Z
zhou-liting125 已提交
3362 3363

## FileSelectorMode枚举说明
3364 3365 3366 3367
| 名称                   | 描述         |
| -------------------- | ---------- |
| FileOpenMode         | 打开上传单个文件。  |
| FileOpenMultipleMode | 打开上传多个文件。  |
Z
zhou-liting125 已提交
3368
| FileOpenFolderMode   | 打开上传文件夹模式。 |
3369
| FileSaveMode         | 文件保存模式。    |
Z
zhou-liting125 已提交
3370 3371 3372

 ## HitTestType枚举说明

3373 3374 3375 3376 3377 3378 3379 3380
| 名称            | 描述                       |
| ------------- | ------------------------ |
| EditText      | 可编辑的区域。                  |
| Email         | 电子邮件地址。                  |
| HttpAnchor    | 超链接,其src为http。           |
| HttpAnchorImg | 带有超链接的图片,其中超链接的src为http。 |
| Img           | HTML::img标签。             |
| Map           | 地理地址。                    |
3381 3382 3383 3384
| Unknown       | 未知内容。                    |

## ProtectedResourceType<sup>9+</sup>枚举说明

X
xiongjun_gitee 已提交
3385 3386
| 名称      | 描述            | 备注           |
| --------- | -------------- | -------------- |
F
Fred Ranking 已提交
3387
| MidiSysex | MIDI SYSEX资源。| 目前仅支持权限事件上报,MIDI设备的使用还未支持。|
3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484

## WebAsyncController

通过WebAsyncController可以控制Web组件具有异步回调通知的行为,一个WebAsyncController对象控制一个Web组件。

### 创建对象

```
webController: WebController = new WebController();
webAsyncController: WebAsyncController = new WebAsyncController(webController);
```

### storeWebArchive<sup>9+</sup>

storeWebArchive(baseName: string, autoName: boolean, callback: AsyncCallback<string>): void

以回调方式异步保存当前页面。

**参数:**

| 参数名      | 参数类型                                     | 必填   | 说明                                  |
| -------- | ---------------------------------------- | ---- | ----------------------------------- |
| baseName | string | 是 | 文件存储路径,该值不能为空。
| autoName | boolean | 是 | 决定是否自动生成文件名。<br/>如果为false,则将baseName作为文件存储路径。<br/>如果为true,则假定baseName是一个目录,将根据当前页的Url自动生成文件名。
| callback | AsyncCallback<string> | 是    | 返回文件存储路径,保持网页失败会返回null。 |

**示例:**

  ```ts
  // xxx.ets
  import WebAsyncController from '@ohos.web';
  @Entry
  @Component
  struct WebComponent {
    controller: WebController = new WebController();
    build() {
      Column() {
        Button('saveWebArchive')
          .onClick(() => {
            let webAsyncController = new WebAsyncController(this.controller);
            webAsyncController.storeWebArchive("/data/storage/el2/base/", true, (filename) => {
              if (filename != null) {
                console.info(`save web archive success: ${filename}`)
              }
            });
          })
        Web({ src: 'www.example.com', controller: this.controller })
      }
    }
  }
  ```

### storeWebArchive<sup>9+</sup>

storeWebArchive(baseName: string, autoName: boolean): Promise<string>

以Promise方式异步保存当前页面。

**参数:**

| 参数名      | 参数类型                                     | 必填   | 说明                                  |
| -------- | ---------------------------------------- | ---- | ----------------------------------- |
| baseName | string | 是 | 文件存储路径,该值不能为空。
| autoName | boolean | 是 | 决定是否自动生成文件名。<br/>如果为false,则将baseName作为文件存储路径。<br/>如果为true,则假定baseName是一个目录,将根据当前页的Url自动生成文件名。

**返回值:**

| 类型                                       | 说明                                       |
| ---------------------------------------- | ---------------------------------------- |
| Promise<string> | Promise实例,保存成功返回文件路径,保存失败返回null。 |

**示例:**

  ```ts
  // xxx.ets
  import WebAsyncController from '@ohos.web';
  @Entry
  @Component
  struct WebComponent {
    controller: WebController = new WebController();
    build() {
      Column() {
        Button('saveWebArchive')
          .onClick(() => {
            let webAsyncController = new WebAsyncController(this.controller);
            webAsyncController.storeWebArchive("/data/storage/el2/base/", true)
              .then(filename => {
                if (filename != null) {
                  console.info(`save web archive success: ${filename}`)
                }
              })
          })
        Web({ src: 'www.example.com', controller: this.controller })
      }
    }
  }
  ```
3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609 3610 3611 3612 3613 3614 3615 3616 3617 3618 3619 3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652

## WebMessagePort<sup>9+</sup>

通过WebMessagePort可以进行消息的发送以及接收。
### postMessageEvent
close(): void

关闭该信息端口。

### postMessageEvent
postMessageEvent(message: WebMessageEvent): void

发送消息。

**参数:**
| 参数名   | 参数类型   | 必填   | 默认值  | 参数描述              |
| ----- | ------ | ---- | ---- | ----------------- |
| message   | WebMessageEvent | 是    | -    | 要发送的消息。 |

**示例:**
  ```ts
  // xxx.ets
  @Entry
  @Component
  struct WebComponent {
    controller: WebController = new WebController();
    ports: WebMessagePorts[] = null;

    build() {
      Column() {
        Button('postMessageEvent')
          .onClick(() => {
            var msg = new WebMessageEvent();
            msg.setData("post message from ets to h5");
            this.port[0].postMessageEvent(msg);
          })
        Web({ src: 'www.example.com', controller: this.controller })
      }
    }
  }
  ```

### onMessageEvent
onMessageEvent(callback: (result: string) => void): void

注册回调函数,接收h5侧发送过来的消息。

**参数:**
| 参数名   | 参数类型   | 必填   | 默认值  | 参数描述              |
| ----- | ------ | ---- | ---- | ----------------- |
| callback   | function | 是    | -    | 接收消息的回调函数。 |

**示例:**
  ```ts
  // xxx.ets
  @Entry
  @Component
  struct WebComponent {
    controller: WebController = new WebController();
    ports: WebMessagePorts[] = null;

    build() {
      Column() {
        Button('onMessageEvent')
          .onClick(() => {
            this.port[0].onMessageEvent((result: string) => {
              console.log("received message from h5, on message:" + result);
            })
          })
        Web({ src: 'www.example.com', controller: this.controller })
      }
    }
  }
  ```


## WebMessageEvent<sup>9+</sup>

通过WebMessagePort对要发送的消息和端口进行封装。

### getData
getData(): string

获取当前对象中存放的消息。

**返回值:**
| 类型                              | 说明            |
| ------------------------------- | ------------- |
| string | 当前该类型对象中存放的消息。 |


### setData
setData(data: string): void

设置当前对象中的消息。

**参数:**
| 参数名   | 参数类型   | 必填   | 默认值  | 参数描述              |
| ----- | ------ | ---- | ---- | ----------------- |
| data   | string | 是    | -    | 要发送的消息。 |

**示例:**
  ```ts
  // xxx.ets
  @Entry
  @Component
  struct WebComponent {
    controller: WebController = new WebController();
    ports: WebMessagePorts[] = null;

    build() {
      Column() {
        Button('setData')
          .onClick(() => {
            var msg = new WebMessageEvent();
            msg.setData("post message from ets to h5");
            this.port[0].postMessageEvent(msg);
          })
        Web({ src: 'www.example.com', controller: this.controller })
      }
    }
  }
  ```
### getPorts
getPorts(): Array<WebMessagePort>

获取当前对象中存放的消息端口。

**返回值:**
| 类型                              | 说明            |
| ------------------------------- | ------------- |
| Array<WebMessagePort> | 当前该类型对象中存放的消息端口。 |


### setPorts
setPorts(ports: Array<WebMessagePort>): void

设置当前对象中的消息端口。

**参数:**
| 参数名   | 参数类型   | 必填   | 默认值  | 参数描述              |
| ----- | ------ | ---- | ---- | ----------------- |
| ports   | Array<WebMessagePort> | 是    | -    | 要发送的消息端口。 |

**示例:**
  ```ts
  // xxx.ets
  @Entry
  @Component
  struct WebComponent {
    controller: WebController = new WebController();
    ports: WebMessagePorts[] = null;

    build() {
      Column() {
        Button('setPorts')
          .onClick(() => {
            var sendPortArray = new Array(this.ports[1]);
            var msgEvent = new WebMessageEvent();
            msgEvent.setData("__init_ports__");
            msgEvent.setPorts(sendPortArray);
            this.controller.postMessage(msgEvent, uri:"*");
          })
        Web({ src: 'www.example.com', controller: this.controller })
      }
    }
  }
  ```