From b4dc63b4d3b43199b0deacb695d48621615500a7 Mon Sep 17 00:00:00 2001 From: lixiang Date: Tue, 10 Jan 2023 17:23:29 +0800 Subject: [PATCH] =?UTF-8?q?webview=20=E6=96=B0=E5=A2=9E=E9=A1=B5=E9=9D=A2?= =?UTF-8?q?=E7=8A=B6=E6=80=81=E4=BF=9D=E5=AD=98=E6=81=A2=E5=A4=8D=E7=AD=89?= =?UTF-8?q?=E6=8E=A5=E5=8F=A3=E6=96=87=E6=A1=A3=E8=AF=B4=E6=98=8E?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: lixiang --- .../reference/apis/js-apis-webview.md | 224 +++++++++++++++++- 1 file changed, 220 insertions(+), 4 deletions(-) diff --git a/zh-cn/application-dev/reference/apis/js-apis-webview.md b/zh-cn/application-dev/reference/apis/js-apis-webview.md index 83c76ef9c3..92ed335f0f 100644 --- a/zh-cn/application-dev/reference/apis/js-apis-webview.md +++ b/zh-cn/application-dev/reference/apis/js-apis-webview.md @@ -2750,6 +2750,104 @@ struct WebComponent { } ``` +### pageUp + +pageUp(top:boolean): void + +将Webview的内容向上滚动半个视框大小或者跳转到页面最顶部,通过top入参控制。 + +**系统能力:** SystemCapability.Web.Webview.Core + +**参数:** + +| 参数名 | 类型 | 必填 | 说明 | +| ------ | ------- | ---- | ------------------------------------------------------------ | +| top | boolean | 是 | 是否跳转到页面最顶部,设置为false时将页面内容向上滚动半个视框大小,设置为true时跳转到页面最顶部。 | + +**错误码:** + +以下错误码的详细介绍请参见 [webview错误码](../errorcodes/errorcode-webview.md) + +| 错误码ID | 错误信息 | +| -------- | ------------------------------------------------------------ | +| 17100001 | Init error. The WebviewController must be associated with a Web component. | + +**示例:** + +```ts +// xxx.ets +import web_webview from '@ohos.web.webview'; + +@Entry +@Component +struct WebComponent { + controller: web_webview.WebviewController = new web_webview.WebviewController(); + + build() { + Column() { + Button('pageUp') + .onClick(() => { + try { + this.controller.pageUp(false); + } catch (error) { + console.error(`ErrorCode: ${error.code}, Message: ${error.message}`); + } + }) + Web({ src: 'www.example.com', controller: this.controller }) + } + } +} +``` + +### pageDown + +pageDown(bottom:boolean): void + +将Webview的内容向下滚动半个视框大小或者跳转到页面最底部,通过bottom入参控制。 + +**系统能力:** SystemCapability.Web.Webview.Core + +**参数:** + +| 参数名 | 类型 | 必填 | 说明 | +| ------ | ------- | ---- | ------------------------------------------------------------ | +| bottom | boolean | 是 | 是否跳转到页面最底部,设置为false时将页面内容向下滚动半个视框大小,设置为true时跳转到页面最底部。 | + +**错误码:** + +以下错误码的详细介绍请参见 [webview错误码](../errorcodes/errorcode-webview.md) + +| 错误码ID | 错误信息 | +| -------- | ------------------------------------------------------------ | +| 17100001 | Init error. The WebviewController must be associated with a Web component. | + +**示例:** + +```ts +// xxx.ets +import web_webview from '@ohos.web.webview'; + +@Entry +@Component +struct WebComponent { + controller: web_webview.WebviewController = new web_webview.WebviewController(); + + build() { + Column() { + Button('pageDown') + .onClick(() => { + try { + this.controller.pageDown(false); + } catch (error) { + console.error(`ErrorCode: ${error.code}, Message: ${error.message}`); + } + }) + Web({ src: 'www.example.com', controller: this.controller }) + } + } +} +``` + ### getBackForwardEntries getBackForwardEntries(): BackForwardList @@ -2799,6 +2897,122 @@ struct WebComponent { } ``` +### serializeWebState + +serializeWebState(): Uint8Array + +将当前Webview的页面状态历史记录信息序列化。 + +**系统能力:** SystemCapability.Web.Webview.Core + +**返回值:** + +| 类型 | 说明 | +| ---------- | --------------------------------------------- | +| Uint8Array | 当前Webview的页面状态历史记录序列化后的数据。 | + +**错误码:** + +以下错误码的详细介绍请参见 [webview错误码](../errorcodes/errorcode-webview.md) + +| 错误码ID | 错误信息 | +| -------- | ------------------------------------------------------------ | +| 17100001 | Init error. The WebviewController must be associated with a Web component. | + +**示例:** + +```ts +// xxx.ets +import web_webview from '@ohos.web.webview'; +import fileio from '@ohos.fileio'; + +@Entry +@Component +struct WebComponent { + controller: web_webview.WebviewController = new web_webview.WebviewController(); + + build() { + Column() { + Button('serializeWebState') + .onClick(() => { + try { + let state = this.controller.serializeWebState(); + let path = globalThis.AbilityContext.cacheDir; + path += '/WebState'; + let fd = fileio.openSync(path, 0o2 | 0o100, 0o666); + fileio.writeSync(fd, state.buffer); + fileio.closeSync(fd); + } catch (error) { + console.error(`ErrorCode: ${error.code}, Message: ${error.message}`); + } + }) + Web({ src: 'www.example.com', controller: this.controller }) + } + } +} +``` + +### restoreWebState + +restoreWebState(state: Uint8Array): void + +当前Webview从序列化数据中恢复页面状态历史记录。 + +**系统能力:** SystemCapability.Web.Webview.Core + +**参数:** + +| 参数名 | 类型 | 必填 | 说明 | +| ------ | ---------- | ---- | ---------------------------- | +| state | Uint8Array | 是 | 页面状态历史记录序列化数据。 | + +**错误码:** + +以下错误码的详细介绍请参见 [webview错误码](../errorcodes/errorcode-webview.md) + +| 错误码ID | 错误信息 | +| -------- | ------------------------------------------------------------ | +| 17100001 | Init error. The WebviewController must be associated with a Web component. | + +**示例:** + +```ts +// xxx.ets +import web_webview from '@ohos.web.webview'; +import fileio from '@ohos.fileio'; + +@Entry +@Component +struct WebComponent { + controller: web_webview.WebviewController = new web_webview.WebviewController(); + + build() { + Column() { + Button('RestoreWebState') + .onClick(() => { + try { + let path = globalThis.AbilityContext.cacheDir; + path += '/WebState'; + let fd = fileio.openSync(path, 0o002, 0o666); + let stat = fileio.fstatSync(fd); + let size = stat.size; + let buf = new ArrayBuffer(size); + fileio.read(fd, buf, (err, data) => { + if (data) { + this.controller.restoreWebState(new Uint8Array(data.buffer)); + } + fileio.closeSync(fd); + }); + } catch (error) { + console.error(`ErrorCode: ${error.code}, Message: ${error.message}`); + } + }) + Web({ src: 'www.example.com', controller: this.controller }) + } + } +} +``` + ### customizeSchemes static customizeSchemes(schemes: Array\): void @@ -4469,6 +4683,8 @@ Web组件返回的请求/响应头对象。 用于描述[WebMessagePort](#webmessageport)所支持的数据类型。 +**系统能力:** SystemCapability.Web.Webview.Core + | 类型 | 说明 | | -------- | -------------------------------------- | | string | 字符串类型数据。 | @@ -4492,10 +4708,10 @@ Web组件返回的请求/响应头对象。 **系统能力:** SystemCapability.Web.Webview.Core -| 名称 | 类型 | 可读 | 可写 | 说明 | -| ------------ | ------ | ---- | ---- | ---------------------------- | -| currentIndex | number | 是 | 否 | 当前页面在页面历史列表中的索引。 | -| size | number | 是 | 否 | 历史列表中索引的数量。 | +| 名称 | 类型 | 可读 | 可写 | 说明 | +| ------------ | ------ | ---- | ---- | ------------------------------------------------------------ | +| currentIndex | number | 是 | 否 | 当前在页面历史列表中的索引。 | +| size | number | 是 | 否 | 历史列表中索引的数量,最多保存50条,超过时起始记录会被覆盖。 | ### getItemAtIndex -- GitLab