Is it possible to overload geolocation functions using Chrome extension?
I want to overload navigator.geolocation.getCurrentPosition and navigator.geolocation.watchPosition to simulate geolocation during development. Is it possible to开发者_开发知识库 do it using Google Chrome extension?
You mean something like this?
navigator.geolocation.getCurrentPosition = function(successCallback, errorCallback) {
successCallback({coords:{lattitude:10, longitude:20}});
}
If it is just for your own needs then it would be much easier to just do it in your js code.
If you want to make extension out of it then:
- This code needs to be injected on every page of your app (via content script) and put inside dynamically created
<script>
tag, otherwise you will be inside content script sandbox and your overwritten function will not be visible to the parent page. - You would need to run this code before your app uses geolocation. There is no easy solution for this.
Now there's a chrome extension that does just that - the manual geolocation chrome extension. It tries its best to inject the script before any other one.
I found the extension what you want:)
https://chrome.google.com/webstore/detail/change-geolocation/njjpmclekpigefnogajiknnheheacoaj
精彩评论