web拦截并替换图片,有谁知道怎么处理?

web拦截并替换图片

HarmonyOS
2024-05-20 20:08:09
821浏览
已于2024-5-21 15:40:49修改
收藏 0
回答 1
回答 1
按赞同
/
按时间
QW_MA

核心代码解释

主要为web组件的页面刷新,页面跳转,加载url,加载字符串等等。

import { webview } from '@kit.ArkWeb'; 
import { common } from '@kit.AbilityKit'; 
import fs from '@ohos.file.fs'; 
import axios from '@ohos/axios'; 
 
@Entry 
@Component 
struct GetFile { 
  @State message: string = 'Hello World'; 
  // 获取应用文件路径 
  context = getContext(this) as common.UIAbilityContext; 
  webviewController: webview.WebviewController = new webview.WebviewController(); 
  responseweb: WebResourceResponse = new WebResourceResponse() 
 
  aboutToAppear() { 
    // 配置Web开启调试模式 
    webview.WebviewController.setWebDebuggingAccess(true); 
  } 
 
  hasFile(filePath: string) { 
    let b = fs.accessSync(filePath) 
    return b 
  } 
 
  async getFile(url: string) { 
    // const regex = new RegExp('/[^/]+/g', 'gim'); 
    // console.log(url+" 123456") 
    // const fileName = url.match(regex); 
    const fileName = url.split("/").pop(); 
    console.log(fileName + " 123456") 
    const filePath = this.context.cacheDir + '/' + fileName; 
    console.log(filePath + ' 123456') 
    const flag = this.hasFile(filePath); 
    console.log(flag + "123456") 
    if (!flag) { 
      await this.downloadImage(url, filePath); 
    } 
    // const fd : number = await fileio.open(filePath); 
    let file = fs.openSync(filePath, fs.OpenMode.READ_ONLY); 
    console.log(file + "1234567") 
    let arrayBuffer = new ArrayBuffer(4096); 
    console.log(file.fd + " 123456") 
    return file.fd; 
  } 
 
  async downloadImage(url: string, filePath: string) { 
    try { 
      await axios({ 
        method: "get", 
        url, 
        context: this.context, 
        filePath: filePath 
      }); 
    } catch (error) { 
      console.error('[Demo] axios error'); 
    } 
  } 
 
  build() { 
    Column() { 
      Web({ src: $rawfile('catch.html'), controller: this.webviewController }) 
        .onInterceptRequest((event) => { 
          if (event) { 
            console.log('url123456:' + event.request.getRequestUrl()) 
          } 
          const url = event!.request.getRequestUrl(); 
          // const regExp = new RegExp("^(http|https)[w/-]+(.jpg)$"); 
          console.log(url.endsWith(".jpg") + '123456') 
          if (!url.endsWith(".jpg")) return null; 
          try { 
            // const responseweb = new WebResourceResponse(); 
            let url2 = 'https://img.tukuppt.com/photo-big/17/12/16/57/94/171216579483.jpg'; 
            this.getFile(url2).then(fd => { 
              this.responseweb.setResponseData(fd); 
              this.responseweb.setResponseCode(200); 
              this.responseweb.setReasonMessage('OK'); 
              this.responseweb.setResponseIsReady(true); 
              console.log(fd + " 1234567") 
            }); 
            this.responseweb.setResponseMimeType('image/*'); 
            this.responseweb.setResponseIsReady(false); 
            console.log(this.responseweb.getResponseData().toString + "123456") 
            return this.responseweb; 
          } catch (error) { 
            console.error(`[Demo]Code: ${error.code},Message: ${error.message} `); 
            return null 
          } 
        }) 
    } 
  } 
}
  • 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.

上诉是web拦截并替换图片,对于web拦截并替换资源,可以参考一下

实现效果

web拦截并替换图片,对于web拦截并替换资源,对违规图片或不想要的图片进行替换。

分享
微博
QQ
微信
回复
2024-05-21 15:32:27
相关问题
有谁知道web拦截如何处理文件
1595浏览 • 1回复 待解决
设备唯一ID,有谁知道怎么处理
2925浏览 • 1回复 待解决
有谁知道如何拦截住页面返回
1146浏览 • 3回复 待解决
有谁知道Image图片取反色
3086浏览 • 1回复 待解决
有谁知道web组件如何下拉刷新
2452浏览 • 1回复 待解决
有谁知道沙箱目录怎么获取
3532浏览 • 1回复 待解决
有谁知道如何生成UUID
2509浏览 • 1回复 待解决
有谁知道flexBasis使用问题
1725浏览 • 1回复 待解决
有谁知道如何创建 JSONObject
1132浏览 • 1回复 待解决
滑动选择组件,有谁知道怎么解决吗?
1576浏览 • 1回复 待解决
有谁知道如何将图片保存到相册
2470浏览 • 1回复 待解决
有谁知道如何直接调起图片
2731浏览 • 1回复 待解决
有谁知道如何主动关闭应用
2862浏览 • 1回复 待解决
有谁知道应用升级的方式
2561浏览 • 1回复 待解决
有谁知道如何理解栅格布局
1341浏览 • 1回复 待解决
有谁知道常用AppFreeze使用指导
2175浏览 • 1回复 待解决
有谁知道napi多函数导出写法
2298浏览 • 1回复 待解决
有谁知道如何计算文本的宽度
2963浏览 • 1回复 待解决
有谁知道如何获取应用包信息
2618浏览 • 1回复 待解决
vp、fp、px的区别,有谁知道
3657浏览 • 1回复 待解决