Fix permissions changes not being supported on some browsers (#1513)

This commit is contained in:
toasted-nutbread 2021-03-10 20:26:57 -05:00 committed by GitHub
parent b1f96a7ca7
commit 63de9273b3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 18 additions and 5 deletions

View File

@ -180,9 +180,11 @@ class Backend {
const onMessage = this._onMessageWrapper.bind(this); const onMessage = this._onMessageWrapper.bind(this);
chrome.runtime.onMessage.addListener(onMessage); chrome.runtime.onMessage.addListener(onMessage);
if (this._canObservePermissionsChanges()) {
const onPermissionsChanged = this._onWebExtensionEventWrapper(this._onPermissionsChanged.bind(this)); const onPermissionsChanged = this._onWebExtensionEventWrapper(this._onPermissionsChanged.bind(this));
chrome.permissions.onAdded.addListener(onPermissionsChanged); chrome.permissions.onAdded.addListener(onPermissionsChanged);
chrome.permissions.onRemoved.addListener(onPermissionsChanged); chrome.permissions.onRemoved.addListener(onPermissionsChanged);
}
chrome.runtime.onInstalled.addListener(this._onInstalled.bind(this)); chrome.runtime.onInstalled.addListener(this._onInstalled.bind(this));
} }
@ -2095,7 +2097,12 @@ class Backend {
this._updateBadge(); this._updateBadge();
} }
_canObservePermissionsChanges() {
return isObject(chrome.permissions) && isObject(chrome.permissions.onAdded) && isObject(chrome.permissions.onRemoved);
}
_hasRequiredPermissionsForSettings(options) { _hasRequiredPermissionsForSettings(options) {
if (!this._canObservePermissionsChanges()) { return true; }
return this._permissions === null || this._permissionsUtil.hasRequiredPermissionsForOptions(this._permissions, options); return this._permissions === null || this._permissionsUtil.hasRequiredPermissionsForOptions(this._permissions, options);
} }

View File

@ -51,9 +51,11 @@ class SettingsController extends EventDispatcher {
prepare() { prepare() {
yomichan.on('optionsUpdated', this._onOptionsUpdated.bind(this)); yomichan.on('optionsUpdated', this._onOptionsUpdated.bind(this));
if (this._canObservePermissionsChanges()) {
chrome.permissions.onAdded.addListener(this._onPermissionsChanged.bind(this)); chrome.permissions.onAdded.addListener(this._onPermissionsChanged.bind(this));
chrome.permissions.onRemoved.addListener(this._onPermissionsChanged.bind(this)); chrome.permissions.onRemoved.addListener(this._onPermissionsChanged.bind(this));
} }
}
async refresh() { async refresh() {
await this._onOptionsUpdatedInternal(); await this._onOptionsUpdatedInternal();
@ -206,4 +208,8 @@ class SettingsController extends EventDispatcher {
const permissions = await this._permissionsUtil.getAllPermissions(); const permissions = await this._permissionsUtil.getAllPermissions();
this.trigger(event, {permissions}); this.trigger(event, {permissions});
} }
_canObservePermissionsChanges() {
return isObject(chrome.permissions) && isObject(chrome.permissions.onAdded) && isObject(chrome.permissions.onRemoved);
}
} }