HarmonyOS onInterceptRequest的返回值WebResourceResponse应该怎么构造,做到只拦截部分请求,其余的请求不拦截?

如题:HarmonyOS onInterceptRequest的返回值WebResourceResponse应该怎么构造,做到只拦截部分请求,其余的请求不拦截?

阅读 439
1 个回答

参考代码:

Web({ src: $rawfile('route.html'), controller: this.webviewController })
  .onLoadIntercept((event) => {
    if (event) {
      let context: common.UIAbilityContext = getContext(this) as common.UIAbilityContext;
      let url: string = event.data.getRequestUrl();
      if (url.startsWith("xxx") || url.startsWith("xxxx")) {
        let link = 'xxx'
        let openLinkOptions: OpenLinkOptions = {
          appLinkingOnly: false
        }
        try {
          // 实际使用时用真实url,此处link仅做拉起xxx展示
          context.openLink(link, openLinkOptions, (err, data) => {
            // AbilityResult callback回调,仅在被拉起ability死亡时触发
            console.log('open link success. Callback result:' + JSON.stringify(data));
          }).then(() => {
            console.log('open link success.');
          }).catch((err: BusinessError) => {
            console.log(`open link failed. Code is ${err.code}, message is ${err.message}`);
          })
        } catch (paramError) {
          console.log(`Failed to start link. Code is ${paramError.code}, message is ${paramError.message}`);
        }
      }
    }
    return false;
  })