未验证 提交 2346bf2d 编写于 作者: O openharmony_ci 提交者: Gitee

!18047 add Sample code for Customized UserAgent(3.2release)

Merge pull request !18047 from 李想/OpenHarmony-3.2-Release
...@@ -1606,24 +1606,24 @@ struct WebComponent { ...@@ -1606,24 +1606,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}`);
} }
...@@ -1992,6 +1992,37 @@ struct WebComponent { ...@@ -1992,6 +1992,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
...@@ -2660,7 +2691,7 @@ import image from "@ohos.multimedia.image" ...@@ -2660,7 +2691,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() {
...@@ -2766,12 +2797,12 @@ struct WebComponent { ...@@ -2766,12 +2797,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}`);
} }
...@@ -2821,11 +2852,11 @@ struct WebComponent { ...@@ -2821,11 +2852,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}`);
} }
...@@ -3240,7 +3271,7 @@ struct WebComponent { ...@@ -3240,7 +3271,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
...@@ -3296,7 +3327,7 @@ struct WebComponent { ...@@ -3296,7 +3327,7 @@ struct WebComponent {
} }
``` ```
### setCookie ### setCookie
static setCookie(url: string, value: string): void static setCookie(url: string, value: string): void
...@@ -3347,7 +3378,7 @@ struct WebComponent { ...@@ -3347,7 +3378,7 @@ struct WebComponent {
} }
``` ```
### saveCookieAsync ### saveCookieAsync
static saveCookieAsync(callback: AsyncCallback\<void>): void static saveCookieAsync(callback: AsyncCallback\<void>): void
...@@ -3393,7 +3424,7 @@ struct WebComponent { ...@@ -3393,7 +3424,7 @@ struct WebComponent {
} }
``` ```
### saveCookieAsync ### saveCookieAsync
static saveCookieAsync(): Promise\<void> static saveCookieAsync(): Promise\<void>
...@@ -3440,7 +3471,7 @@ struct WebComponent { ...@@ -3440,7 +3471,7 @@ struct WebComponent {
} }
``` ```
### putAcceptCookieEnabled ### putAcceptCookieEnabled
static putAcceptCookieEnabled(accept: boolean): void static putAcceptCookieEnabled(accept: boolean): void
...@@ -3481,7 +3512,7 @@ struct WebComponent { ...@@ -3481,7 +3512,7 @@ struct WebComponent {
} }
``` ```
### isCookieAllowed ### isCookieAllowed
static isCookieAllowed(): boolean static isCookieAllowed(): boolean
...@@ -3519,7 +3550,7 @@ struct WebComponent { ...@@ -3519,7 +3550,7 @@ struct WebComponent {
} }
``` ```
### putAcceptThirdPartyCookieEnabled ### putAcceptThirdPartyCookieEnabled
static putAcceptThirdPartyCookieEnabled(accept: boolean): void static putAcceptThirdPartyCookieEnabled(accept: boolean): void
...@@ -3560,7 +3591,7 @@ struct WebComponent { ...@@ -3560,7 +3591,7 @@ struct WebComponent {
} }
``` ```
### isThirdPartyCookieAllowed ### isThirdPartyCookieAllowed
static isThirdPartyCookieAllowed(): boolean static isThirdPartyCookieAllowed(): boolean
...@@ -3598,7 +3629,7 @@ struct WebComponent { ...@@ -3598,7 +3629,7 @@ struct WebComponent {
} }
``` ```
### existCookie ### existCookie
static existCookie(): boolean static existCookie(): boolean
...@@ -3636,7 +3667,7 @@ struct WebComponent { ...@@ -3636,7 +3667,7 @@ struct WebComponent {
} }
``` ```
### deleteEntireCookie ### deleteEntireCookie
static deleteEntireCookie(): void static deleteEntireCookie(): void
...@@ -3667,7 +3698,7 @@ struct WebComponent { ...@@ -3667,7 +3698,7 @@ struct WebComponent {
} }
``` ```
### deleteSessionCookie ### deleteSessionCookie
static deleteSessionCookie(): void static deleteSessionCookie(): void
......
...@@ -3952,7 +3952,7 @@ resend(): void ...@@ -3952,7 +3952,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.
先完成此消息的编辑!
想要评论请 注册