【问题标题】:I set geolocation permission in the manifest, but every page still asks to share location我在清单中设置了地理位置权限,但每个页面仍然要求共享位置
【发布时间】:2013-08-19 17:01:46
【问题描述】:

我正在编写的 chrome 扩展中使用内容脚本。我将geolocation 包含在我的权限列表中,但在每个网页上,我仍然会被问到是否要分享我的位置。

我想如果我在权限列表中设置geolocation,我会避免这种情况吗?这是我manifest.json的相关代码:

"permissions": ["geolocation"],
"content_scripts": [
 {
   "matches": ["<all_urls>"],
   "js": ["main.js"]
 }
]

以及我是如何使用它的:

navigator.geolocation.getCurrentPosition(function(position) {
    console.log("latitude=" + position.coords.latitude +
    ", longitude=" + position.coords.longitude);
});

【问题讨论】:

    标签: javascript google-chrome-extension geolocation content-script


    【解决方案1】:

    因为您是从内容脚本调用地理位置,所以使用了目标页面的上下文,并且请求看起来像是来自目标页面。所以每个不同的域都必须被授权。 (内容脚本本质上是注入了增强权限的 javascript。)

    为避免需要逐个域的权限,请从an Event Page 调用地理定位 API (which is an HTML5 API, not a chrome.* API)。

    这里有一个完整的扩展来演示这个过程:

    ma​​nifest.json:

    {
        "manifest_version": 2,
        "permissions":      ["geolocation"],
        "content_scripts":  [ {
            "js":               [   "main.js" ],
            "matches":          [   "<all_urls>" ]
        } ],
        "background": {
            "scripts":      ["eventPage.js"],
            "persistent":   false
        },
        "name":             "_Read browser location",
        "description":      "See SO Q 18307051. Scarf location without spamming warnings",
        "version":          "1"
    }
    


    ma​​in.js:

    chrome.runtime.sendMessage ( {command: "gimmeGimme"}, function (response) {
        console.log (response.geoLocation);
    } );
    


    eventPage.js:

    chrome.runtime.onMessage.addListener (
        function (request, sender, sendResponse) {
    
            if (request.command == "gimmeGimme") {
    
                navigator.geolocation.getCurrentPosition (function (position) {
                    sendResponse ( {
                        geoLocation: (
                              "latitude="    + position.coords.latitude
                            + ", longitude=" + position.coords.longitude
                        )
                    } );
                } );
                return true; // Needed because the response is asynchronous
            }
        }
    );
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-10-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多