1# Intelligent Tracking Prevention 2 3 4The **Web** component supports the intelligent tracking prevention feature. 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- Call the [enableIntelligentTrackingPrevention](../reference/apis-arkweb/arkts-apis-webview-WebviewController.md#enableintelligenttrackingprevention12) API to enable or disable the intelligent tracking prevention feature of the **Web** component. By default, this functionality 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- Call the [isIntelligentTrackingPreventionEnabled](../reference/apis-arkweb/arkts-apis-webview-WebviewController.md#isintelligenttrackingpreventionenabled12) API to check whether the intelligent tracking prevention feature 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- Call the [onIntelligentTrackingPreventionResult](../reference/apis-arkweb/arkts-basic-components-web-events.md#onintelligenttrackingpreventionresult12) API to return the blocked tracking domain name and the domain name of the triggered website to the application. 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 prevention feature 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 97The intelligent tracking prevention feature provides a group of APIs for setting the list of domain names that bypass this feature. The domain name list set by these APIs applies to the application rather than a specific **Web** component. 98 99- Call the [addIntelligentTrackingPreventionBypassingList](../reference/apis-arkweb/arkts-apis-webview-WebviewController.md#addintelligenttrackingpreventionbypassinglist12) API to set the bypass domain name list. 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- Call the [removeIntelligentTrackingPreventionBypassingList](../reference/apis-arkweb/arkts-apis-webview-WebviewController.md#removeintelligenttrackingpreventionbypassinglist12) API to delete some domain names in the bypass list. 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- Call the [clearIntelligentTrackingPreventionBypassingList](../reference/apis-arkweb/arkts-apis-webview-WebviewController.md#clearintelligenttrackingpreventionbypassinglist12) API to clear the bypass domain name list. 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