文档开发者中心
我的
|

ArkWeb

HarmonyOS 5

ArkWeb组件加载网页,无法回调JS函数

已解决
使用ArkWeb加载人机验证网页,第一次进入可以触发回调,后续重新进入,无法触发
点赞
收藏
回复
1
分享
举报
浏览150 发布于2025-01-24 06:59广东
全部评论
最多点赞
最新发布
最早发布
|
采纳答复

我这边试了一下发现是正常能触发的,你这边再确认一下

收起
自动换行
深色代码主题
复制
//index.html 文件
<!-- index.html -->
<!DOCTYPE html>
<html>
<head>
    <title>Hello World</title>
    <style>
        button {
            width:300px;
            height:100px;
            background-color: #008CBA;
            padding: 15px 30px;
            text-align: center;
            text-decoration: none;
             font-size: 20px;
             color: white;
        }
        div {
            padding: 15px 30px;
            text-align: center;
            width:100%;
        }
    </style>
</head>
<body>
<div>
    <button  type="button" onclick="start()">测试</button>
</div>

<p id="demo"></p>
<script>
    function start() {
        HarmonyOS_native.captchaResult('');
        console.info('start ok:');
    }
</script>
</body>
</html>
------------------------------------------------------
//webPage.ets 文件
import { router } from '@kit.ArkUI'
import webview from '@ohos.web.webview';
import { WebDataClass, WebJSBridgeClass, webJSBridgeMethods } from './WebJSBridgeManager';

@Entry
@Component
struct WebPage {
  controller: webview.WebviewController = new webview.WebviewController();
  title: string = (router.getParams() as Record<string, string>)['title']
  url: string = (router.getParams() as Record<string, string>)['url']
  @State webDataObject: WebDataClass = new WebDataClass('', false)
  @State webJSBridgeClass: WebJSBridgeClass = new WebJSBridgeClass(this.controller, this.webDataObject);
  @State mode: MixedMode = MixedMode.All;




  build() {
    Column() {
      Text(this.title)
        .width('100%')
        .height(50)
        .textAlign(TextAlign.Center)

      Web({ src: $rawfile("index.html"), controller: this.controller })
     // Web({ src: this.url, controller: this.controller })
        .domStorageAccess(true)
        .mixedMode(this.mode)
        .cacheMode(CacheMode.Online)
        .onProgressChange((event) => {
          if (event) {
          }
        })
        .onTitleReceive((event) => {
          if (event) {
            this.webDataObject.title = event.title
          }
        })
        .onRenderExited((event) => {
          if (event) {
            console.error('reason:' + event.renderExitReason)
          }
        })
        .javaScriptProxy({
          object: this.webJSBridgeClass,
          name: "HarmonyOS_native",
          methodList: webJSBridgeMethods,
          controller: this.controller
        })
        .layoutWeight(1)
        .width('100%')
    }
  }

  aboutToAppear(): void {
    console.error('url = '+ this.url)
    webview.once('webInited', () => {
      let ua = this.controller.getUserAgent() + 'mxsa_mxbc';
      this.controller.setCustomUserAgent(ua);
    })
  }
}

------------------------------------------------------
//WebJSBridgeManager.ets 文件

import web_webview from '@ohos.web.webview';
import pasteboard from '@ohos.pasteboard';
import { router } from '@kit.ArkUI';
import { emitter } from '@kit.BasicServicesKit';
import { HashMap } from '@kit.ArkTS';

interface JsResponse {
  code: number;
  msg: String;
  data: object | undefined
}

export class captchaVerifyParam {
  code: string = ''
}

export class BaseEventData implements emitter.EventData {
  data: captchaVerifyParam = new captchaVerifyParam()
}


/*
由于struct和class不同,不建议把this作为参数传递到struct外部使用,避免引起实例引用无法释放的情况,导致内存泄露。
建议将状态变量对象传递到struct外面使用,通过修改对象的属性,来触发UI刷新。
 * */
class WebDataClass {
  title: string = ''
  isHideTitleBar: boolean = false

  constructor(title: string, isHideTitleBar: boolean) {
    this.title = title
    this.isHideTitleBar = isHideTitleBar
  }
}

interface GeneratedObjectLiteralInterface_1 {
  code: number;
  msg: string;
}

class WebJSBridgeClass {
  private webviewController: web_webview.WebviewController;
  private webDataObject?: WebDataClass;

  constructor(webviewController: web_webview.WebviewController,
    webDataObject: WebDataClass) {
    this.webviewController = webviewController;
    this.webDataObject = webDataObject;
  }




  captchaResult(data: string) {
    // console.error('receive js bridge captchaResult = '+data)
    // let dataObj: Record<string, Object> = JSON.parse(data);
    // if (dataObj) {
    //   let params = dataObj.data as Record<string, Object>
    //   if (params && params.captchaVerifyParam) {
    //
    //     // emitter.emit('captchaVerifyParam', eventData)
    //     router.back()
    //   }
    // } else {
    //   let res: JsResponse = {
    //     code: -1,
    //     msg: '参数异常',
    //     data: undefined
    //   }
    //   this.evaluateJavaScript(JSON.stringify(res));
    // }
    router.back()
  }

  //执行JS函数
  private evaluateJavaScript(params: string) {
    this.webviewController.runJavaScript(`HarmonyOSCallBack(${params})`);
  }
}

let webJSBridgeMethods: Array<string> = [
  'captchaResult'
]

export {
  WebJSBridgeClass, webJSBridgeMethods, WebDataClass
}
展开全部
1楼回复于2025-01-24 08:56 来自广东
写回答
新增插入模板功能
一键使用模板,快速填写内容,轻松发帖~
知道了
  • 为了保障您的信息安全,请勿上传您的敏感个人信息(如您的密码等信息)和您的敏感资产信息(如关键源代码、签名私钥、调试安装包、业务日志等信息),且您需自行承担由此产生的信息泄露等安全风险。
  • 如您发布的内容为转载内容,请注明内容来源。

我要提问题

了解社区公约,与您携手共创和谐专业的开发者社区。