我想开发一个与 Manifest 版本 3 兼容的 Chrome 扩展程序,但我在获取当前用户的地理位置坐标时遇到了问题,该坐标适用于 Manifest 版本 2,但对于 Manifest 版本 3,它会抛出navigator.geolocation
未定义的问题。
这是我的 manifest.json 和 background.js 文件:
清单.json:
"name": "Chrome Extension MV3",
"description": "Learning all about Chrome Extensions Manifest Version 3!",
"version": "0.1.0",
"manifest_version": 2,
"background": {
"service_worker": "background.js"
},
"permissions": ["geolocation"]
}
背景.js:
navigator.geolocation.getCurrentPosition((position) => {
const { latitude, longitude } = position.coords;
console.log(latitude, longitude);
});