1# Intelligent Tracking Prevention 2 3 4The **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. 5 6- Invoke the [enableIntelligentTrackingPrevention](../reference/apis-arkweb/js-apis-webview.md#enableintelligenttrackingprevention12) API to enable or disable the intelligent tracking prevention of a **Web** component. By default, this prevention is disabled. 7 8 ```ts 9 // xxx.ets 10 import { webview } from '@kit.ArkWeb'; 11 import { BusinessError } from '@kit.BasicServicesKit'; 12 13 @Entry 14 @Component 15 struct WebComponent { 16 controller: webview.WebviewController = new webview.WebviewController(); 17 18 build() { 19 Column() { 20 Button('enableIntelligentTrackingPrevention') 21 .onClick(() => { 22 try { 23 this.controller.enableIntelligentTrackingPrevention(true); 24 console.log("enableIntelligentTrackingPrevention: true"); 25 } catch (error) { 26 console.error(`ErrorCode: ${(error as BusinessError).code}, Message: ${(error as BusinessError).message}`); 27 } 28 }) 29 Web({ src: 'www.example.com', controller: this.controller }) 30 } 31 } 32 } 33 ``` 34 35- Invoke the [isIntelligentTrackingPreventionEnabled](../reference/apis-arkweb/js-apis-webview.md#isintelligenttrackingpreventionenabled12) API to check whether the intelligent tracking prevention is enabled for the **Web **component. 36 37 ```ts 38 // xxx.ets 39 import { webview } from '@kit.ArkWeb'; 40 import { BusinessError } from '@kit.BasicServicesKit'; 41 42 @Entry 43 @Component 44 struct WebComponent { 45 controller: webview.WebviewController = new webview.WebviewController(); 46 47 build() { 48 Column() { 49 Button('isIntelligentTrackingPreventionEnabled') 50 .onClick(() => { 51 try { 52 let result = this.controller.isIntelligentTrackingPreventionEnabled(); 53 console.log("result: " + result); 54 } catch (error) { 55 console.error(`ErrorCode: ${(error as BusinessError).code}, Message: ${(error as BusinessError).message}`); 56 } 57 }) 58 Web({ src: 'www.example.com', controller: this.controller }) 59 } 60 } 61 } 62 ``` 63 64- You can call the [onIntelligentTrackingPreventionResult](../reference/apis-arkweb/ts-basic-components-web.md#onintelligenttrackingpreventionresult12) API to asynchronously obtain the domain names of intercepted tracking websites and accessed websites. 65 66 ```ts 67 // xxx.ets 68 import { webview } from '@kit.ArkWeb'; 69 import { BusinessError } from '@kit.BasicServicesKit'; 70 71 @Entry 72 @Component 73 struct WebComponent { 74 controller: webview.WebviewController = new webview.WebviewController(); 75 76 build() { 77 Column() { 78 // The onIntelligentTrackingPreventionResult callback is triggered only when the intelligent tracking prenvention is enabled. 79 Button('enableIntelligentTrackingPrevention') 80 .onClick(() => { 81 try { 82 this.controller.enableIntelligentTrackingPrevention(true); 83 } catch (error) { 84 console.error(`ErrorCode: ${(error as BusinessError).code}, Message: ${(error as BusinessError).message}`); 85 } 86 }) 87 Web({ src: 'www.example.com', controller: this.controller }) 88 .onIntelligentTrackingPreventionResult((details) => { 89 console.log("onIntelligentTrackingPreventionResult: [websiteHost]= " + details.host + 90 ", [trackerHost]=" + details.trackerHost); 91 }) 92 } 93 } 94 } 95 ``` 96 97In 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. 98 99- Invoke the [addIntelligentTrackingPreventionBypassingList](../reference/apis-arkweb/js-apis-webview.md#addintelligenttrackingpreventionbypassinglist12) API to set the list of domain names that need to bypass the intelligent tracking prevention. 100 101 ```ts 102 // xxx.ets 103 import { webview } from '@kit.ArkWeb'; 104 import { BusinessError } from '@kit.BasicServicesKit'; 105 106 @Entry 107 @Component 108 struct WebComponent { 109 controller: webview.WebviewController = new webview.WebviewController(); 110 111 build() { 112 Column() { 113 Button('addIntelligentTrackingPreventionBypassingList') 114 .onClick(() => { 115 try { 116 let hostList = ["www.test1.com", "www.test2.com", "www.test3.com"]; 117 webview.WebviewController.addIntelligentTrackingPreventionBypassingList(hostList); 118 } catch (error) { 119 console.error(`ErrorCode: ${(error as BusinessError).code}, Message: ${(error as BusinessError).message}`); 120 } 121 }) 122 Web({ src: 'www.example.com', controller: this.controller }) 123 } 124 } 125 } 126 ``` 127 128- Invoke the [removeIntelligentTrackingPreventionBypassingList](../reference/apis-arkweb/js-apis-webview.md#removeintelligenttrackingpreventionbypassinglist12) API to remove the partial domain name list set using the [addIntelligentTrackingPreventionBypassingList](../reference/apis-arkweb/js-apis-webview.md#addintelligenttrackingpreventionbypassinglist12) API. 129 130 ```ts 131 // xxx.ets 132 import { webview } from '@kit.ArkWeb'; 133 import { BusinessError } from '@kit.BasicServicesKit'; 134 135 @Entry 136 @Component 137 struct WebComponent { 138 controller: webview.WebviewController = new webview.WebviewController(); 139 140 build() { 141 Column() { 142 Button('removeIntelligentTrackingPreventionBypassingList') 143 .onClick(() => { 144 try { 145 let hostList = [ "www.test1.com", "www.test2.com" ]; 146 webview.WebviewController.removeIntelligentTrackingPreventionBypassingList(hostList); 147 } catch (error) { 148 console.error(`ErrorCode: ${(error as BusinessError).code}, Message: ${(error as BusinessError).message}`); 149 } 150 }) 151 Web({ src: 'www.example.com', controller: this.controller }) 152 } 153 } 154 } 155 ``` 156 157- Invoke the [clearIntelligentTrackingPreventionBypassingList](../reference/apis-arkweb/js-apis-webview.md#clearintelligenttrackingpreventionbypassinglist12) API to clear all domain names set using the [addIntelligentTrackingPreventionBypassingList](../reference/apis-arkweb/js-apis-webview.md#addintelligenttrackingpreventionbypassinglist12) API. 158 159 ```ts 160 // xxx.ets 161 import { webview } from '@kit.ArkWeb'; 162 163 @Entry 164 @Component 165 struct WebComponent { 166 controller: webview.WebviewController = new webview.WebviewController(); 167 168 build() { 169 Column() { 170 Button('clearIntelligentTrackingPreventionBypassingList') 171 .onClick(() => { 172 webview.WebviewController.clearIntelligentTrackingPreventionBypassingList(); 173 }) 174 Web({ src: 'www.example.com', controller: this.controller }) 175 } 176 } 177 } 178 ``` 179