DEV Community

SameX
SameX

Posted on

ArkWeb Intelligent Anti-tracking and Ad Filtering - Protecting User Privacy

This article aims to deeply explore the technical details of the Huawei HarmonyOS Next system (up to API 12 as of now), and is summarized based on actual development practices.
It mainly serves as a vehicle for technical sharing and communication. Mistakes and omissions are inevitable. Colleagues are welcome to put forward valuable opinions and questions so that we can make progress together.
This article is original content, and any form of reprint must indicate the source and the original author.

Introduction

The ArkWeb (Ark Web) application framework of the Huawei HarmonyOS Next system provides developers with powerful capabilities for Web application development. However, Web applications may collect users' personal information and browsing behaviors, thus causing privacy issues. The ArkWeb framework offers intelligent anti-tracking and ad filtering functions, which can help developers protect users' privacy.

Intelligent Anti-tracking Function

The intelligent anti-tracking function of the ArkWeb framework can prevent tracking websites from carrying cookies to track users, thus protecting users' privacy. Tracking websites usually use third-party libraries or scripts to track users' browsing behaviors and send them to servers for analysis. The intelligent anti-tracking function can block these requests, thereby preventing users' information from being leaked.

Working Principle

The intelligent anti-tracking function is achieved by intercepting requests from tracking websites. When a Web component attempts to load resources from a tracking website, the ArkWeb kernel will intercept the request and decide whether to allow it according to the configuration. If it is configured to prohibit requests from tracking websites, the request will be intercepted and the tracking with cookies will be prevented.

How to Enable and Configure

You can enable and configure the intelligent anti-tracking function by following these steps:

  1. Call the following API in the application's entry file (such as EntryAbility.ets) to enable the intelligent anti-tracking function:
import { webview } from '@ohos.web.webview';
//...
// Enable the intelligent anti-tracking function
webview.WebviewController.enableIntelligentTrackingPrevention({
    enable: true // Enable the intelligent anti-tracking function
});
Enter fullscreen mode Exit fullscreen mode
  1. You can set a list of domain names to bypass the intelligent anti-tracking function, allowing requests from these domain names to carry cookies. For example:
import { webview } from '@ohos.web.webview';
//...
// Set the list of domain names to bypass the intelligent anti-tracking function
webview.WebviewController.addIntelligentTrackingPreventionBypassingList(["trusteddomain.com", "anothertrusteddomain.com"]);
Enter fullscreen mode Exit fullscreen mode

Ad Filtering Function

The ArkWeb framework provides an ad filtering function, which can help developers intercept ads in web pages, thereby improving the user experience and protecting users' privacy. You can use a custom Easylist rule file for ad blocking, or you can use the built-in rule file.

Using a Custom Easylist Rule File

You can use a custom Easylist rule file to intercept ads in web pages. The Easylist rule file is a simple text file that contains URL patterns or CSS selectors to be blocked. For example:

https://example.com/ad/*
.advert*
Enter fullscreen mode Exit fullscreen mode

These rules will block all URLs starting with "ad" under the example.com domain and all elements with the class name "advert".
You can save the custom Easylist rule file to the application's resource directory and use the following API to set it in the Web component:

import { webview } from '@ohos.web.webview';
import { picker } from '@ohos.core.file';
//...
// Select the custom Easylist rule file
const documentSelectOptions = new picker.DocumentSelectOptions();
const documentPicker = new picker.DocumentViewPicker();
documentPicker.select(documentSelectOptions).then((documentSelectResult) => {
    if (documentSelectResult && documentSelectResult.length > 0) {
        const fileRealPath = new fileUri.FileUri(documentSelectResult[0]);
        webview.AdsBlockManager.setAdsBlockRules(fileRealPath.path, true);
    }
});
Enter fullscreen mode Exit fullscreen mode

Using the Built-in Rule File

The ArkWeb framework has some built-in ad filtering rule files. You can use the following API to set them in the Web component:

import { webview } from '@ohos.web.webview';
//...
// Use the built-in rule file to enable the ad filtering function
webview.AdsBlockManager.setAdsBlockRules(null, false);
Enter fullscreen mode Exit fullscreen mode

How to Enable and Configure

You can enable and configure the ad filtering function by following these steps:

  1. Call the following API in the application's entry file (such as EntryAbility.ets) to enable the ad filtering function:
import { webview } from '@ohos.web.webview';
//...
// Enable the ad filtering function
webview.WebviewController.enableAdsBlock({
    enable: true // Enable the ad filtering function
});
Enter fullscreen mode Exit fullscreen mode
  1. You can set a list of domain names to allow or prohibit ad filtering. For example:
import { webview } from '@ohos.web.webview';
//...
// Set the list of domain names to allow ad filtering
webview.AdsBlockManager.addAdsBlockAllowedList(["example.com", "anotherexample.com"]);
Enter fullscreen mode Exit fullscreen mode
import { webview } from '@ohos.web.webview';
//...
// Set the list of domain names to prohibit ad filtering
webview.AdsBlockManager.addAdsBlockDisallowedList(["adexample.com", "anotheradexample.com"]);
Enter fullscreen mode Exit fullscreen mode

Example Code

The following example code shows how to use the ArkWeb API to enable the intelligent anti-tracking function and the ad filtering function and set the relevant configurations:

import { webview } from '@ohos.web.webview';
import { picker } from '@ohos.core.file';
//...
// Enable the intelligent anti-tracking function
webview.WebviewController.enableIntelligentTrackingPrevention({
    enable: true // Enable the intelligent anti-tracking function
});
// Enable the ad filtering function
webview.WebviewController.enableAdsBlock({
    enable: true // Enable the ad filtering function
});
// Select the custom Easylist rule file
const documentSelectOptions = new picker.DocumentSelectOptions();
const documentPicker = new picker.DocumentViewPicker();
documentPicker.select(documentSelectOptions).then((documentSelectResult) => {
    if (documentSelectResult && documentSelectResult.length > 0) {
        const fileRealPath = new fileUri.FileUri(documentSelectResult[0]);
        webview.AdsBlockManager.setAdsBlockRules(fileRealPath.path, true);
    }
});
// Set the list of domain names to bypass the intelligent anti-tracking function
webview.WebviewController.addIntelligentTrackingPreventionBypassingList(["trusteddomain.com", "anothertrusteddomain.com"]);
// Set the list of domain names to allow ad filtering
webview.AdsBlockManager.addAdsBlockAllowedList(["example.com", "anotherexample.com"]);
// Set the list of domain names to prohibit ad filtering
webview.AdsBlockManager.addAdsBlockDisallowedList(["adexample.com", "anotheradexample.com"]);
Enter fullscreen mode Exit fullscreen mode

Summary

The intelligent anti-tracking function and the ad filtering function of the ArkWeb framework can help developers protect users' privacy. By understanding the working principles of these functions and taking appropriate configurations, you can develop more secure and reliable Web applications and protect users' privacy and data security.

Top comments (0)