@@ -81,14 +81,37 @@ export function bindToCheckbox(permissions, checkbox, options = {}) {
81
81
return ;
82
82
}
83
83
84
- const granted = await isGranted ( permissions ) ;
84
+ checkbox . checked = false ;
85
+ if ( configs . requestingPermissionsNatively ) {
86
+ return ;
87
+ }
88
+ let granted ;
89
+
90
+ // Following code will throw error on Firefox 60 and earlier (but not on Firefox ESR 60) due to https://bugzilla.mozilla.org/show_bug.cgi?id=1382953
91
+ // Also must not have used await before calling browser.permissions.request or it will throw an error.
92
+ try {
93
+ configs . requestingPermissionsNatively = permissions ;
94
+ granted = await browser . permissions . request ( permissions ) ;
95
+ } catch ( error ) {
96
+
97
+ } finally {
98
+ configs . requestingPermissionsNatively = null ;
99
+ }
100
+
101
+ if ( granted === undefined ) {
102
+ granted = await isGranted ( permissions ) ;
103
+ } else if ( ! granted ) {
104
+ return ;
105
+ }
106
+
85
107
if ( granted ) {
86
- options . onChanged ( true ) ;
108
+ checkbox . checked = true ;
109
+ if ( options . onChanged )
110
+ options . onChanged ( true ) ;
87
111
return ;
88
112
}
89
113
90
114
configs . requestingPermissions = permissions ;
91
- checkbox . checked = false ;
92
115
browser . browserAction . setBadgeText ( { text : '!' } ) ;
93
116
browser . browserAction . setPopup ( { popup : '' } ) ;
94
117
@@ -98,14 +121,6 @@ export function bindToCheckbox(permissions, checkbox, options = {}) {
98
121
icon : 'resources/24x24.svg'
99
122
} ) ;
100
123
return ;
101
-
102
- /*
103
- // following codes don't work as expected due to https://bugzilla.mozilla.org/show_bug.cgi?id=1382953
104
- if (!await browser.permissions.request(permissions)) {
105
- checkbox.checked = false;
106
- return;
107
- }
108
- */
109
124
}
110
125
catch ( error ) {
111
126
console . log ( error ) ;
@@ -120,6 +135,8 @@ export function requestPostProcess() {
120
135
121
136
const permissions = configs . requestingPermissions ;
122
137
configs . requestingPermissions = null ;
138
+ configs . requestingPermissionsNatively = permissions ;
139
+
123
140
browser . browserAction . setBadgeText ( { text : '' } ) ;
124
141
browser . permissions . request ( permissions ) . then ( granted => {
125
142
log ( 'permission requested: ' , permissions , granted ) ;
@@ -128,6 +145,8 @@ export function requestPostProcess() {
128
145
type : Constants . kCOMMAND_NOTIFY_PERMISSIONS_GRANTED ,
129
146
permissions : permissions
130
147
} ) ;
148
+ } ) . finally ( ( ) => {
149
+ configs . requestingPermissionsNatively = null ;
131
150
} ) ;
132
151
return true ;
133
152
}
0 commit comments