未验证 提交 9f9a8adc 编写于 作者: O openharmony_ci 提交者: Gitee

!15981 Add API onLoadIntercept and Get Sandbox Path File

Merge pull request !15981 from 李想/master
......@@ -3626,7 +3626,7 @@ static getCookie(url: string): string
| 参数名 | 类型 | 必填 | 说明 |
| ------ | ------ | ---- | :------------------------ |
| url | string | 是 | 要获取的cookie所属的url。 |
| url | string | 是 | 要获取的cookie所属的url,建议使用完整的url。 |
**返回值:**
......@@ -3682,7 +3682,7 @@ static setCookie(url: string, value: string): void
| 参数名 | 类型 | 必填 | 说明 |
| ------ | ------ | ---- | :------------------------ |
| url | string | 是 | 要设置的cookie所属的url。 |
| url | string | 是 | 要设置的cookie所属的url,建议使用完整的url。 |
| value | string | 是 | 要设置的cookie的值。 |
**错误码:**
......
......@@ -27,7 +27,7 @@ Web(options: { src: ResourceStr, controller: WebviewController | WebController})
| 参数名 | 参数类型 | 必填 | 参数描述 |
| ---------- | ---------------------------------------- | ---- | ------- |
| src | [ResourceStr](ts-types.md) | 是 | 网页资源地址。如果访问本地资源文件,请使用$rawfile或者resource协议。 |
| src | [ResourceStr](ts-types.md) | 是 | 网页资源地址。如果访问本地资源文件,请使用$rawfile或者resource协议。如果加载应用包外沙箱路径的本地资源文件,请使用file://沙箱文件路径。 |
| controller | [WebviewController<sup>9+</sup>](../apis/js-apis-webview.md#webviewcontroller) \| [WebController](#webcontroller) | 是 | 控制器。从API Version 9开始,WebController不在维护,建议使用WebviewController替代。 |
**示例:**
......@@ -84,6 +84,43 @@ Web(options: { src: ResourceStr, controller: WebviewController | WebController})
}
```
加载沙箱路径下的本地资源文件
1.通过[globalthis](../../application-models/uiability-data-sync-with-ui.md#uiability和page之间使用globalthis)获取沙箱路径。
```ts
// xxx.ets
import web_webview from '@ohos.web.webview'
let url = 'file://' + globalThis.filesDir + '/xxx.html'
@Entry
@Component
struct WebComponent {
controller: web_webview.WebviewController = new web_webview.WebviewController()
build() {
Column() {
// 加载沙箱路径文件。
Web({ src: url, controller: this.controller })
}
}
}
```
2.修改MainAbility.ts。
以filesDir为例,获取沙箱路径。若想获取其他路径,请参考[应用开发路径](../../application-models/application-context-stage.md#获取应用开发路径)
```ts
// xxx.ts
import UIAbility from '@ohos.app.ability.UIAbility';
import web_webview from '@ohos.web.webview';
export default class EntryAbility extends UIAbility {
onCreate(want, launchParam) {
// 通过在globalThis对象上绑定filesDir,可以实现UIAbility组件与UI之间的数据同步。
globalThis.filesDir = this.context.filesDir
console.log("Sandbox path is " + globalThis.filesDir)
}
}
```
```html
<!-- index.html -->
<!DOCTYPE html>
......@@ -2995,6 +3032,50 @@ onAudioStateChanged(callback: (event: { playing: boolean }) => void)
}
```
### onLoadIntercept<sup>10+</sup>
onLoadIntercept(callback: (event?: { request: WebResourceRequest }) => boolean)
当Web组件加载url之前触发该回调,用于判断是否阻止此次访问。默认允许加载。
**参数:**
| 参数名 | 参数类型 | 参数描述 |
| ------- | ---------------------------------------- | --------- |
| request | [Webresourcerequest](#webresourcerequest) | url请求的相关信息。 |
**返回值:**
| 类型 | 说明 |
| ------- | ------------------------ |
| boolean | 返回true表示阻止此次加载,否则允许此次加载。 |
**示例:**
```ts
// xxx.ets
import web_webview from '@ohos.web.webview'
@Entry
@Component
struct WebComponent {
controller: web_webview.WebviewController = new web_webview.WebviewController()
build() {
Column() {
Web({ src: 'www.example.com', controller: this.controller })
.onUrlLoadIntercept((event) => {
console.log('url:' + event.request.getRequestUrl())
console.log('isMainFrame:' + event.request.isMainFrame())
console.log('isRedirect:' + event.request.isRedirect())
console.log('isRequestGesture:' + event.request.isRequestGesture())
return true
})
}
}
}
```
## ConsoleMessage
Web组件获取控制台信息对象。示例代码参考[onConsole事件](#onconsole)
......@@ -4686,7 +4767,7 @@ setCookie(url: string, value: string): boolean
| 参数名 | 参数类型 | 必填 | 默认值 | 参数描述 |
| ----- | ------ | ---- | ---- | ----------------- |
| url | string | 是 | - | 要设置的cookie所属的url。 |
| url | string | 是 | - | 要设置的cookie所属的url,建议使用完整的url。 |
| value | string | 是 | - | cookie的值。 |
**返回值:**
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册