From 13278a5cf67de69678d8c4c5fb97e6eb00c94c11 Mon Sep 17 00:00:00 2001 From: James Maa Date: Thu, 9 May 2024 15:42:35 +0800 Subject: Update eslint unsafe rule (#887) * Enable @typescript-eslint/no-unsafe-assignment * Updates * Add missing import * Updates * Fix types? * Fix tests * Address comments * Move TextProcessorVariant to types * Update types/ext/translation-internal.d.ts Co-authored-by: StefanVukovic99 Signed-off-by: James Maa --------- Signed-off-by: James Maa Co-authored-by: toasted-nutbread Co-authored-by: StefanVukovic99 --- ext/js/background/backend.js | 29 +++++++++++++++++++---------- ext/js/background/offscreen-proxy.js | 1 + ext/js/background/script-manager.js | 28 ++++++++++++++++++---------- 3 files changed, 38 insertions(+), 20 deletions(-) (limited to 'ext/js/background') diff --git a/ext/js/background/backend.js b/ext/js/background/backend.js index 294c11db..d042a253 100644 --- a/ext/js/background/backend.js +++ b/ext/js/background/backend.js @@ -1831,16 +1831,7 @@ export class Backend { } try { - const tabWindow = await new Promise((resolve, reject) => { - chrome.windows.get(tab.windowId, {}, (value) => { - const e = chrome.runtime.lastError; - if (e) { - reject(new Error(e.message)); - } else { - resolve(value); - } - }); - }); + const tabWindow = await this._getWindow(tab.windowId); if (!tabWindow.focused) { await /** @type {Promise} */ (new Promise((resolve, reject) => { chrome.windows.update(tab.windowId, {focused: true}, () => { @@ -1858,6 +1849,23 @@ export class Backend { } } + /** + * @param {number} windowId + * @returns {Promise} + */ + _getWindow(windowId) { + return new Promise((resolve, reject) => { + chrome.windows.get(windowId, {}, (value) => { + const e = chrome.runtime.lastError; + if (e) { + reject(new Error(e.message)); + } else { + resolve(value); + } + }); + }); + } + /** * @param {number} tabId * @param {number} frameId @@ -2208,6 +2216,7 @@ export class Backend { async _injectAnkiNoteDictionaryMedia(ankiConnect, timestamp, dictionaryMediaDetails) { const targets = []; const detailsList = []; + /** @type {Map} */ const detailsMap = new Map(); for (const {dictionary, path} of dictionaryMediaDetails) { const target = {dictionary, path}; diff --git a/ext/js/background/offscreen-proxy.js b/ext/js/background/offscreen-proxy.js index e65ec65e..9e7b5b74 100644 --- a/ext/js/background/offscreen-proxy.js +++ b/ext/js/background/offscreen-proxy.js @@ -88,6 +88,7 @@ export class OffscreenProxy { if (!chrome.runtime.getContexts) { // Chrome version below 116 // Clients: https://developer.mozilla.org/en-US/docs/Web/API/ServiceWorkerGlobalScope/clients // @ts-expect-error - Types not set up for service workers yet + // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment const matchedClients = await clients.matchAll(); // @ts-expect-error - Types not set up for service workers yet return await matchedClients.some((client) => client.url === offscreenUrl); diff --git a/ext/js/background/script-manager.js b/ext/js/background/script-manager.js index 84213452..ea1702e9 100644 --- a/ext/js/background/script-manager.js +++ b/ext/js/background/script-manager.js @@ -60,16 +60,7 @@ export function injectStylesheet(type, content, tabId, frameId, allFrames) { * @returns {Promise} `true` if a script is registered, `false` otherwise. */ export async function isContentScriptRegistered(id) { - const scripts = await new Promise((resolve, reject) => { - chrome.scripting.getRegisteredContentScripts({ids: [id]}, (result) => { - const e = chrome.runtime.lastError; - if (e) { - reject(new Error(e.message)); - } else { - resolve(result); - } - }); - }); + const scripts = await getRegisteredContentScripts([id]); for (const script of scripts) { if (script.id === id) { return true; @@ -155,3 +146,20 @@ function createContentScriptRegistrationOptions(details, id) { } return options; } + +/** + * @param {string[]} ids + * @returns {Promise} + */ +function getRegisteredContentScripts(ids) { + return new Promise((resolve, reject) => { + chrome.scripting.getRegisteredContentScripts({ids}, (result) => { + const e = chrome.runtime.lastError; + if (e) { + reject(new Error(e.message)); + } else { + resolve(result); + } + }); + }); +} -- cgit v1.2.3