• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Customizing Page Request Responses
2
3
4The **Web** component supports customization of the response to intercepted page requests. You can call [onInterceptRequest()](../reference/apis-arkweb/ts-basic-components-web.md#oninterceptrequest9) to customize web page responses, file resource responses, and more.
5
6
7When a resource loading request is initiated on a web page, the application layer will receive the request. The application layer then constructs a local resource response and sends it to the web kernel. On receiving the response, the web kernel parses the response and loads page resources accordingly.
8
9
10In the following example, the **Web** component intercepts the web page request **https://www.example.com/test.html** and constructs a custom response in the application code.
11
12
13- Code of the **index.html** page:
14
15  ```html
16  <!DOCTYPE html>
17  <html>
18  <head>
19      <meta charset="utf-8">
20  </head>
21  <body>
22  <!-- Page resource request ->
23  <a href="https://www.example.com/test.html">intercept test!</a>
24  </body>
25  </html>
26  ```
27
28- Application code:
29
30  ```ts
31  // xxx.ets
32  import web_webview from '@ohos.web.webview';
33
34  @Entry
35  @Component
36  struct WebComponent {
37    controller: web_webview.WebviewController = new web_webview.WebviewController()
38    responseResource: WebResourceResponse = new WebResourceResponse()
39    // Customize a response.
40    @State webData: string = '<!DOCTYPE html>\n' +
41    '<html>\n'+
42    '<head>\n'+
43    '<title>intercept test</title>\n'+
44    '</head>\n'+
45    '<body>\n'+
46    '<h1>intercept ok</h1>\n'+
47    '</body>\n'+
48    '</html>'
49    build() {
50      Column() {
51        Web({ src: $rawfile('index.html'), controller: this.controller })
52          .onInterceptRequest((event) => {
53            if (event) {
54              console.info('url:' + event.request.getRequestUrl());
55              // Intercept the web page request.
56              if (event.request.getRequestUrl() !== 'https://www.example.com/test.html') {
57                return null;
58              }
59            }
60            // Construct a custom response.
61            this.responseResource.setResponseData(this.webData);
62            this.responseResource.setResponseEncoding('utf-8');
63            this.responseResource.setResponseMimeType('text/html');
64            this.responseResource.setResponseCode(200);
65            this.responseResource.setReasonMessage('OK');
66            return this.responseResource;
67          })
68      }
69    }
70  }
71  ```
72
73Create a **CodeCache** object for a JavaScript resource in a custom request response: If the resource of a custom request response is a JavaScript script, you can add the **ResponseDataID** field to the response header. After obtaining this field, the **Web** kernel generates a **CodeCache** object, which accelerates JavaScript execution. Any changes to the **ResponseDataID** field must be notified to the **Web** component. If the **ResponseDataID** field is not added, no **CodeCache** object is created by default.
74
75In the following example, the **Web** component intercepts the web page request **https://www.example.com/test.js**; a custom response is constructed in the application code, with the **ResponseDataID** field added to the response header.
76
77- Code of the **index.html** page:
78
79  ```html
80  <!DOCTYPE html>
81  <html>
82  <head>
83      <meta charset="utf-8">
84  </head>
85  <body>
86
87  <div id="div-1">this is a test div</div>
88  <div id="div-2">this is a test div</div>
89  <div id="div-3">this is a test div</div>
90  <div id="div-4">this is a test div</div>
91  <div id="div-5">this is a test div</div>
92  <div id="div-6">this is a test div</div>
93  <div id="div-7">this is a test div</div>
94  <div id="div-8">this is a test div</div>
95  <div id="div-9">this is a test div</div>
96  <div id="div-10">this is a test div</div>
97  <div id="div-11">this is a test div</div>
98
99  <script src="https://www.example.com/test.js"></script>
100  </body>
101  </html>
102  ```
103
104- Application code:
105
106  ```ts
107  // xxx.ets
108  import web_webview from '@ohos.web.webview';
109
110  @Entry
111  @Component
112  struct WebComponent {
113    controller: web_webview.WebviewController = new web_webview.WebviewController()
114    responseResource: WebResourceResponse = new WebResourceResponse()
115    // Customize response data. (The CodeCache object is created only when the response data length is greater than or equal to 1024 bytes.)
116    @State jsData: string = 'let text_msg = "the modified content:version 0000000000001";\n' +
117      'let element1 = window.document.getElementById("div-1");\n' +
118      'let element2 = window.document.getElementById("div-2");\n' +
119      'let element3 = window.document.getElementById("div-3");\n' +
120      'let element4 = window.document.getElementById("div-4");\n' +
121      'let element5 = window.document.getElementById("div-5");\n' +
122      'let element6 = window.document.getElementById("div-6");\n' +
123      'let element7 = window.document.getElementById("div-7");\n' +
124      'let element8 = window.document.getElementById("div-8");\n' +
125      'let element9 = window.document.getElementById("div-9");\n' +
126      'let element10 = window.document.getElementById("div-10");\n' +
127      'let element11 = window.document.getElementById("div-11");\n' +
128      'element1.innerHTML = text_msg;\n' +
129      'element2.innerHTML = text_msg;\n' +
130      'element3.innerHTML = text_msg;\n' +
131      'element4.innerHTML = text_msg;\n' +
132      'element5.innerHTML = text_msg;\n' +
133      'element6.innerHTML = text_msg;\n' +
134      'element7.innerHTML = text_msg;\n' +
135      'element8.innerHTML = text_msg;\n' +
136      'element9.innerHTML = text_msg;\n' +
137      'element10.innerHTML = text_msg;\n' +
138      'element11.innerHTML = text_msg;\n'
139
140    build() {
141      Column() {
142        Web({ src: $rawfile('index.html'), controller: this.controller })
143          .onInterceptRequest((event) => {
144            // Intercept the web page request.
145            if (event?.request.getRequestUrl() == 'https://www.example.com/test.js') {
146              // Construct a custom response.
147              this.responseResource.setResponseHeader([
148                {
149                  // The value is a string of a maximum of 13 digits. It is a JavaScript identifier and must be updated to maintain consistency with JavaScript.
150                  headerKey: "ResponseDataID",
151                  headerValue: "0000000000001"
152                }]);
153              this.responseResource.setResponseData(this.jsData);
154              this.responseResource.setResponseEncoding('utf-8');
155              this.responseResource.setResponseMimeType('application/javascript');
156              this.responseResource.setResponseCode(200);
157              this.responseResource.setReasonMessage('OK');
158              return this.responseResource;
159            }
160            return null;
161          })
162      }
163    }
164  }
165  ```
166