我在清单中设置了地理位置权限,但每个页面仍然会要求共享位置 [英] I set geolocation permission in the manifest, but every page still asks to share location

查看:221
本文介绍了我在清单中设置了地理位置权限,但每个页面仍然会要求共享位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用我正在编写的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的。)



为了避免需要逐个域的权限,请调用geolocation API(这是一个HTML5 API,而不是chrome。* API )来自一个事件页面



这是一个完整的扩展程序,演示了这个过程:



manifest.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:请参阅SO Q 18307051.没有垃圾邮件警告的围巾位置,
version:1
}





main.js:


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



eventPage.js:

  chrome.runtime.onMessage.addListener(
函数(request,sender,sendResponse){

if(request.command ==gimmeGimme){

navigator.geolocation.getCurrentPosition(function(position){
sendResponse({
geoLocation:(
latitude =+ position.coords.latitude
+,longitude =+ position.coords.longitude

});
});
return true ; //需要,因为响应是异步的
}
}
);


I'm using a content-script in a chrome extension that I'm writing. I included geolocation in my list of permissions, but on every webpage I still get asked if I want to share my location.

I thought if I set geolocation in the list of permissions, I would avoid this? Here's the relevant code from my manifest.json:

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

And how I'm using it:

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

解决方案

Because you are calling the geolocation from a content script, the context of the target page is used and the request looks like it's coming from the target page. So each different domain must be authorized. (Content scripts are essentially injected javascript with enhanced privileges.)

To avoid the need for domain-by-domain permission, call the geolocation API (which is an HTML5 API, not a chrome.* API) from an Event Page.

Here's a complete extension that demonstrates the process:

manifest.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"
}


main.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
        }
    }
);

这篇关于我在清单中设置了地理位置权限,但每个页面仍然会要求共享位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆