admin管理员组文章数量:1414908
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);
});
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);
});
Share
Improve this question
edited Aug 19, 2013 at 9:01
Brock Adams
93.7k23 gold badges241 silver badges305 bronze badges
asked Aug 19, 2013 at 5:44
Vlad the ImpalaVlad the Impala
15.9k17 gold badges84 silver badges128 bronze badges
1 Answer
Reset to default 7Because you are calling the geolocation from a content script, the context of the target page is used and the request looks like it's ing 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 plete 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 ( {mand: "gimmeGimme"}, function (response) {
console.log (response.geoLocation);
} );
eventPage.js:
chrome.runtime.onMessage.addListener (
function (request, sender, sendResponse) {
if (request.mand == "gimmeGimme") {
navigator.geolocation.getCurrentPosition (function (position) {
sendResponse ( {
geoLocation: (
"latitude=" + position.coords.latitude
+ ", longitude=" + position.coords.longitude
)
} );
} );
return true; // Needed because the response is asynchronous
}
}
);
本文标签:
版权声明:本文标题:javascript - I set geolocation permission in the manifest, but every page still asks to share location - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745165505a2645651.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论