diff --git a/arkui/ace_ets_web_dev_three/entry/src/main/ets/MainAbility/pages/web.ets b/arkui/ace_ets_web_dev_three/entry/src/main/ets/MainAbility/pages/web.ets index 9c9b2053b834aecf20d97e3b4ee0a9c14a28c9ff..79afd9a91f37e154b34365279eca80ef6dc0f599 100644 --- a/arkui/ace_ets_web_dev_three/entry/src/main/ets/MainAbility/pages/web.ets +++ b/arkui/ace_ets_web_dev_three/entry/src/main/ets/MainAbility/pages/web.ets @@ -33,6 +33,8 @@ struct Index { scheme2: web_webview.WebCustomScheme = {schemeName: "name2", isSupportCORS: true, isSupportFetch: true} scheme3: web_webview.WebCustomScheme = {schemeName: "name3", isSupportCORS: true, isSupportFetch: true} responseweb: WebResourceResponse = new WebResourceResponse() + + @State str:string="emitLoadUrl" @State text:boolean=false @State num:number=0 @@ -40,11 +42,21 @@ struct Index { @State mode1: WebDarkMode = WebDarkMode.On @State mode2: WebDarkMode = WebDarkMode.Auto @State enterPageEnd: boolean = false + @State getString:boolean =false + @State getNum:boolean =false + @State getBoo:boolean =false + @State getArrayBuffer:boolean =false + @State getArray:boolean =false + @State msg1:string="" + @State type:number=0 - - ports: web_webview.WebMessagePort[]; + message: web_webview.WebMessageExt = new web_webview.WebMessageExt(); + ports: web_webview.WebMessagePort[] = null; + nativePort: web_webview.WebMessagePort = null; + @State webMessResult:string ="" + testObj = { testOne: (data) => { Utils.emitEvent(data,13) @@ -586,6 +598,268 @@ struct Index { } break; } + case "emitJsMessExtGetString":{ + try { + this.getString = true; + this.controller.loadUrl("resource://rawfile/index.html") + } catch(error) { + console.error(`ErrorCode: ${error.code}, Message: ${error.message}`); + } + break; + } + case "emitJsMessExtGetNum":{ + try { + this.getNum = true; + this.controller.loadUrl("resource://rawfile/index.html") + } catch(error) { + console.error(`ErrorCode: ${error.code}, Message: ${error.message}`); + } + break; + } + case "emitJsMessExtGetBoo":{ + try { + this.getBoo = true; + this.controller.loadUrl("resource://rawfile/index.html") + } catch(error) { + console.error(`ErrorCode: ${error.code}, Message: ${error.message}`); + } + break; + } + case "emitJsMessExtGetArrayBuffer":{ + try { + this.getArrayBuffer = true; + this.controller.loadUrl("resource://rawfile/index.html") + } catch(error) { + console.error(`ErrorCode: ${error.code}, Message: ${error.message}`); + } + break; + } + case "emitJsMessExtGetArray":{ + try { + this.getArray = true; + this.controller.loadUrl("resource://rawfile/index.html") + } catch(error) { + console.error(`ErrorCode: ${error.code}, Message: ${error.message}`); + } + break; + } + case "emitWebMessExtSetString":{ + try { + this.controller.loadUrl($rawfile('webMessageExt.html')); + console.log("In eTS side send true start"); + if (this.nativePort) { + setTimeout(()=>{ + this.message.setType(1); + this.message.setString("helloFromEts"); + this.nativePort.postMessageEventExt(this.message); + },2000) + } + setTimeout(()=>{ + this.controller.runJavaScript("postStringToHtml()",(error, result) => { + console.log(`The emitWebMessExtSetString() return value is: ${result}`); + this.webMessResult = result + Utils.emitEvent(this.webMessResult,506) + }) + },3000) + } + catch (error) { + console.log("In eTS side send message catch error:" + error.code + ", msg:" + error.message); + } + break; + } + case "emitWebMessExtGetString":{ + try { + this.controller.loadUrl($rawfile('webMessageExt.html')); + this.controller.runJavaScript("postStringToApp()",(error, result) => { + console.log(`The emitWebMessExtGetString() return value is: ${result}`); + }) + } + catch (error) { + console.log("In eTS side send message catch error:" + error.code + ", msg:" + error.message); + } + break; + } + case "emitWebMessExtSetNumber":{ + try { + this.controller.loadUrl($rawfile('webMessageExt.html')); + console.log("In eTS side send true start"); + if (this.nativePort) { + setTimeout(()=>{ + this.message.setNumber(1); + this.nativePort.postMessageEventExt(this.message); + },2000) + } + setTimeout(()=>{ + this.controller.runJavaScript("postNumberToHtml()",(error, result) => { + console.log(`The emitWebMessExtSetNumber() return value is: ${result}`); + Utils.emitEvent(result,507) + }) + },3000) + } + catch (error) { + console.log("In eTS side send message catch error:" + error.code + ", msg:" + error.message); + } + break; + } + case "emitWebMessExtGetNumber":{ + try { + this.controller.loadUrl($rawfile('webMessageExt.html')); + this.controller.runJavaScript("postNumberToApp()",(error, result) => { + console.log(`The emitWebMessExtGetNumber() return value is: ${result}`); + }) + } + catch (error) { + console.log("In eTS side send message catch error:" + error.code + ", msg:" + error.message); + } + break; + } + case "emitWebMessExtSetBoolean":{ + try { + this.controller.loadUrl($rawfile('webMessageExt.html')); + console.log("In eTS side send true start"); + if (this.nativePort) { + setTimeout(()=>{ + this.message.setBoolean(true); + this.nativePort.postMessageEventExt(this.message); + },2000) + } + setTimeout(()=>{ + this.controller.runJavaScript("postBooleanToHtml()",(error, result) => { + console.log(`The emitWebMessExtSetBoolean() return value is: ${result}`); + Utils.emitEvent(result,509) + }) + },3000) + } + catch (error) { + console.log("In eTS side send message catch error:" + error.code + ", msg:" + error.message); + } + break; + } + case "emitWebMessExtGetBoolean":{ + try { + this.controller.loadUrl($rawfile('webMessageExt.html')); + this.controller.runJavaScript("postBooleanToApp()",(error, result) => { + console.log(`The emitWebMessExtGetBoolean() return value is: ${result}`); + }) + } + catch (error) { + console.log("In eTS side send message catch error:" + error.code + ", msg:" + error.message); + } + break; + } + case "emitWebMessExtSetArray":{ + try { + this.controller.loadUrl($rawfile('webMessageExt.html')); + console.log("In eTS side send true start"); + if (this.nativePort) { + setTimeout(()=>{ + const fruits = []; + fruits.push("apple"); + this.message.setArray(fruits); + this.nativePort.postMessageEventExt(this.message); + },2000) + } + setTimeout(()=>{ + this.controller.runJavaScript("postArrayToHtml()",(error, result) => { + console.log(`The emitWebMessExtSetArray() return value is: ${result}`); + Utils.emitEvent(result,511) + }) + },3000) + } + catch (error) { + console.log("In eTS side send message catch error:" + error.code + ", msg:" + error.message); + } + break; + } + case "emitWebMessExtGetArray":{ + try { + this.controller.loadUrl($rawfile('webMessageExt.html')); + this.controller.runJavaScript("postArrayToApp()",(error, result) => { + console.log(`The emitWebMessExtGetArray() return value is: ${result}`); + }) + } + catch (error) { + console.log("In eTS side send message catch error:" + error.code + ", msg:" + error.message); + } + break; + } + case "emitWebMessExtSetArrayBuffer":{ + try { + this.controller.loadUrl($rawfile('webMessageExt.html')); + console.log("In eTS side send true start"); + if (this.nativePort) { + setTimeout(()=>{ + let buffer = new ArrayBuffer(8); + this.message.setArrayBuffer(buffer); + this.nativePort.postMessageEventExt(this.message); + },2000) + } + setTimeout(()=>{ + this.controller.runJavaScript("postArrayBufferToHtml()",(error, result) => { + console.log(`The emitWebMessExtSetArrayBuffer() return value is: ${result}`); + Utils.emitEvent(result,513) + }) + },3000) + } + catch (error) { + console.log("In eTS side send message catch error:" + error.code + ", msg:" + error.message); + } + break; + } + case "emitWebMessExtGetArrayBuffer":{ + try { + this.controller.loadUrl($rawfile('webMessageExt.html')); + this.controller.runJavaScript("postArrayBufferToApp()",(error, result) => { + console.log(`The emitWebMessExtGetArrayBuffer() return value is: ${result}`); + }) + } + catch (error) { + console.log("In eTS side send message catch error:" + error.code + ", msg:" + error.message); + } + break; + } + case "emitWebMessExtSetError":{ + try { + console.log("URL Error"); + throw new URIError("URI seq") + } + catch (e) { + console.log("hwd URIError:"+(e instanceof SyntaxError)); + console.log("hwd URIError:"+e.message); + console.log("hwd URIError:"+e.name); + if (this.nativePort) { + setTimeout(()=>{ + var message =new web_webview.WebMessageExt(); + message.setType(web_webview.WebMessageType.ERROR) + this.message.setError(e); + this.nativePort.postMessageEventExt(message); + },2000) + } + } + setTimeout(()=>{ + this.controller.runJavaScript("postErrorToHtml()",(error, result) => { + console.log(`The emitWebMessExtSetError() return value is: ${result}`); + Utils.emitEvent(result,515) + }) + },3000) + break; + } + case "emitWebMessExtGetError":{ + try { + this.controller.loadUrl($rawfile('webMessageExt.html')); + this.controller.runJavaScript("postErrorToApp()",(error, result) => { + console.log(`The emitWebMessExtGetError() return value is: ${result}`); + }) + } + catch (error) { + console.log("In eTS side send message catch error:" + error.code + ", msg:" + error.message); + } + break; + } + + + + default: console.info("can not match case") } @@ -624,6 +898,149 @@ struct Index { Utils.emitEvent(1,446) this.enterPageEnd=false } + if(this.getString){ + try { + this.controller.runJavaScriptExt( + 'getStringForJs()', + (error, result) => { + if (error) { + console.info(`run JavaScript error: ` + JSON.stringify(error)) + return; + } + var type = result.getType() + if(web_webview.JsMessageType.STRING == type){ + this.msg1 = "result type:" + typeof (result.getString()); + Utils.emitEvent(this.msg1,500) + } + }); + console.info('urlgetstring: ', event.url); + } catch (error) { + console.error(`ErrorCode: ${error.code}, Message: ${error.message}`); + } + } + if(this.getNum){ + try { + this.controller.runJavaScriptExt( + 'getNumForJs()', + (error, result) => { + if (error) { + console.info(`run JavaScript error: ` + JSON.stringify(error)) + return; + } + var type = result.getType() + if(web_webview.JsMessageType.NUMBER == type){ + this.msg1 = "result type:" + typeof (result.getNumber()); + Utils.emitEvent(this.msg1,501) + } + }); + console.info('urlgetstring: ', event.url); + } catch (error) { + console.error(`ErrorCode: ${error.code}, Message: ${error.message}`); + } + } + if(this.getBoo){ + try { + this.controller.runJavaScriptExt( + 'getBooForJs()', + (error, result) => { + if (error) { + console.info(`run JavaScript error: ` + JSON.stringify(error)) + return; + } + var type = result.getType() + if(web_webview.JsMessageType.BOOLEAN == type){ + this.msg1 = "result type:" + typeof (result.getBoolean()); + Utils.emitEvent(this.msg1,502) + } + }); + console.info('urlgetstring: ', event.url); + } catch (error) { + console.error(`ErrorCode: ${error.code}, Message: ${error.message}`); + } + } + if(this.getArrayBuffer){ + try { + this.controller.runJavaScriptExt( + 'getArrayBufferForJs()', + (error, result) => { + if (error) { + console.info(`run JavaScript error: ` + JSON.stringify(error)) + return; + } + var type = result.getType() + if(web_webview.JsMessageType.ARRAY_BUFFER== type){ + this.msg1 = "result type:" + typeof (result.getArrayBuffer()); + Utils.emitEvent(this.msg1,503) + } + }); + console.info('urlgetstring: ', event.url); + } catch (error) { + console.error(`ErrorCode: ${error.code}, Message: ${error.message}`); + } + } + if(this.getArray){ + try { + this.controller.runJavaScriptExt( + 'getArrayForJs()', + (error, result) => { + if (error) { + console.info(`run JavaScript error: ` + JSON.stringify(error)) + return; + } + var type = result.getType() + if(web_webview.JsMessageType.ARRAY== type){ + this.msg1 = "result type:" + typeof (result.getArray()); + Utils.emitEvent(this.msg1+web_webview.JsMessageType.NOT_SUPPORT,504) + } + }); + console.info('urlgetstring: ', event.url); + } catch (error) { + console.error(`ErrorCode: ${error.code}, Message: ${error.message}`); + } + } + this.getString=false + this.getNum=false + this.getBoo=false + this.getArrayBuffer=false + this.getArray=false + + this.ports = this.controller.createWebMessagePorts(true); + this.controller.postMessage("init_web_messageport", [this.ports[1]], "*"); + this.nativePort = this.ports[0]; + this.nativePort.onMessageEventExt((result) => { + console.log("In eTS side got message"); + try { + this.type =result.getType(); + if(this.type == web_webview.WebMessageType.STRING){ + this.msg1 = "result type:" + typeof (result.getString()); + Utils.emitEvent(this.msg1,505) + } + if(this.type == web_webview.WebMessageType.NUMBER){ + this.msg1 = "result type:" + typeof (result.getNumber()); + Utils.emitEvent(this.msg1+web_webview.WebMessageType.NOT_SUPPORT,508) + } + if(this.type == web_webview.WebMessageType.BOOLEAN){ + this.msg1 = "result type:" + typeof (result.getBoolean()); + Utils.emitEvent(this.msg1,510) + } + if(this.type == web_webview.WebMessageType.ARRAY){ + this.msg1 = "result type:" + typeof (result.getArray()); + Utils.emitEvent(this.msg1,512) + } + if(this.type == web_webview.WebMessageType.ARRAY_BUFFER){ + this.msg1 = "result getArrayBuffer byteLength:" + ((result.getArrayBuffer().byteLength)); + Utils.emitEvent(this.msg1,514) + } + if(this.type == web_webview.WebMessageType.ERROR){ + this.msg1 = "result type:" + typeof (result.getError()); + Utils.emitEvent(this.msg1,516) + } + } + catch (resError) { + console.log(`log error code: ${resError.code}, Message: ${resError.message}`); + } + }) + console.log('url:' + event.url) }) .darkMode(this.mode) diff --git a/arkui/ace_ets_web_dev_three/entry/src/main/ets/MainAbility/pages/webTwo.ets b/arkui/ace_ets_web_dev_three/entry/src/main/ets/MainAbility/pages/webTwo.ets index e0a2a30420e01ba890bba137aab3428a06cde38c..021a0d9b57dfab2caa8afc72cf9b9d9bfff09642 100644 --- a/arkui/ace_ets_web_dev_three/entry/src/main/ets/MainAbility/pages/webTwo.ets +++ b/arkui/ace_ets_web_dev_three/entry/src/main/ets/MainAbility/pages/webTwo.ets @@ -101,6 +101,7 @@ struct indexTwo { },3000) break; } + default: console.info("can not match case") } diff --git a/arkui/ace_ets_web_dev_three/entry/src/main/ets/test/WebJsunit.test.ets b/arkui/ace_ets_web_dev_three/entry/src/main/ets/test/WebJsunit.test.ets index ff9d268434d72cec6e9cc378440e128d073c9885..292092036c7a6e10b60764c771a4b945148b9da0 100644 --- a/arkui/ace_ets_web_dev_three/entry/src/main/ets/test/WebJsunit.test.ets +++ b/arkui/ace_ets_web_dev_three/entry/src/main/ets/test/WebJsunit.test.ets @@ -426,10 +426,185 @@ export default function webJsunit() { *tc.desic Save current page */ it('GetAccessibleGeolocation',0,async function(done){ - emitKey="emitGetAccessibleGeolocation"; + emitKey="emitJsMessExtGetString"; Utils.registerEvent("GetAccessibleGeolocation",true,451,done); sendEventByKey('webcomponent',10,''); }) - + /* + *tc.number SUB_ACE_BASIC_ETS_API_039 + *tc.name JsMessExtGetString + *tc.desic Save current page + */ + it('JsMessExtGetString',0,async function(done){ + emitKey="emitJsMessExtGetNum"; + Utils.registerEvent("JsMessExtGetString","result type:string",500,done); + sendEventByKey('webcomponent',10,''); + }) + /* + *tc.number SUB_ACE_BASIC_ETS_API_040 + *tc.name JsMessExtGetNum + *tc.desic Save current page + */ + it('JsMessExtGetNum',0,async function(done){ + emitKey="emitJsMessExtGetBoo"; + Utils.registerEvent("JsMessExtGetNum","result type:number",501,done); + sendEventByKey('webcomponent',10,''); + }) + /* + *tc.number SUB_ACE_BASIC_ETS_API_041 + *tc.name JsMessExtGetBoo + *tc.desic Save current page + */ + it('JsMessExtGetBoo',0,async function(done){ + emitKey="emitJsMessExtGetArrayBuffer"; + Utils.registerEvent("JsMessExtGetBoo","result type:boolean",502,done); + sendEventByKey('webcomponent',10,''); + }) + /* + *tc.number SUB_ACE_BASIC_ETS_API_042 + *tc.name JsMessExtGetArrayBuffer + *tc.desic Save current page + */ + it('JsMessExtGetArrayBuffer',0,async function(done){ + emitKey="emitJsMessExtGetArray"; + Utils.registerEvent("JsMessExtGetArrayBuffer","result type:object",503,done); + sendEventByKey('webcomponent',10,''); + }) + /* + *tc.number SUB_ACE_BASIC_ETS_API_043 + *tc.name JsMessExtGetArray + *tc.desic Save current page + */ + it('JsMessExtGetArray',0,async function(done){ + emitKey="emitWebMessExtSetString"; + Utils.registerEvent("JsMessExtGetArray","result type:object0",504,done); + sendEventByKey('webcomponent',10,''); + }) + /* + *tc.number SUB_ACE_BASIC_ETS_API_044 + *tc.name WebMessExtSetString + *tc.desic Save current page + */ + it('WebMessExtSetString',0,async function(done){ + emitKey="emitWebMessExtGetString"; + var result="String:helloFromEts" + Utils.registerEvent("WebMessExtSetString",JSON.stringify(result),506,done); + sendEventByKey('webcomponent',10,''); + }) + /* + *tc.number SUB_ACE_BASIC_ETS_API_045 + *tc.name WebMessExtGetString + *tc.desic Save current page + */ + it('WebMessExtGetString',0,async function(done){ + emitKey="emitWebMessExtSetNumber"; + Utils.registerEvent("WebMessExtGetString","result type:string",505,done); + sendEventByKey('webcomponent',10,''); + }) + /* + *tc.number SUB_ACE_BASIC_ETS_API_046 + *tc.name WebMessExtSetNumber + *tc.desic Save current page + */ + it('WebMessExtSetNumber',0,async function(done){ + emitKey="emitWebMessExtGetNumber"; + var result="Number:1" + Utils.registerEvent("WebMessExtSetNumber",JSON.stringify(result),507,done); + sendEventByKey('webcomponent',10,''); + }) + /* + *tc.number SUB_ACE_BASIC_ETS_API_047 + *tc.name WebMessExtGetNumber + *tc.desic Save current page + */ + it('WebMessExtGetNumber',0,async function(done){ + emitKey="emitWebMessExtSetBoolean"; + Utils.registerEvent("WebMessExtGetNumber","result type:number0",508,done); + sendEventByKey('webcomponent',10,''); + }) + /* + *tc.number SUB_ACE_BASIC_ETS_API_048 + *tc.name WebMessExtSetBoolean + *tc.desic Save current page + */ + it('WebMessExtSetBoolean',0,async function(done){ + emitKey="emitWebMessExtGetBoolean"; + var result="Boolean:true" + Utils.registerEvent("WebMessExtSetBoolean",JSON.stringify(result),509,done); + sendEventByKey('webcomponent',10,''); + }) + /* + *tc.number SUB_ACE_BASIC_ETS_API_049 + *tc.name WebMessExtGetBoolean + *tc.desic Save current page + */ + it('WebMessExtGetBoolean',0,async function(done){ + emitKey="emitWebMessExtSetArray"; + Utils.registerEvent("WebMessExtGetBoolean","result type:boolean",510,done); + sendEventByKey('webcomponent',10,''); + }) + /* + *tc.number SUB_ACE_BASIC_ETS_API_050 + *tc.name WebMessExtSetArray + *tc.desic Save current page + */ + it('WebMessExtSetArray',0,async function(done){ + emitKey="emitWebMessExtGetArray"; + var result="Array len:1, value:apple" + Utils.registerEvent("WebMessExtSetArray",JSON.stringify(result),511,done); + sendEventByKey('webcomponent',10,''); + }) + /* + *tc.number SUB_ACE_BASIC_ETS_API_051 + *tc.name WebMessExtGetArray + *tc.desic Save current page + */ + it('WebMessExtGetArray',0,async function(done){ + emitKey="emitWebMessExtSetArrayBuffer"; + Utils.registerEvent("WebMessExtGetArray","result type:object",512,done); + sendEventByKey('webcomponent',10,''); + }) + /* + *tc.number SUB_ACE_BASIC_ETS_API_052 + *tc.name WebMessExtSetArrayBuffer + *tc.desic Save current page + */ + it('WebMessExtSetArrayBuffer',0,async function(done){ + emitKey="emitWebMessExtGetArrayBuffer"; + var result="ArrayBuffer:8" + Utils.registerEvent("WebMessExtSetArrayBuffer",JSON.stringify(result),513,done); + sendEventByKey('webcomponent',10,''); + }) + /* + *tc.number SUB_ACE_BASIC_ETS_API_053 + *tc.name WebMessExtGetArrayBuffer + *tc.desic Save current page + */ + it('WebMessExtGetArrayBuffer',0,async function(done){ + emitKey="emitWebMessExtSetError"; + Utils.registerEvent("WebMessExtGetArrayBuffer","result getArrayBuffer byteLength:8",514,done); + sendEventByKey('webcomponent',10,''); + }) + /* + *tc.number SUB_ACE_BASIC_ETS_API_054 + *tc.name WebMessExtSetError + *tc.desic Save current page + */ + it('WebMessExtSetError',0,async function(done){ + emitKey="emitWebMessExtGetError"; + var result="Error:SyntaxError, msg:" + Utils.registerEvent("WebMessExtSetError",JSON.stringify(result),515,done); + sendEventByKey('webcomponent',10,''); + }) + /* + *tc.number SUB_ACE_BASIC_ETS_API_055 + *tc.name WebMessExtGetError + *tc.desic Save current page + */ + it('WebMessExtGetError',0,async function(done){ + emitKey="emitWebMessExtGetError"; + Utils.registerEvent("WebMessExtGetError","result type:object",516,done); + sendEventByKey('webcomponent',10,''); + }) }) } diff --git a/arkui/ace_ets_web_dev_three/entry/src/main/resources/rawfile/index.html b/arkui/ace_ets_web_dev_three/entry/src/main/resources/rawfile/index.html index b32d947f498f4bdbda9dab7f68edeea052600af6..f77763767473ead146d883026ac0d2165aa5b6d6 100644 --- a/arkui/ace_ets_web_dev_three/entry/src/main/resources/rawfile/index.html +++ b/arkui/ace_ets_web_dev_three/entry/src/main/resources/rawfile/index.html @@ -70,5 +70,24 @@ let str = objName.testOne('Web') return str } + function getStringForJs() { + return "hello, world"; + } + function getNumForJs() { + return 1; + } + function getBooForJs() { + return true; + } + function getArrayBufferForJs() { + const buffer = new ArrayBuffer(8); + const view = new Int32Array(buffer); + return view; + } + function getArrayForJs() { + const fruits = []; + fruits.push("banana", "apple", "peach"); + return fruits; + } diff --git a/arkui/ace_ets_web_dev_three/entry/src/main/resources/rawfile/index.js b/arkui/ace_ets_web_dev_three/entry/src/main/resources/rawfile/index.js new file mode 100644 index 0000000000000000000000000000000000000000..7e8694c446f59ccc1286350822615d7ce54e1d83 --- /dev/null +++ b/arkui/ace_ets_web_dev_three/entry/src/main/resources/rawfile/index.js @@ -0,0 +1,153 @@ +/* + * Copyright (c) 2021~2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +var h5Port; +window.addEventListener('message', function(event) { + console.info("htmlSSSSS") + if (event.data == 'init_web_messageport') { + if(event.ports[0] != null) { + h5Port = event.ports[0]; // 1. 保存从ets侧发送过来的端口 + h5Port.onmessage = function(event) { + console.info("hwd In html got message"); + // 2. 接收ets侧发送过来的消息. + var result = event.data; + console.info("In html got message, typeof: ", typeof(result)); + console.info("In html got message, result: ", (result)); + if (typeof(result) == "string") { + console.log("In html got message, String: ", result); + document.getElementById("msg").innerHTML = "String:" + result; + } else if (typeof(result) == "number") { + console.log("In html side got message, number: ", result); + document.getElementById("msg").innerHTML = "Number:" + result; + } else if (typeof(result) == "boolean") { + console.log("In html side got message, boolean: ", result); + document.getElementById("msg").innerHTML = "Boolean:" + result; + } else if (typeof(result) == "object") { + if (result instanceof ArrayBuffer) { + document.getElementById("msg2").innerHTML = "ArrayBuffer:" + result.byteLength; + console.log("In html got message, byteLength: ", result.byteLength); + } else if (result instanceof Error) { + console.log("In html error message, err:" + (result)); + console.log("In html error message, typeof err:" + typeof(result)); + document.getElementById("msg2").innerHTML = "Error:" + result.name + ", msg:" + result.message; + } else if (result instanceof Array) { + console.log("In html got message, Array"); + console.log("In html got message, Array length:" + result.length); + console.log("In html got message, Array[0]:" + (result[0])); + console.log("In html got message, typeof Array[0]:" + typeof(result[0])); + document.getElementById("msg2").innerHTML = "Array len:" + result.length + ", value:" + result; + } else { + console.log("In html got message, not any instance of support type"); + document.getElementById("msg").innerHTML = "not any instance of support type"; + } + } else { + console.log("In html got message, not support type"); + document.getElementById("msg").innerHTML = "not support type"; + } + } + h5Port.onmessageerror = (event) => { + console.error(`hwd In html Error receiving message: ${event}`); + }; + } + } +}) + +// 使用h5Port往ets侧发送String类型的消息. +function postStringToApp() { + if (h5Port) { + console.log("In html send string message"); + h5Port.postMessage("hello"); + console.log("In html send string message end"); + } else { + console.error("In html h5port is null, please init first"); + } +} +function postNumberToApp() { + if (h5Port) { + console.log("In html send Number message"); + h5Port.postMessage(1); + console.log("In html send Number message end"); + } else { + console.error("In html h5port is null, please init first"); + } +} +function postBooleanToApp() { + if (h5Port) { + console.log("In html send Boolean message"); + h5Port.postMessage(true); + console.log("In html send Boolean message end"); + } else { + console.error("In html h5port is null, please init first"); + } +} +function postArrayToApp() { + if (h5Port) { + console.log("In html send Array message"); + const fruits = []; + fruits.push("apple"); + h5Port.postMessage(fruits); + console.log("In html send Array message end"); + } else { + console.error("In html h5port is null, please init first"); + } +} +function postArrayBufferToApp() { + if (h5Port) { + console.log("In html send Buffer message"); + let buffer = new ArrayBuffer(8); + h5Port.postMessage(buffer); + console.log("In html send Buffer message end"); + } else { + console.error("In html h5port is null, please init first"); + } +} +function postErrorToApp() { + try { + throw new URIError("URI seq 0222") + } + catch (e){ + console.log("hwd URIError:"+(e instanceof SyntaxError)); + console.log("hwd URIError:"+e.message); + console.log("hwd URIError:"+e.name); + if (h5Port) { + h5Port.postMessage(e); + } + } +} + + +function postStringToHtml() { + var a=document.getElementById("msg").innerText + return a +} +function postNumberToHtml() { + var a=document.getElementById("msg").innerText + return a +} +function postBooleanToHtml() { + var a=document.getElementById("msg").innerText + return a +} +function postArrayToHtml() { + var a=document.getElementById("msg2").innerText + return a +} +function postArrayBufferToHtml() { + var a=document.getElementById("msg2").innerText + return a +} +function postErrorToHtml() { + var a=document.getElementById("msg2").innerText + return a +} diff --git a/arkui/ace_ets_web_dev_three/entry/src/main/resources/rawfile/webMessageExt.html b/arkui/ace_ets_web_dev_three/entry/src/main/resources/rawfile/webMessageExt.html new file mode 100644 index 0000000000000000000000000000000000000000..fc618dbcf5716e80a0c419f2e32f7bbbda8dce8d --- /dev/null +++ b/arkui/ace_ets_web_dev_three/entry/src/main/resources/rawfile/webMessageExt.html @@ -0,0 +1,17 @@ + + + + + WebView MessagePort Demo + + + +

Html5 Send and Receive Message

+

Receive string:

+

Receive arraybuffer:

+
+
+
+ + + \ No newline at end of file