怎样在H5不传对象名的情况下原生收到对方要调用的方法?

例如H5调用方法window.jplugin_utils(“share”), 这种没有对象名,原生怎样能监听到对方调用了这个方法并得到参数,然后再原生代码里实现相关功能。

HarmonyOS
2024-11-06 10:15:37
762浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
superinsect

可通过runJavaScript API,在页面加载后注入代理函数,示例代码:

<!DOCTYPE html> 
  <html lang="en"> 
  <head> 
  <meta charset="UTF-8"> 
  <meta name="viewport" content="width=device-width, initial-scale=1.0"> 
  <title>Custom</title> 
  <style> 
    .custom-menu { 
  display: none; 
  position: absolute; 
  background-color: #f9f9f9; 
  border: 1px solid #ccc; 
  padding: 8px; 
} 
</style> 
  </head> 
  <body> 
 
  <div class="custom-menu" id="custom-menu"> 
 
  </div> 
 
  <p id="ele">Select some text to see the custom menu.</p> 
  <span id="span1">span</span> 
  <div onclick="jplugin_utils('share')">Share</div> 
  </body> 
  <script> 
  function jplugin_utils(name) { 
    document.getElementById('span1').innerText = name; 
    console.log(name); 
    return name + ' result'; 
  } 
  </script> 
  </html>
import { webview } from '@kit.ArkWeb'; 
import { promptAction } from '@kit.ArkUI'; 
 
class TestObj { 
  constructor() { 
  } 
 
  test(testStr:string): string { 
    console.log('Web Component str' + testStr); 
    return testStr.length + ''; 
  } 
 
  share(result: string): void { 
    promptAction.showToast({ 
      message: 'hello: ' + result 
    }) 
  } 
} 
 
// 代理jplugin_utils,将结果传递至testObj处理 
const jsString = ` 
  function proxyJPlugin() { 
    window.jplugin_utils_prev = window.jplugin_utils; 
    window.jplugin_utils = function(name) { 
      var result = jplugin_utils_prev(name); 
      window.objName[name] (result); 
    } 
  } 
  proxyJPlugin(); 
` 
 
 
@Entry 
@Component 
struct Page240527171140077 { 
  @State message: string = 'Hello World'; 
 
  @State testObjtest: TestObj = new TestObj(); 
 
  controller: webview.WebviewController = new webview.WebviewController(); 
 
  aboutToAppear(): void { 
    webview.WebviewController.setWebDebuggingAccess(true); 
  } 
 
  build() { 
    Column() { 
      Web({ src: $rawfile('Page240527171140077.html'), controller: this.controller }) 
        .javaScriptAccess(true) 
        .domStorageAccess(true) 
        .zoomAccess(false) 
        .geolocationAccess(true) 
        .onControllerAttached(() => { 
          this.controller.registerJavaScriptProxy(this.testObjtest, "objName", ["test", "share"]); 
        }) 
        .onPageEnd(() => { 
          this.controller.runJavaScript(jsString) 
        }) 
    } 
    .height('100%') 
    .width('100%') 
  } 
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.
  • 77.
  • 78.
  • 79.
  • 80.
  • 81.
  • 82.
  • 83.
  • 84.
  • 85.
  • 86.
  • 87.
  • 88.
  • 89.
  • 90.
  • 91.
  • 92.
  • 93.
  • 94.
  • 95.
  • 96.
  • 97.
分享
微博
QQ
微信
回复
2024-11-06 17:44:33


相关问题
HarmonyOS H5调用原生扫码功能
955浏览 • 1回复 待解决
HarmonyOS h5前端侧调用应用侧方法
646浏览 • 2回复 待解决
HarmonyOS H5原生交互
952浏览 • 1回复 待解决
H5原生调JSbrigedemo示例
996浏览 • 1回复 待解决
HarmonyOS 原生与webview中H5消息通信
817浏览 • 1回复 待解决
HarmonyOS h5页面怎样判断当前环境?
889浏览 • 1回复 待解决
HarmonyOS h5原生交互、页面状态机
783浏览 • 1回复 待解决
HarmonyOS web原生H5如何交互?
1489浏览 • 1回复 待解决
如何桥接鸿蒙原生H5之间交互?
619浏览 • 2回复 已解决