Skip to content

Commit

Permalink
Merge branch 'master' into language-select
Browse files Browse the repository at this point in the history
  • Loading branch information
StefanVukovic99 committed Jan 31, 2024
2 parents 39ec942 + 2356223 commit 96a0b0e
Show file tree
Hide file tree
Showing 49 changed files with 2,255 additions and 2,269 deletions.
18 changes: 10 additions & 8 deletions ext/js/accessibility/google-docs-util.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,10 @@ import {TextSourceRange} from '../dom/text-source-range.js';
* This class is a helper for handling Google Docs content in content scripts.
*/
export class GoogleDocsUtil {
/** @type {HTMLStyleElement|undefined} */
static _styleNode = void 0;
constructor() {
/** @type {?HTMLStyleElement} */
this._styleNode = null;
}

/**
* Scans the document for text or elements with text information at the given coordinate.
Expand All @@ -34,7 +36,7 @@ export class GoogleDocsUtil {
* @param {import('document-util').GetRangeFromPointOptions} options Options to configure how element detection is performed.
* @returns {?TextSourceRange} A range for the hovered text or element, or `null` if no applicable content was found.
*/
static getRangeFromPoint(x, y, {normalizeCssZoom}) {
getRangeFromPoint(x, y, {normalizeCssZoom}) {
const styleNode = this._getStyleNode();
styleNode.disabled = false;
const element = document.elementFromPoint(x, y);
Expand All @@ -55,8 +57,8 @@ export class GoogleDocsUtil {
* which allows them to be included in document.elementsFromPoint's return value.
* @returns {HTMLStyleElement}
*/
static _getStyleNode() {
if (typeof this._styleNode === 'undefined') {
_getStyleNode() {
if (this._styleNode === null) {
const style = document.createElement('style');
style.textContent = [
'.kix-canvas-tile-content{pointer-events:none!important;}',
Expand All @@ -79,7 +81,7 @@ export class GoogleDocsUtil {
* @param {boolean} normalizeCssZoom
* @returns {TextSourceRange}
*/
static _createRange(element, text, x, y, normalizeCssZoom) {
_createRange(element, text, x, y, normalizeCssZoom) {
// Create imposter
const content = document.createTextNode(text);
const svgText = document.createElementNS('http://www.w3.org/2000/svg', 'text');
Expand Down Expand Up @@ -120,7 +122,7 @@ export class GoogleDocsUtil {
* @param {boolean} normalizeCssZoom
* @returns {Range}
*/
static _getRangeWithPoint(textNode, x, y, normalizeCssZoom) {
_getRangeWithPoint(textNode, x, y, normalizeCssZoom) {
if (normalizeCssZoom) {
const scale = DocumentUtil.computeZoomScale(textNode);
x /= scale;
Expand Down Expand Up @@ -149,7 +151,7 @@ export class GoogleDocsUtil {
* @param {string} propertyName
* @param {string} value
*/
static _setImportantStyle(style, propertyName, value) {
_setImportantStyle(style, propertyName, value) {
style.setProperty(propertyName, value, 'important');
}
}
9 changes: 7 additions & 2 deletions ext/js/app/frontend.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {log} from '../core/logger.js';
import {promiseAnimationFrame} from '../core/utilities.js';
import {DocumentUtil} from '../dom/document-util.js';
import {TextSourceElement} from '../dom/text-source-element.js';
import {TextSourceGenerator} from '../dom/text-source-generator.js';
import {TextSourceRange} from '../dom/text-source-range.js';
import {TextScanner} from '../language/text-scanner.js';
import {yomitan} from '../yomitan.js';
Expand Down Expand Up @@ -84,14 +85,17 @@ export class Frontend {
this._contentScale = 1.0;
/** @type {Promise<void>} */
this._lastShowPromise = Promise.resolve();
/** @type {TextSourceGenerator} */
this._textSourceGenerator = new TextSourceGenerator();
/** @type {TextScanner} */
this._textScanner = new TextScanner({
node: window,
ignoreElements: this._ignoreElements.bind(this),
ignorePoint: this._ignorePoint.bind(this),
getSearchContext: this._getSearchContext.bind(this),
searchTerms: true,
searchKanji: true
searchKanji: true,
textSourceGenerator: this._textSourceGenerator
});
/** @type {boolean} */
this._textScannerHasBeenEnabled = false;
Expand Down Expand Up @@ -949,6 +953,7 @@ export class Frontend {
*/
async _prepareGoogleDocs() {
const {GoogleDocsUtil} = await import('../accessibility/google-docs-util.js');
DocumentUtil.registerGetRangeFromPointHandler(GoogleDocsUtil.getRangeFromPoint.bind(GoogleDocsUtil));
const googleDocsUtil = new GoogleDocsUtil();
this._textSourceGenerator.registerGetRangeFromPointHandler(googleDocsUtil.getRangeFromPoint.bind(googleDocsUtil));
}
}
38 changes: 17 additions & 21 deletions ext/js/background/backend.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,20 +25,20 @@ import {createApiMap, invokeApiMapHandler} from '../core/api-map.js';
import {ExtensionError} from '../core/extension-error.js';
import {log} from '../core/logger.js';
import {clone, deferPromise, fetchJson, fetchText, 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 {LanguageUtil} from '../language/language-util.js';
import {distributeFuriganaInflected, isCodePointJapanese, isStringPartiallyJapanese, convertKatakanaToHiragana as jpConvertKatakanaToHiragana} from '../language/languages/ja/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, LanguageUtilProxy, 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';

Expand Down Expand Up @@ -100,8 +100,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} */
Expand Down Expand Up @@ -143,8 +141,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();

Expand Down Expand Up @@ -266,7 +262,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;
Expand Down Expand Up @@ -554,7 +550,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);
Expand Down Expand Up @@ -824,7 +820,7 @@ export class Backend {

let permissionsOkay = false;
try {
permissionsOkay = await this._permissionsUtil.hasPermissions({permissions: ['nativeMessaging']});
permissionsOkay = await hasPermissions({permissions: ['nativeMessaging']});
} catch (e) {
// NOP
}
Expand Down Expand Up @@ -1321,7 +1317,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) {
Expand All @@ -1331,7 +1327,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);
}

Expand Down Expand Up @@ -2109,7 +2105,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, '');
Expand All @@ -2128,7 +2124,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');
}
Expand All @@ -2150,7 +2146,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');
}
Expand Down Expand Up @@ -2196,7 +2192,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 : '',
Expand Down Expand Up @@ -2595,7 +2591,7 @@ export class Backend {
* @returns {Promise<void>}
*/
async _checkPermissions() {
this._permissions = await this._permissionsUtil.getAllPermissions();
this._permissions = await getAllPermissions();
this._updateBadge();
}

Expand All @@ -2612,7 +2608,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);
}

/**
Expand Down Expand Up @@ -2647,7 +2643,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;
Expand Down
4 changes: 2 additions & 2 deletions ext/js/background/offscreen-proxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

import {ExtensionError} from '../core/extension-error.js';
import {isObject} from '../core/utilities.js';
import {ArrayBufferUtil} from '../data/sandbox/array-buffer-util.js';
import {base64ToArrayBuffer} from '../data/sandbox/array-buffer-util.js';

export class OffscreenProxy {
/**
Expand Down Expand Up @@ -144,7 +144,7 @@ export class DictionaryDatabaseProxy {
*/
async getMedia(targets) {
const serializedMedia = /** @type {import('dictionary-database').Media<string>[]} */ (await this._offscreen.sendMessagePromise({action: 'databaseGetMediaOffscreen', params: {targets}}));
const media = serializedMedia.map((m) => ({...m, content: ArrayBufferUtil.base64ToArrayBuffer(m.content)}));
const media = serializedMedia.map((m) => ({...m, content: base64ToArrayBuffer(m.content)}));
return media;
}
}
Expand Down
4 changes: 2 additions & 2 deletions ext/js/background/offscreen.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

import {ClipboardReader} from '../comm/clipboard-reader.js';
import {createApiMap, invokeApiMapHandler} from '../core/api-map.js';
import {ArrayBufferUtil} from '../data/sandbox/array-buffer-util.js';
import {arrayBufferToBase64} from '../data/sandbox/array-buffer-util.js';
import {DictionaryDatabase} from '../dictionary/dictionary-database.js';
import {LanguageUtil} from '../language/language-util.js';
import {Translator} from '../language/translator.js';
Expand Down Expand Up @@ -117,7 +117,7 @@ export class Offscreen {
/** @type {import('offscreen').ApiHandler<'databaseGetMediaOffscreen'>} */
async _getMediaHandler({targets}) {
const media = await this._dictionaryDatabase.getMedia(targets);
const serializedMedia = media.map((m) => ({...m, content: ArrayBufferUtil.arrayBufferToBase64(m.content)}));
const serializedMedia = media.map((m) => ({...m, content: arrayBufferToBase64(m.content)}));
return serializedMedia;
}

Expand Down
Loading

0 comments on commit 96a0b0e

Please sign in to comment.