Skip to content
This repository has been archived by the owner on Feb 25, 2023. It is now read-only.

Fix permissions changes not being supported on some browsers #1513

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions ext/js/background/backend.js
Original file line number Diff line number Diff line change
Expand Up @@ -180,9 +180,11 @@ class Backend {
const onMessage = this._onMessageWrapper.bind(this);
chrome.runtime.onMessage.addListener(onMessage);

const onPermissionsChanged = this._onWebExtensionEventWrapper(this._onPermissionsChanged.bind(this));
chrome.permissions.onAdded.addListener(onPermissionsChanged);
chrome.permissions.onRemoved.addListener(onPermissionsChanged);
if (this._canObservePermissionsChanges()) {
const onPermissionsChanged = this._onWebExtensionEventWrapper(this._onPermissionsChanged.bind(this));
chrome.permissions.onAdded.addListener(onPermissionsChanged);
chrome.permissions.onRemoved.addListener(onPermissionsChanged);
}

chrome.runtime.onInstalled.addListener(this._onInstalled.bind(this));
}
Expand Down Expand Up @@ -2095,7 +2097,12 @@ class Backend {
this._updateBadge();
}

_canObservePermissionsChanges() {
return isObject(chrome.permissions) && isObject(chrome.permissions.onAdded) && isObject(chrome.permissions.onRemoved);
}

_hasRequiredPermissionsForSettings(options) {
if (!this._canObservePermissionsChanges()) { return true; }
return this._permissions === null || this._permissionsUtil.hasRequiredPermissionsForOptions(this._permissions, options);
}

Expand Down
10 changes: 8 additions & 2 deletions ext/js/pages/settings/settings-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,10 @@ class SettingsController extends EventDispatcher {

prepare() {
yomichan.on('optionsUpdated', this._onOptionsUpdated.bind(this));
chrome.permissions.onAdded.addListener(this._onPermissionsChanged.bind(this));
chrome.permissions.onRemoved.addListener(this._onPermissionsChanged.bind(this));
if (this._canObservePermissionsChanges()) {
chrome.permissions.onAdded.addListener(this._onPermissionsChanged.bind(this));
chrome.permissions.onRemoved.addListener(this._onPermissionsChanged.bind(this));
}
}

async refresh() {
Expand Down Expand Up @@ -206,4 +208,8 @@ class SettingsController extends EventDispatcher {
const permissions = await this._permissionsUtil.getAllPermissions();
this.trigger(event, {permissions});
}

_canObservePermissionsChanges() {
return isObject(chrome.permissions) && isObject(chrome.permissions.onAdded) && isObject(chrome.permissions.onRemoved);
}
}