gpt4 book ai didi

javascript - WKWebView是否可以关闭位置权限?

转载 作者:可可西里 更新时间:2023-11-01 01:57:05 25 4
gpt4 key购买 nike

我想知道是否可以阻止WKWebView 显示位置权限提示? (“website.com”想使用您的当前位置)我相信它正在显示,因为该网站包含谷歌地图。我对在其他 SO 问题中显示的位置预加载位置不感兴趣。我只是不想在 WKWebView 中使用位置。有没有办法阻止位置权限提示出现?我已尝试注入(inject)以下 javascript,但它不起作用。

        let contentController = WKUserContentController()

let scriptSource = "navigator.geolocation.getCurrentPosition = function(success, error, options) { // }; navigator.geolocation.watchPosition = function(success, error, options) { // }; navigator.geolocation.clearWatch = function(id) { // };"
let script = WKUserScript(source: scriptSource, injectionTime: .atDocumentEnd, forMainFrameOnly: true)
contentController.addUserScript(script)

let config = WKWebViewConfiguration()
config.userContentController = contentController

webView = WKWebView(frame: .zero, configuration: config)
self.view = webView

最佳答案

一些注意事项:

  1. .atDocumentEnd 意味着您的脚本将在页面加载后执行,这可能会导致 javascript 在您劫持位置功能之前要求运行位置;使用 .atDocumentStart
  2. 可能会得到更好的结果
  3. 脚本中有 // 序列,这可能会导致脚本的其余部分被解释为注释,从而导致注入(inject)不正确的 JS
  4. 函数的签名似乎与 HTML 5 Geolocation API 不匹配,尽管考虑到我们处于 JS 世界并且函数体是空的,但这可能没有什么不同。

尝试解决上述问题,这可能会导致您需要的行为。

关于javascript - WKWebView是否可以关闭位置权限?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51576836/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com