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/background/backend.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/background/backend.js')
-rw-r--r-- | ext/js/background/backend.js | 38 |
1 files changed, 17 insertions, 21 deletions
diff --git a/ext/js/background/backend.js b/ext/js/background/backend.js index 0773dc4b..b95626f5 100644 --- a/ext/js/background/backend.js +++ b/ext/js/background/backend.js @@ -26,19 +26,19 @@ import {ExtensionError} from '../core/extension-error.js'; import {readResponseJson} from '../core/json.js'; import {log} from '../core/logger.js'; import {clone, deferPromise, isObject, promiseTimeout} from '../core/utilities.js'; -import {AnkiUtil} from '../data/anki-util.js'; +import {isNoteDataValid} from '../data/anki-util.js'; import {OptionsUtil} from '../data/options-util.js'; -import {PermissionsUtil} from '../data/permissions-util.js'; -import {ArrayBufferUtil} from '../data/sandbox/array-buffer-util.js'; +import {getAllPermissions, hasPermissions, hasRequiredPermissionsForOptions} from '../data/permissions-util.js'; +import {arrayBufferToBase64} from '../data/sandbox/array-buffer-util.js'; import {DictionaryDatabase} from '../dictionary/dictionary-database.js'; import {Environment} from '../extension/environment.js'; import {ObjectPropertyAccessor} from '../general/object-property-accessor.js'; import {distributeFuriganaInflected, isCodePointJapanese, isStringPartiallyJapanese, convertKatakanaToHiragana as jpConvertKatakanaToHiragana} from '../language/japanese.js'; import {Translator} from '../language/translator.js'; import {AudioDownloader} from '../media/audio-downloader.js'; -import {MediaUtil} from '../media/media-util.js'; +import {getFileExtensionFromAudioMediaType, getFileExtensionFromImageMediaType} from '../media/media-util.js'; import {ClipboardReaderProxy, DictionaryDatabaseProxy, OffscreenProxy, TranslatorProxy} from './offscreen-proxy.js'; -import {ProfileConditionsUtil} from './profile-conditions-util.js'; +import {createSchema, normalizeContext} from './profile-conditions-util.js'; import {RequestBuilder} from './request-builder.js'; import {injectStylesheet} from './script-manager.js'; @@ -95,8 +95,6 @@ export class Backend { this._options = null; /** @type {import('../data/json-schema.js').JsonSchema[]} */ this._profileConditionsSchemaCache = []; - /** @type {ProfileConditionsUtil} */ - this._profileConditionsUtil = new ProfileConditionsUtil(); /** @type {?string} */ this._defaultAnkiFieldTemplates = null; /** @type {RequestBuilder} */ @@ -138,8 +136,6 @@ export class Backend { this._logErrorLevel = null; /** @type {?chrome.permissions.Permissions} */ this._permissions = null; - /** @type {PermissionsUtil} */ - this._permissionsUtil = new PermissionsUtil(); /** @type {Map<string, (() => void)[]>} */ this._applicationReadyHandlers = new Map(); @@ -259,7 +255,7 @@ export class Backend { try { this._prepareInternalSync(); - this._permissions = await this._permissionsUtil.getAllPermissions(); + this._permissions = await getAllPermissions(); this._defaultBrowserActionTitle = await this._getBrowserIconTitle(); this._badgePrepareDelayTimer = setTimeout(() => { this._badgePrepareDelayTimer = null; @@ -545,7 +541,7 @@ export class Backend { for (let i = 0; i < notes.length; ++i) { const note = notes[i]; let canAdd = canAddArray[i]; - const valid = AnkiUtil.isNoteDataValid(note); + const valid = isNoteDataValid(note); if (!valid) { canAdd = false; } const info = {canAdd, valid, noteIds: null}; results.push(info); @@ -815,7 +811,7 @@ export class Backend { let permissionsOkay = false; try { - permissionsOkay = await this._permissionsUtil.hasPermissions({permissions: ['nativeMessaging']}); + permissionsOkay = await hasPermissions({permissions: ['nativeMessaging']}); } catch (e) { // NOP } @@ -1302,7 +1298,7 @@ export class Backend { * @returns {?import('settings').Profile} */ _getProfileFromContext(options, optionsContext) { - const normalizedOptionsContext = this._profileConditionsUtil.normalizeContext(optionsContext); + const normalizedOptionsContext = normalizeContext(optionsContext); let index = 0; for (const profile of options.profiles) { @@ -1312,7 +1308,7 @@ export class Backend { if (index < this._profileConditionsSchemaCache.length) { schema = this._profileConditionsSchemaCache[index]; } else { - schema = this._profileConditionsUtil.createSchema(conditionGroups); + schema = createSchema(conditionGroups); this._profileConditionsSchemaCache.push(schema); } @@ -2128,7 +2124,7 @@ export class Backend { return null; } - let extension = contentType !== null ? MediaUtil.getFileExtensionFromAudioMediaType(contentType) : null; + let extension = contentType !== null ? getFileExtensionFromAudioMediaType(contentType) : null; if (extension === null) { extension = '.mp3'; } let fileName = this._generateAnkiNoteMediaFileName('yomitan_audio', extension, timestamp, definitionDetails); fileName = fileName.replace(/\]/g, ''); @@ -2147,7 +2143,7 @@ export class Backend { const dataUrl = await this._getScreenshot(tabId, frameId, format, quality); const {mediaType, data} = this._getDataUrlInfo(dataUrl); - const extension = MediaUtil.getFileExtensionFromImageMediaType(mediaType); + const extension = getFileExtensionFromImageMediaType(mediaType); if (extension === null) { throw new Error('Unknown media type for screenshot image'); } @@ -2169,7 +2165,7 @@ export class Backend { } const {mediaType, data} = this._getDataUrlInfo(dataUrl); - const extension = MediaUtil.getFileExtensionFromImageMediaType(mediaType); + const extension = getFileExtensionFromImageMediaType(mediaType); if (extension === null) { throw new Error('Unknown media type for clipboard image'); } @@ -2215,7 +2211,7 @@ export class Backend { let fileName = null; if (media !== null) { const {content, mediaType} = media; - const extension = MediaUtil.getFileExtensionFromImageMediaType(mediaType); + const extension = getFileExtensionFromImageMediaType(mediaType); fileName = this._generateAnkiNoteMediaFileName( `yomitan_dictionary_media_${i + 1}`, extension !== null ? extension : '', @@ -2611,7 +2607,7 @@ export class Backend { * @returns {Promise<void>} */ async _checkPermissions() { - this._permissions = await this._permissionsUtil.getAllPermissions(); + this._permissions = await getAllPermissions(); this._updateBadge(); } @@ -2628,7 +2624,7 @@ export class Backend { */ _hasRequiredPermissionsForSettings(options) { if (!this._canObservePermissionsChanges()) { return true; } - return this._permissions === null || this._permissionsUtil.hasRequiredPermissionsForOptions(this._permissions, options); + return this._permissions === null || hasRequiredPermissionsForOptions(this._permissions, options); } /** @@ -2663,7 +2659,7 @@ export class Backend { const results = []; for (const item of await this._dictionaryDatabase.getMedia(targets)) { const {content, dictionary, height, mediaType, path, width} = item; - const content2 = ArrayBufferUtil.arrayBufferToBase64(content); + const content2 = arrayBufferToBase64(content); results.push({content: content2, dictionary, height, mediaType, path, width}); } return results; |