我正在尝试在Chrome打包应用程序的网页视图中获取地理位置,以便正确运行我的应用程序.我已经尝试了几种方法来获取manifest.json和注入脚本的权限,但它不起作用,并且不显示任何错误消息.
有人可以给我一个亮点或解决方案来获得许可并显示我的地理位置吗?
通常在普通网页中需要权限的某些功能也可以在webview中使用.但是,不是正常的弹出窗口"网站xyz.com想知道你的物理位置 - 允许/拒绝",包含webview的应用程序需要明确授权它.下面是它的工作原理:
无需更改webview中的网页;
在应用程序中,您可以监听元素permissionrequest上的事件<webview>:
webview.addEventListener('permissionrequest', function(e) {
if ( e.permission === 'geolocation' ) {
e.request.allow();
} else {
console.log('Denied permission '+e.permission+' requested by webview');
e.request.deny();
}
});
Run Code Online (Sandbox Code Playgroud)
需要注意的一点是,请求不需要立即处理.只要您preventDefault在permissionrequest事件中调用并保持事件对象不被垃圾回收,您就可以在允许或拒绝之前执行任何操作.如果您需要执行任何异步操作,这非常有用,例如转到存储以检查是否应允许请求权限的URL.
例如:
webview.addEventListener('permissionrequest', function(e) {
if ( e.permission === 'geolocation' ) {
// Calling e.preventDefault() is necessary to delay the response.
// If the default is not prevented then the default action is to
// deny the permission request.
e.preventDefault();
setTimeout(function() { decidePermission(e); }, 0);
}
});
var decidePermission = function(e) {
if (e.url == 'http://www.google.com') {
e.request.allow();
}
// Calling e.request.deny() explicitly is not absolutely necessary because
// the request object is managed by the Javascript garbage collector.
// Once collected, the request will automatically be denied.
// If you wish to deny immediately call e.request.deny();
}
Run Code Online (Sandbox Code Playgroud)
"permissions": ["geolocation"],
Run Code Online (Sandbox Code Playgroud)
该网页流量样品有其他的权限,如pointerLock和媒体捕获更多的代码.
| 归档时间: |
|
| 查看次数: |
1055 次 |
| 最近记录: |