2013-04-30 142 views
3

我试图在Chrome浏览器打包的应用程序中浏览webview中的地理位置,以便正确运行我的应用程序。我尝试了几种方法来获取manifest.json中的权限并注入脚本,但它不起作用,并且不显示任何错误消息。Chrome浏览器内的web浏览器中的GeoLocation打包应用程序

有人可以给我一个灯光或解决方案来获得许可,并显示我的地理位置?

回答

5

通常在普通网页中需要权限的一些功能也可以在web视图中使用。然而,包含webview的应用程序需要明确授权它,而不是正常的弹出窗口“网站xyz.com想知道您的物理位置 - 允许/拒绝”。这是它是如何工作的:

  • 无需更改web视图内的网页;

  • 在应用中,你听的<webview>元素permissionrequest事件:

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(); 
    } 
}); 

有一点要注意的是,请求并不需要马上处理。只要您在permissionrequest事件中调用preventDefault并保持事件对象不被垃圾收集,就可以在允许或拒绝之前做任何您需要做的事情。如果您需要执行任何异步操作(如转到存储以检查是否允许请求权限的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(); 
} 
  • 另外请注意,您的应用需要也要求相应的权限:
"permissions": ["geolocation"], 

webview sample具有其他权限的详细代码,例如pointerLock和媒体捕获。更

+0

UPDATE:添加@ user2296820关于延迟审批/拒绝许可请求的信息 – mangini 2013-04-30 21:52:00

+0

谢谢,问题已解决! =) – brunowindrop 2013-05-01 07:54:47

+0

早上好! Mangini我以为我已经解决了你的代码的问题,但在我发布应用程序来安装我的一些朋友没有工作。 我意识到只有在铬金丝雀工作...做了一些测试,以强制显示在正常位置可以得到,但它根本不显示。 你知道输入一个原因吗? 链接网上商店:https://chrome.google.com/webstore/detail/localizeime/ihappndkmffngabclkkpebgnbchlbhjg?utm_source=plus 那里,你可以找到github上的文件 – brunowindrop 2013-05-02 13:53:28

0

详细一点:

的响应并不需要,只要你preventDefault()以立即进行。默认操作是拒绝权限请求。

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(); 
} 
+0

谢谢,我已将此添加到我的回复中。 – mangini 2013-04-30 21:53:17

相关问题