Skip to content

Commit bb1af27

Browse files
authored
Merge pull request #2011 from Lej77/patch-1
Request optional permissions without toolbar button
2 parents 0aa5656 + 21da085 commit bb1af27

File tree

2 files changed

+31
-11
lines changed

2 files changed

+31
-11
lines changed

webextensions/common/common.js

+1
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,7 @@ export const configs = new Configs({
168168
simulateSVGContextFill: true,
169169

170170
requestingPermissions: null,
171+
requestingPermissionsNatively: null,
171172

172173
// https://dxr.mozilla.org/mozilla-central/rev/2535bad09d720e71a982f3f70dd6925f66ab8ec7/browser/base/content/browser.css#137
173174
newTabAnimationDuration: 100,

webextensions/common/permissions.js

+30-11
Original file line numberDiff line numberDiff line change
@@ -81,14 +81,37 @@ export function bindToCheckbox(permissions, checkbox, options = {}) {
8181
return;
8282
}
8383

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+
85107
if (granted) {
86-
options.onChanged(true);
108+
checkbox.checked = true;
109+
if (options.onChanged)
110+
options.onChanged(true);
87111
return;
88112
}
89113

90114
configs.requestingPermissions = permissions;
91-
checkbox.checked = false;
92115
browser.browserAction.setBadgeText({ text: '!' });
93116
browser.browserAction.setPopup({ popup: '' });
94117

@@ -98,14 +121,6 @@ export function bindToCheckbox(permissions, checkbox, options = {}) {
98121
icon: 'resources/24x24.svg'
99122
});
100123
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-
*/
109124
}
110125
catch(error) {
111126
console.log(error);
@@ -120,6 +135,8 @@ export function requestPostProcess() {
120135

121136
const permissions = configs.requestingPermissions;
122137
configs.requestingPermissions = null;
138+
configs.requestingPermissionsNatively = permissions;
139+
123140
browser.browserAction.setBadgeText({ text: '' });
124141
browser.permissions.request(permissions).then(granted => {
125142
log('permission requested: ', permissions, granted);
@@ -128,6 +145,8 @@ export function requestPostProcess() {
128145
type: Constants.kCOMMAND_NOTIFY_PERMISSIONS_GRANTED,
129146
permissions: permissions
130147
});
148+
}).finally(() => {
149+
configs.requestingPermissionsNatively = null;
131150
});
132151
return true;
133152
}

0 commit comments

Comments
 (0)