注入JavaScript对象在window对象中调用,获取返回值
通过Web组件的javaScriptProxy属性注入JavaScript对象到window对象中,并在window对象中调用该对象的方法。可以通过静态变量的方式
import { webview } from '@kit.ArkWeb'export class Toolkits { static richText: string = '' onRichChange(dataSource: string) { Toolkits.richText = dataSource } }@Componentexport struct CustomH5 { @Link controller: webview.WebviewController @Prop toolkits: Toolkits | null = null @Prop extraJavaScript: string = '' @Prop url: string = '' @State scripts: Array<ScriptItem> = [ { script: this.getJavaScript(), scriptRules: ['*'] } ] getJavaScript() { const access_token = '*** access_token ***' return `window.localStorage.setItem('access_token','${access_token}')` } build() { Column() { Text(`RichText:${Toolkits.richText}`) Web({ src: $rawfile('test.html'), controller: this.controller, }) .domStorageAccess(true) .fileAccess(true) .javaScriptOnDocumentStart(this.scripts) .javaScriptProxy({ object: this.toolkits ? this.toolkits : new Toolkits(), name: 'Toolkits', methodList: ['onRichChange'], controller: this.controller, }) } .width('100%') } }
免责声明:本站所有信息均来源于互联网搜集,并不代表本站观点,本站不对其真实合法性负责。如有信息侵犯了您的权益,请告知,本站将立刻删除。