jsBridgePermission: JsBridgePermission = { javascriptProxyPermission: { urlPermissionList: [ // Object级权限,如果匹配,所有Method都授权 { scheme: 'resource', // 精确匹配,不能为空 host: 'rawfile', // 精确匹配,不能为空 port: '', // 精确匹配,为空不检查 path: '' // 前缀匹配,为空不检查 }, { scheme: 'file', // 精确匹配,不能为空 host: '', // 精确匹配,不能为空 port: '', // 精确匹配,为空不检查 path: '' // 前缀匹配,为空不检查 } ], methodList: [ { methodName: 'test', urlPermissionList: [ // Method级权限 { scheme: 'resource', // 精确匹配,不能为空 host: 'rawfile', // 精确匹配,不能为空 port: '', // 精确匹配,为空不检查 path: '' // 前缀匹配,为空不检查 } ] } ] } }
根据问题现象及报错信息,可知是JsBridge权限相关问题。发现代码中配置了JavaScriptProxy的permission参数,考虑JsBridge使用受限是由于该参数对JsBridge权限进行了管控,排查该参数配置:
要使用JavaScriptProxy的permission对JsBridge方法的调用进行权限管控,JavaScriptProxy的permission参数配置需要遵循以下原则:
以file协议加载Download目录下的html文件场景为例,要允许注入的JsBridge的方法A被调用,可参考如下修改建议:
jsBridgePermission: JsBridgePermission = { javascriptProxyPermission: { urlPermissionList: [ // Object级权限,如果匹配,所有Method都授权 { scheme: 'resource', // 精确匹配,不能为空 host: 'rawfile', // 精确匹配,不能为空 port: '', // 精确匹配,为空不检查 path: '' // 前缀匹配,为空不检查 }, { scheme: 'file', // 精确匹配,不能为空 host: 'docs', // 精确匹配,不能为空 port: '', // 精确匹配,为空不检查 path: '/storage/Users/currentUser/Download/' // file协议加载HTML时,所有JsBridge方法只允许Download目录下的HTML调用 }, ] } };
import { webview } from '@kit.ArkWeb'; import { BusinessError } from '@kit.BasicServicesKit'; import { fileIo, picker } from '@kit.CoreFileKit'; import { common } from '@kit.AbilityKit'; import { JSON } from '@kit.ArkTS'; class TestClass { constructor() { } test(param: string): string { return `${param}->Hello, I am ets!`; } } interface JsBridgePermission { javascriptProxyPermission: JavascriptProxyPermission; } interface JavascriptProxyPermission { urlPermissionList: UrlPermission[]; methodList?: MethodPermission[]; } interface MethodPermission { urlPermissionList: UrlPermission[]; methodName: string; } interface UrlPermission { scheme: string; host: string; port: string; path: string; } @Entry @Component struct Index { webviewController: webview.WebviewController = new webview.WebviewController(); // 声明需要注册的对象 testObj: TestClass = new TestClass(); jsBridgePermission: JsBridgePermission = { javascriptProxyPermission: { urlPermissionList: [ // Object级权限,如果匹配,所有Method都授权 { scheme: 'resource', // 精确匹配,不能为空 host: 'rawfile', // 精确匹配,不能为空 port: '', // 精确匹配,为空不检查 path: '' // 前缀匹配,为空不检查 }, { scheme: 'file', // 精确匹配,不能为空 host: 'docs', // 精确匹配,不能为空 port: '', // 精确匹配,为空不检查 path: '/storage/Users/currentUser/' // file协议加载HTML时,所有JsBridge方法只允许docs/storage/Users/currentUser/目录下的HTML调用 } ], methodList: [ { methodName: 'test', urlPermissionList: [ // Method级权限 { scheme: 'resource', // 精确匹配,不能为空 host: 'rawfile', // 精确匹配,不能为空 port: '', // 精确匹配,为空不检查 path: '' // 前缀匹配,为空不检查 }, { scheme: 'file', // 精确匹配,不能为空 host: 'docs', // 精确匹配,不能为空 port: '', // 精确匹配,为空不检查 path: '/storage/Users/currentUser/Download/' // file协议加载HTML时,test方法只允许Download目录下的HTML调用 } ] } ] } }; build() { Column({ space: 20 }) { Button('将index.html下载到Download目录') .type(ButtonType.ROUNDED_RECTANGLE) .onClick(async () => { try { let context = this.getUIContext().getHostContext() as common.UIAbilityContext; let documentSaveOptions = new picker.DocumentSaveOptions(); documentSaveOptions.newFileNames = ['index.html']; let documentPicker = new picker.DocumentViewPicker(context); documentPicker.save(documentSaveOptions).then((documentSelectResult: Array<string>) => { console.info('DocumentViewPicker.select successfully, documentSelectResult uri: ' + JSON.stringify(documentSelectResult)); let path = documentSelectResult[0]; let file = fileIo.openSync(path, fileIo.OpenMode.READ_WRITE); let data = context.resourceManager.getRawFileContentSync('index.html'); fileIo.writeSync(file.fd, data.buffer); fileIo.closeSync(file); console.info(`文件写入成功`); }).catch((err: BusinessError) => { console.error(`DocumentViewPicker.select failed with err, code is: ${err.code}, message is: ${err.message}`); }); } catch (err) { console.error(`Failed to getRdbStore. code: ${err.code}, message: ${err.message}`); } }); Button('选择Download下index.html') .onClick(() => { this.choseHtml(); }); Web({ src: '', controller: this.webviewController }) .javaScriptProxy({ object: this.testObj, name: 'testObjName', methodList: ['test'], controller: this.webviewController, asyncMethodList: [], permission: JSON.stringify(this.jsBridgePermission) }) .fileAccess(true) .javaScriptAccess(true) .geolocationAccess(false) .domStorageAccess(true); }; } choseHtml() { const documentSelectOptions = new picker.DocumentSelectOptions(); documentSelectOptions.maxSelectNumber = 1; documentSelectOptions.fileSuffixFilters = ['文档|.html']; let uris: Array<string> = []; let context = this.getUIContext().getHostContext() as common.UIAbilityContext; const documentViewPicker = new picker.DocumentViewPicker(context); documentViewPicker.select(documentSelectOptions).then((documentSelectResult: Array<string>) => { uris = documentSelectResult; console.info(uris[0]); // file://docs/storage/Users/currentUser/Download/index.html try { this.webviewController.loadUrl(uris[0]); } catch (error) { console.error(`ErrorCode: ${(error as BusinessError).code}, Message: ${(error as BusinessError).message}`); } }).catch((err: BusinessError) => { console.error(`Invoke documentViewPicker.select failed, code is ${err.code}, message is ${err.message}`); }); } }
完整示例参考如下:
ArkTS示例代码:
import { webview } from '@kit.ArkWeb'; import { BusinessError } from '@kit.BasicServicesKit'; import { fileIo, picker } from '@kit.CoreFileKit'; import { common } from '@kit.AbilityKit'; import { JSON } from '@kit.ArkTS'; class TestClass { constructor() { } test(param: string): string { return `${param}->Hello, I am ets!`; } } interface JsBridgePermission { javascriptProxyPermission: JavascriptProxyPermission; } interface JavascriptProxyPermission { urlPermissionList: UrlPermission[]; methodList?: MethodPermission[]; } interface MethodPermission { urlPermissionList: UrlPermission[]; methodName: string; } interface UrlPermission { scheme: string; host: string; port: string; path: string; } @Entry @Component struct Index { webviewController: webview.WebviewController = new webview.WebviewController(); // 声明需要注册的对象 testObj: TestClass = new TestClass(); jsBridgePermission: JsBridgePermission = { javascriptProxyPermission: { urlPermissionList: [ // Object级权限,如果匹配,所有Method都授权 { scheme: 'resource', // 精确匹配,不能为空 host: 'rawfile', // 精确匹配,不能为空 port: '', // 精确匹配,为空不检查 path: '' // 前缀匹配,为空不检查 }, { scheme: 'file', // 精确匹配,不能为空 host: 'docs', // 精确匹配,不能为空 port: '', // 精确匹配,为空不检查 path: '/storage/Users/currentUser/' // file协议加载HTML时,所有JsBridge方法只允许docs/storage/Users/currentUser/目录下的HTML调用 } ], methodList: [ { methodName: 'test', urlPermissionList: [ // Method级权限 { scheme: 'resource', // 精确匹配,不能为空 host: 'rawfile', // 精确匹配,不能为空 port: '', // 精确匹配,为空不检查 path: '' // 前缀匹配,为空不检查 }, { scheme: 'file', // 精确匹配,不能为空 host: 'docs', // 精确匹配,不能为空 port: '', // 精确匹配,为空不检查 path: '/storage/Users/currentUser/Download/' // file协议加载HTML时,test方法只允许Download目录下的HTML调用 } ] } ] } }; build() { Column({ space: 20 }) { Button('将index.html下载到Download目录') .type(ButtonType.ROUNDED_RECTANGLE) .onClick(async () => { try { let context = this.getUIContext().getHostContext() as common.UIAbilityContext; let documentSaveOptions = new picker.DocumentSaveOptions(); documentSaveOptions.newFileNames = ['index.html']; let documentPicker = new picker.DocumentViewPicker(context); documentPicker.save(documentSaveOptions).then((documentSelectResult: Array<string>) => { console.info('DocumentViewPicker.select successfully, documentSelectResult uri: ' + JSON.stringify(documentSelectResult)); let path = documentSelectResult[0]; let file = fileIo.openSync(path, fileIo.OpenMode.READ_WRITE); let data = context.resourceManager.getRawFileContentSync('index.html'); fileIo.writeSync(file.fd, data.buffer); fileIo.closeSync(file); console.info(`文件写入成功`); }).catch((err: BusinessError) => { console.error(`DocumentViewPicker.select failed with err, code is: ${err.code}, message is: ${err.message}`); }); } catch (err) { console.error(`Failed to getRdbStore. code: ${err.code}, message: ${err.message}`); } }); Button('选择Download下index.html') .onClick(() => { this.choseHtml(); }); Web({ src: '', controller: this.webviewController }) .javaScriptProxy({ object: this.testObj, name: 'testObjName', methodList: ['test'], controller: this.webviewController, asyncMethodList: [], permission: JSON.stringify(this.jsBridgePermission) }) .fileAccess(true) .javaScriptAccess(true) .geolocationAccess(false) .domStorageAccess(true); }; } choseHtml() { const documentSelectOptions = new picker.DocumentSelectOptions(); documentSelectOptions.maxSelectNumber = 1; documentSelectOptions.fileSuffixFilters = ['文档|.html']; let uris: Array<string> = []; let context = this.getUIContext().getHostContext() as common.UIAbilityContext; const documentViewPicker = new picker.DocumentViewPicker(context); documentViewPicker.select(documentSelectOptions).then((documentSelectResult: Array<string>) => { uris = documentSelectResult; console.info(uris[0]); // file://docs/storage/Users/currentUser/Download/index.html try { this.webviewController.loadUrl(uris[0]); } catch (error) { console.error(`ErrorCode: ${(error as BusinessError).code}, Message: ${(error as BusinessError).message}`); } }).catch((err: BusinessError) => { console.error(`Invoke documentViewPicker.select failed, code is ${err.code}, message is ${err.message}`); }); } }
html示例代码:
<!-- index.html -->
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no"/>
<style>
button {
width: 200px;
height: 60px;
font-size: 20px;
}
#demo {
font-size: 24px;
font-weight: 700;
}
</style>
</head>
<body>
<button class="inline-style-button" type="button" onclick="callArkTSMethod()">
CallArkTS Method
</button>
<p id="demo"></p>
<script>
function callArkTSMethod() {
let str = testObjName.test("Hi, I am H5.");
document.getElementById("demo").innerHTML = str;
}
</script>
</body>
</html>