如何在Electron中启用位置权限?

问题描述 投票:0回答:1

我正在尝试在电子中获取位置权限,并且我尝试在会话中使用(“setPermisionRequestHandler”),但它似乎不起作用,我做错了什么?就是这样:

session
    .fromPartition("some-partition")
    .setPermissionRequestHandler((webContents, permission, callback) => {
      const url = webContents.getURL();

      if (permission === "geolocation") {
        // Approves the permissions request
        callback(true);
      }
      // Verify URL
      if (!url.startsWith("https://puram.com")) {
        // Denies the permissions request
        return callback(false);
      }
    });
javascript electron
1个回答
0
投票

您可以使用 navigator.permissions.query

检查地理位置权限
navigator.permissions.query({ name: 'geolocation' }).then(function(permissionStatus) {
        if (permissionStatus.state === 'granted') {
          // Geolocation permission is already granted
          // You can use navigator.geolocation.getCurrentPosition() here
          console.log('Geolocation permission granted');              
        } else if (permissionStatus.state === 'prompt') {
          // Geolocation permission is not granted, but user hasn't made a decision yet
          // You can prompt the user for permission
          //promptGeolocationPermission();
          console.log('Geolocation permission prompt');
        } else if (permissionStatus.state === 'denied') {
          // Geolocation permission is denied
          console.log('Geolocation permission denied');
        }
    });
© www.soinside.com 2019 - 2024. All rights reserved.