diff options
author | toasted-nutbread <toasted-nutbread@users.noreply.github.com> | 2024-01-31 08:38:30 -0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-01-31 13:38:30 +0000 |
commit | 87ed7c8affd3ade9d3cd2d9ed1a61dd5f224e473 (patch) | |
tree | be727294e31ef21e8a3f634734610e69e4a155ac /ext/js/data/permissions-util.js | |
parent | 3e419aa562aab03ca20421aaf7e4d1a39194a5b4 (diff) |
Module refactoring (#588)
* Convert PronunciationGenerator into static functions
* Convert DictionaryDataUtil into static functions
* Convert AnkiNoteDataCreator into static functions
* Convert MediaUtil into static functions
* Convert RegexUtil into static functions
* Convert StringUtil into static functions
* Convert ArrayBufferUtil into static functions
* Convert AnkiUtil into static functions
* Convert PermissionsUtil into static functions
* Convert ProfileConditionsUtil into static functions
Diffstat (limited to 'ext/js/data/permissions-util.js')
-rw-r--r-- | ext/js/data/permissions-util.js | 191 |
1 files changed, 98 insertions, 93 deletions
diff --git a/ext/js/data/permissions-util.js b/ext/js/data/permissions-util.js index 43aaa87c..837b6d5f 100644 --- a/ext/js/data/permissions-util.js +++ b/ext/js/data/permissions-util.js @@ -16,123 +16,128 @@ * along with this program. If not, see <https://www.gnu.org/licenses/>. */ -import {AnkiUtil} from './anki-util.js'; +import {getFieldMarkers} from './anki-util.js'; -export class PermissionsUtil { - constructor() { - /** @type {Set<string>} */ - this._ankiFieldMarkersRequiringClipboardPermission = new Set([ - 'clipboard-image', - 'clipboard-text' - ]); +/** + * This function returns whether an Anki field marker might require clipboard permissions. + * This is speculative and may not guarantee that the field marker actually does require the permission, + * as the custom handlebars template is not deeply inspected. + * @param {string} marker + * @returns {boolean} + */ +function ankiFieldMarkerMayUseClipboard(marker) { + switch (marker) { + case 'clipboard-image': + case 'clipboard-text': + return true; + default: + return false; } +} + +/** + * @param {chrome.permissions.Permissions} permissions + * @returns {Promise<boolean>} + */ +export function hasPermissions(permissions) { + return new Promise((resolve, reject) => chrome.permissions.contains(permissions, (result) => { + const e = chrome.runtime.lastError; + if (e) { + reject(new Error(e.message)); + } else { + resolve(result); + } + })); +} - /** - * @param {chrome.permissions.Permissions} permissions - * @returns {Promise<boolean>} - */ - hasPermissions(permissions) { - return new Promise((resolve, reject) => chrome.permissions.contains(permissions, (result) => { +/** + * @param {chrome.permissions.Permissions} permissions + * @param {boolean} shouldHave + * @returns {Promise<boolean>} + */ +export function setPermissionsGranted(permissions, shouldHave) { + return ( + shouldHave ? + new Promise((resolve, reject) => chrome.permissions.request(permissions, (result) => { const e = chrome.runtime.lastError; if (e) { reject(new Error(e.message)); } else { resolve(result); } - })); - } - - /** - * @param {chrome.permissions.Permissions} permissions - * @param {boolean} shouldHave - * @returns {Promise<boolean>} - */ - setPermissionsGranted(permissions, shouldHave) { - return ( - shouldHave ? - new Promise((resolve, reject) => chrome.permissions.request(permissions, (result) => { - const e = chrome.runtime.lastError; - if (e) { - reject(new Error(e.message)); - } else { - resolve(result); - } - })) : - new Promise((resolve, reject) => chrome.permissions.remove(permissions, (result) => { - const e = chrome.runtime.lastError; - if (e) { - reject(new Error(e.message)); - } else { - resolve(!result); - } - })) - ); - } - - /** - * @returns {Promise<chrome.permissions.Permissions>} - */ - getAllPermissions() { - return new Promise((resolve, reject) => chrome.permissions.getAll((result) => { + })) : + new Promise((resolve, reject) => chrome.permissions.remove(permissions, (result) => { const e = chrome.runtime.lastError; if (e) { reject(new Error(e.message)); } else { - resolve(result); + resolve(!result); } - })); - } + })) + ); +} - /** - * @param {string} fieldValue - * @returns {string[]} - */ - getRequiredPermissionsForAnkiFieldValue(fieldValue) { - const markers = AnkiUtil.getFieldMarkers(fieldValue); - const markerPermissions = this._ankiFieldMarkersRequiringClipboardPermission; - for (const marker of markers) { - if (markerPermissions.has(marker)) { - return ['clipboardRead']; - } +/** + * @returns {Promise<chrome.permissions.Permissions>} + */ +export function getAllPermissions() { + return new Promise((resolve, reject) => chrome.permissions.getAll((result) => { + const e = chrome.runtime.lastError; + if (e) { + reject(new Error(e.message)); + } else { + resolve(result); + } + })); +} + +/** + * @param {string} fieldValue + * @returns {string[]} + */ +export function getRequiredPermissionsForAnkiFieldValue(fieldValue) { + const markers = getFieldMarkers(fieldValue); + for (const marker of markers) { + if (ankiFieldMarkerMayUseClipboard(marker)) { + return ['clipboardRead']; } - return []; } + return []; +} - /** - * @param {chrome.permissions.Permissions} permissions - * @param {import('settings').ProfileOptions} options - * @returns {boolean} - */ - hasRequiredPermissionsForOptions(permissions, options) { - const permissionsSet = new Set(permissions.permissions); +/** + * @param {chrome.permissions.Permissions} permissions + * @param {import('settings').ProfileOptions} options + * @returns {boolean} + */ +export function hasRequiredPermissionsForOptions(permissions, options) { + const permissionsSet = new Set(permissions.permissions); - if (!permissionsSet.has('nativeMessaging')) { - if (options.parsing.enableMecabParser) { - return false; - } + if (!permissionsSet.has('nativeMessaging')) { + if (options.parsing.enableMecabParser) { + return false; } + } - if (!permissionsSet.has('clipboardRead')) { - if (options.clipboard.enableBackgroundMonitor || options.clipboard.enableSearchPageMonitor) { - return false; - } - const fieldMarkersRequiringClipboardPermission = this._ankiFieldMarkersRequiringClipboardPermission; - const fieldsList = [ - options.anki.terms.fields, - options.anki.kanji.fields - ]; - for (const fields of fieldsList) { - for (const fieldValue of Object.values(fields)) { - const markers = AnkiUtil.getFieldMarkers(fieldValue); - for (const marker of markers) { - if (fieldMarkersRequiringClipboardPermission.has(marker)) { - return false; - } + if (!permissionsSet.has('clipboardRead')) { + if (options.clipboard.enableBackgroundMonitor || options.clipboard.enableSearchPageMonitor) { + return false; + } + const fieldsList = [ + options.anki.terms.fields, + options.anki.kanji.fields + ]; + for (const fields of fieldsList) { + for (const fieldValue of Object.values(fields)) { + const markers = getFieldMarkers(fieldValue); + for (const marker of markers) { + if (ankiFieldMarkerMayUseClipboard(marker)) { + return false; } } } } - - return true; } + + return true; } |