未验证 提交 13e00f83 编写于 作者: O openharmony_ci 提交者: Gitee

!17965 add Sample code for Customized UserAgent

Merge pull request !17965 from 李想/master
...@@ -234,7 +234,7 @@ postMessageEventExt(message: WebMessageExt): void ...@@ -234,7 +234,7 @@ postMessageEventExt(message: WebMessageExt): void
| 参数名 | 类型 | 必填 | 说明 | | 参数名 | 类型 | 必填 | 说明 |
| ------- | ------ | ---- | :------------- | | ------- | ------ | ---- | :------------- |
| message | [WebMessageExt](#webmessageext) | 是 | 要发送的消息。 | | message | [WebMessageExt](#webmessageext10) | 是 | 要发送的消息。 |
**错误码:** **错误码:**
...@@ -491,7 +491,7 @@ static setHttpDns(secureDnsMode:SecureDnsMode, secureDnsConfig:string): void ...@@ -491,7 +491,7 @@ static setHttpDns(secureDnsMode:SecureDnsMode, secureDnsConfig:string): void
| 参数名 | 类型 | 必填 | 说明 | | 参数名 | 类型 | 必填 | 说明 |
| ------------------ | ------- | ---- | ------------- | | ------------------ | ------- | ---- | ------------- |
| secureDnsMode | [SecureDnsMode](#securednsmode) | 是 | 使用HTTPDNS的模式。| | secureDnsMode | [SecureDnsMode](#securednsmode10) | 是 | 使用HTTPDNS的模式。|
| secureDnsConfig | string | 是 | HTTPDNS server的配置,必须是https协议并且只允许配置一个server。 | | secureDnsConfig | string | 是 | HTTPDNS server的配置,必须是https协议并且只允许配置一个server。 |
**示例:** **示例:**
...@@ -2137,24 +2137,24 @@ struct WebComponent { ...@@ -2137,24 +2137,24 @@ struct WebComponent {
this.ports = this.controller.createWebMessagePorts(); this.ports = this.controller.createWebMessagePorts();
// 2、在应用侧的消息端口(如端口1)上注册回调事件。 // 2、在应用侧的消息端口(如端口1)上注册回调事件。
this.ports[1].onMessageEvent((result: web_webview.WebMessage) => { this.ports[1].onMessageEvent((result: web_webview.WebMessage) => {
let msg = 'Got msg from HTML:'; let msg = 'Got msg from HTML:';
if (typeof(result) == "string") { if (typeof(result) == "string") {
console.log("received string message from html5, string is:" + result); console.log("received string message from html5, string is:" + result);
msg = msg + result; msg = msg + result;
} else if (typeof(result) == "object") { } else if (typeof(result) == "object") {
if (result instanceof ArrayBuffer) { if (result instanceof ArrayBuffer) {
console.log("received arraybuffer from html5, length is:" + result.byteLength); console.log("received arraybuffer from html5, length is:" + result.byteLength);
msg = msg + "lenght is " + result.byteLength; msg = msg + "lenght is " + result.byteLength;
} else {
console.log("not support");
}
} else { } else {
console.log("not support"); console.log("not support");
} }
this.receivedFromHtml = msg; } else {
}) console.log("not support");
// 3、将另一个消息端口(如端口0)发送到HTML侧,由HTML侧保存并使用。 }
this.controller.postMessage('__init_port__', [this.ports[0]], '*'); this.receivedFromHtml = msg;
})
// 3、将另一个消息端口(如端口0)发送到HTML侧,由HTML侧保存并使用。
this.controller.postMessage('__init_port__', [this.ports[0]], '*');
} catch (error) { } catch (error) {
console.error(`ErrorCode: ${error.code}, Message: ${error.message}`); console.error(`ErrorCode: ${error.code}, Message: ${error.message}`);
} }
...@@ -2523,6 +2523,37 @@ struct WebComponent { ...@@ -2523,6 +2523,37 @@ struct WebComponent {
} }
``` ```
支持开发者基于默认的UserAgent去定制UserAgent。
```ts
// xxx.ets
import web_webview from '@ohos.web.webview';
@Entry
@Component
struct WebComponent {
controller: web_webview.WebviewController = new web_webview.WebviewController();
@State ua: string = ""
aboutToAppear():void {
web_webview.once('webInited', () => {
try {
// 应用侧用法示例,定制UserAgent。
this.ua = this.controller.getUserAgent() + 'xxx';
} catch(error) {
console.error(`ErrorCode: ${error.code}, Message: ${error.message}`);
}
})
}
build() {
Column() {
Web({ src: 'www.example.com', controller: this.controller })
.userAgent(this.ua)
}
}
}
```
### getTitle ### getTitle
getTitle(): string getTitle(): string
...@@ -3191,7 +3222,7 @@ import image from "@ohos.multimedia.image" ...@@ -3191,7 +3222,7 @@ import image from "@ohos.multimedia.image"
@Component @Component
struct WebComponent { struct WebComponent {
controller: web_webview.WebviewController = new web_webview.WebviewController(); controller: web_webview.WebviewController = new web_webview.WebviewController();
@State pixelmap: image.PixelMap = undefined; @State pixelmap: image.PixelMap = undefined;
build() { build() {
Column() { Column() {
...@@ -3297,12 +3328,12 @@ struct WebComponent { ...@@ -3297,12 +3328,12 @@ struct WebComponent {
.onClick(() => { .onClick(() => {
try { try {
this.controller.hasImage((error, data) => { this.controller.hasImage((error, data) => {
if (error) { if (error) {
console.info(`hasImage error: ` + JSON.stringify(error)) console.info(`hasImage error: ` + JSON.stringify(error))
return; return;
} }
console.info("hasImage: " + data); console.info("hasImage: " + data);
}); });
} catch (error) { } catch (error) {
console.error(`ErrorCode: ${error.code}, Message: ${error.message}`); console.error(`ErrorCode: ${error.code}, Message: ${error.message}`);
} }
...@@ -3352,11 +3383,11 @@ struct WebComponent { ...@@ -3352,11 +3383,11 @@ struct WebComponent {
.onClick(() => { .onClick(() => {
try { try {
this.controller.hasImage().then((data) => { this.controller.hasImage().then((data) => {
console.info('hasImage: ' + data); console.info('hasImage: ' + data);
}) })
.catch(function (error) { .catch(function (error) {
console.error("error: " + error); console.error("error: " + error);
}) })
} catch (error) { } catch (error) {
console.error(`Errorcode: ${error.code}, Message: ${error.message}`); console.error(`Errorcode: ${error.code}, Message: ${error.message}`);
} }
...@@ -4131,7 +4162,7 @@ struct WebComponent { ...@@ -4131,7 +4162,7 @@ struct WebComponent {
通过WebCookie可以控制Web组件中的cookie的各种行为,其中每个应用中的所有web组件共享一个WebCookieManager实例。 通过WebCookie可以控制Web组件中的cookie的各种行为,其中每个应用中的所有web组件共享一个WebCookieManager实例。
### getCookie ### getCookie
static getCookie(url: string): string static getCookie(url: string): string
...@@ -4187,7 +4218,7 @@ struct WebComponent { ...@@ -4187,7 +4218,7 @@ struct WebComponent {
} }
``` ```
### setCookie ### setCookie
static setCookie(url: string, value: string): void static setCookie(url: string, value: string): void
...@@ -4238,7 +4269,7 @@ struct WebComponent { ...@@ -4238,7 +4269,7 @@ struct WebComponent {
} }
``` ```
### saveCookieAsync ### saveCookieAsync
static saveCookieAsync(callback: AsyncCallback\<void>): void static saveCookieAsync(callback: AsyncCallback\<void>): void
...@@ -4284,7 +4315,7 @@ struct WebComponent { ...@@ -4284,7 +4315,7 @@ struct WebComponent {
} }
``` ```
### saveCookieAsync ### saveCookieAsync
static saveCookieAsync(): Promise\<void> static saveCookieAsync(): Promise\<void>
...@@ -4331,7 +4362,7 @@ struct WebComponent { ...@@ -4331,7 +4362,7 @@ struct WebComponent {
} }
``` ```
### putAcceptCookieEnabled ### putAcceptCookieEnabled
static putAcceptCookieEnabled(accept: boolean): void static putAcceptCookieEnabled(accept: boolean): void
...@@ -4372,7 +4403,7 @@ struct WebComponent { ...@@ -4372,7 +4403,7 @@ struct WebComponent {
} }
``` ```
### isCookieAllowed ### isCookieAllowed
static isCookieAllowed(): boolean static isCookieAllowed(): boolean
...@@ -4410,7 +4441,7 @@ struct WebComponent { ...@@ -4410,7 +4441,7 @@ struct WebComponent {
} }
``` ```
### putAcceptThirdPartyCookieEnabled ### putAcceptThirdPartyCookieEnabled
static putAcceptThirdPartyCookieEnabled(accept: boolean): void static putAcceptThirdPartyCookieEnabled(accept: boolean): void
...@@ -4451,7 +4482,7 @@ struct WebComponent { ...@@ -4451,7 +4482,7 @@ struct WebComponent {
} }
``` ```
### isThirdPartyCookieAllowed ### isThirdPartyCookieAllowed
static isThirdPartyCookieAllowed(): boolean static isThirdPartyCookieAllowed(): boolean
...@@ -4489,7 +4520,7 @@ struct WebComponent { ...@@ -4489,7 +4520,7 @@ struct WebComponent {
} }
``` ```
### existCookie ### existCookie
static existCookie(): boolean static existCookie(): boolean
...@@ -4527,7 +4558,7 @@ struct WebComponent { ...@@ -4527,7 +4558,7 @@ struct WebComponent {
} }
``` ```
### deleteEntireCookie ### deleteEntireCookie
static deleteEntireCookie(): void static deleteEntireCookie(): void
...@@ -4558,7 +4589,7 @@ struct WebComponent { ...@@ -4558,7 +4589,7 @@ struct WebComponent {
} }
``` ```
### deleteSessionCookie ### deleteSessionCookie
static deleteSessionCookie(): void static deleteSessionCookie(): void
......
...@@ -4328,7 +4328,7 @@ resend(): void ...@@ -4328,7 +4328,7 @@ resend(): void
} }
``` ```
### cancel<sup>9+</sup> ### cancel<sup>9+</sup>
cancel(): void cancel(): void
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册