通过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%')
  }
}