openharmony 鸿蒙 web-intelligent-tracking-prevention

2025-06-12 浏览 (1)

Intelligent Tracking Prevention

The Web component supports the intelligent tracking prevention. That is, when a tracking website is inserted into another web page as a third party, the network request sent by the website cannot carry cookies.

  • Invoke the enableIntelligentTrackingPrevention API to enable or disable the intelligent tracking prevention of a Web component. By default, this prevention is disabled.

    // xxx.ets
    import { webview } from '@kit.ArkWeb';
    import { BusinessError } from '@kit.BasicServicesKit';
    
    @Entry
    @Component
    struct WebComponent {
      controller: webview.WebviewController = new webview.WebviewController();
    
      build() {
        Column() {
          Button('enableIntelligentTrackingPrevention')
            .onClick(() => {
              try {
                this.controller.enableIntelligentTrackingPrevention(true);
                console.log("enableIntelligentTrackingPrevention: true");
              } catch (error) {
                console.error(`ErrorCode: ${(error as BusinessError).code},  Message: ${(error as BusinessError).message}`);
              }
            })
          Web({ src: 'www.example.com', controller: this.controller })
        }
      }
    }
    
  • Invoke the isIntelligentTrackingPreventionEnabled API to check whether the intelligent tracking prevention is enabled for the **Web **component.

    // xxx.ets
    import { webview } from '@kit.ArkWeb';
    import { BusinessError } from '@kit.BasicServicesKit';
    
    @Entry
    @Component
    struct WebComponent {
      controller: webview.WebviewController = new webview.WebviewController();
    
      build() {
        Column() {
          Button('isIntelligentTrackingPreventionEnabled')
            .onClick(() => {
              try {
                let result = this.controller.isIntelligentTrackingPreventionEnabled();
                console.log("result: " + result);
              } catch (error) {
                console.error(`ErrorCode: ${(error as BusinessError).code},  Message: ${(error as BusinessError).message}`);
              }
            })
          Web({ src: 'www.example.com', controller: this.controller })
        }
      }
    }
    
  • You can call the onIntelligentTrackingPreventionResult API to asynchronously obtain the domain names of intercepted tracking websites and accessed websites.

    // xxx.ets
    import { webview } from '@kit.ArkWeb';
    import { BusinessError } from '@kit.BasicServicesKit';
    
    @Entry
    @Component
    struct WebComponent {
      controller: webview.WebviewController = new webview.WebviewController();
    
      build() {
        Column() {
          // The onIntelligentTrackingPreventionResult callback is triggered only when the intelligent tracking prenvention is enabled.
          Button('enableIntelligentTrackingPrevention')
            .onClick(() => {
              try {
                this.controller.enableIntelligentTrackingPrevention(true);
              } catch (error) {
                console.error(`ErrorCode: ${(error as BusinessError).code}, Message: ${(error as BusinessError).message}`);
              }
            })
          Web({ src: 'www.example.com', controller: this.controller })
            .onIntelligentTrackingPreventionResult((details) => {
              console.log("onIntelligentTrackingPreventionResult: [websiteHost]= " + details.host +
                ", [trackerHost]=" + details.trackerHost);
            })
        }
      }
    }
    

In addition, the intelligent tracking prevention functionality provides APIs for setting the list of domain names that need to bypass the intelligent tracking prevention. The domain name list set by these APIs applies to the entire application instead of a single Web component.

  • Invoke the addIntelligentTrackingPreventionBypassingList API to set the list of domain names that need to bypass the intelligent tracking prevention.

    // xxx.ets
    import { webview } from '@kit.ArkWeb';
    import { BusinessError } from '@kit.BasicServicesKit';
    
    @Entry
    @Component
    struct WebComponent {
      controller: webview.WebviewController = new webview.WebviewController();
    
      build() {
        Column() {
          Button('addIntelligentTrackingPreventionBypassingList')
            .onClick(() => {
              try {
                let hostList = ["www.test1.com", "www.test2.com", "www.test3.com"];
                webview.WebviewController.addIntelligentTrackingPreventionBypassingList(hostList);
              } catch (error) {
                console.error(`ErrorCode: ${(error as BusinessError).code},  Message: ${(error as BusinessError).message}`);
              }
            })
          Web({ src: 'www.example.com', controller: this.controller })
        }
      }
    }
    
  • Invoke the removeIntelligentTrackingPreventionBypassingList API to remove the partial domain name list set using the addIntelligentTrackingPreventionBypassingList API.

    // xxx.ets
    import { webview } from '@kit.ArkWeb';
    import { BusinessError } from '@kit.BasicServicesKit';
    
    @Entry
    @Component
    struct WebComponent {
      controller: webview.WebviewController = new webview.WebviewController();
    
      build() {
        Column() {
          Button('removeIntelligentTrackingPreventionBypassingList')
            .onClick(() => {
              try {
                let hostList = [ "www.test1.com", "www.test2.com" ];
                webview.WebviewController.removeIntelligentTrackingPreventionBypassingList(hostList);
              } catch (error) {
                console.error(`ErrorCode: ${(error as BusinessError).code},  Message: ${(error as BusinessError).message}`);
              }
            })
          Web({ src: 'www.example.com', controller: this.controller })
        }
      }
    }
    
  • Invoke the clearIntelligentTrackingPreventionBypassingList API to clear all domain names set using the addIntelligentTrackingPreventionBypassingList API.

    // xxx.ets
    import { webview } from '@kit.ArkWeb';
    
    @Entry
    @Component
    struct WebComponent {
      controller: webview.WebviewController = new webview.WebviewController();
    
      build() {
        Column() {
          Button('clearIntelligentTrackingPreventionBypassingList')
            .onClick(() => {
              webview.WebviewController.clearIntelligentTrackingPreventionBypassingList();
            })
          Web({ src: 'www.example.com', controller: this.controller })
        }
      }
    }
    

你可能感兴趣的鸿蒙文章

harmony 鸿蒙ArkWeb

harmony 鸿蒙Taking Over the Media Playback on Web Pages

harmony 鸿蒙Mutual Invoking Between the Application and the Frontend Page (C/C++)

harmony 鸿蒙Establishing a Data Channel Between the Application and the Frontend Page (C/C++)

harmony 鸿蒙Enabling Ads Blocking

harmony 鸿蒙Establishing a Data Channel Between the Application and the Frontend Page

harmony 鸿蒙Migrating Web Components Between Different Windows

harmony 鸿蒙Introduction to ArkWeb

harmony 鸿蒙Implementing Content Scrolling

harmony 鸿蒙Managing Cookies and Data Storage

  • 所属分类: 后端技术
  • 本文标签: 软件 鸿蒙
  • 版权声明: 本文链接 https://seaxiang.com/blog/bpUob3