From 0e83afc9b7a50597b181b8faaf3c86ed032264d9 Mon Sep 17 00:00:00 2001 From: Samuel Maddock Date: Thu, 12 Dec 2024 20:49:36 -0500 Subject: [PATCH] fix: api regressions from mv3 changes --- packages/electron-chrome-extensions/package.json | 1 - .../spec/chrome-contextMenus-spec.ts | 16 ++++++---------- .../spec/spec-helpers.ts | 9 +++++---- .../src/browser/api/browser-action.ts | 6 +++--- .../src/browser/api/context-menus.ts | 6 +++++- yarn.lock | 2 +- 6 files changed, 20 insertions(+), 20 deletions(-) diff --git a/packages/electron-chrome-extensions/package.json b/packages/electron-chrome-extensions/package.json index 6620d10..8fa2e86 100644 --- a/packages/electron-chrome-extensions/package.json +++ b/packages/electron-chrome-extensions/package.json @@ -40,7 +40,6 @@ "mocha": "^8.2.1", "ts-node": "^10.9.1", "typescript": "^4.9.4", - "uuid": "^8.3.1", "walkdir": "^0.4.1", "webpack": "^5.73.0", "webpack-cli": "^4.10.0" diff --git a/packages/electron-chrome-extensions/spec/chrome-contextMenus-spec.ts b/packages/electron-chrome-extensions/spec/chrome-contextMenus-spec.ts index f5fa84b..10d6888 100644 --- a/packages/electron-chrome-extensions/spec/chrome-contextMenus-spec.ts +++ b/packages/electron-chrome-extensions/spec/chrome-contextMenus-spec.ts @@ -1,6 +1,6 @@ import { expect } from 'chai' import { ipcMain } from 'electron' -import { emittedOnce } from './events-helpers' +import { once } from 'node:events' import { useExtensionBrowser, useServer } from './hooks' import { uuid } from './spec-helpers' @@ -13,22 +13,18 @@ describe('chrome.contextMenus', () => { }) const getContextMenuItems = async () => { - const promise = new Promise((resolve) => { - browser.webContents.once('context-menu', (_, params) => { - const items = browser.extensions.getContextMenuItems(browser.webContents, params) - resolve(items) - }) - }) - // TODO: why is this needed since upgrading to Electron 22? await new Promise((resolve) => setTimeout(resolve, 1000)) + const contextMenuPromise = once(browser.webContents, 'context-menu') + // Simulate right-click to create context-menu event. const opts = { x: 0, y: 0, button: 'right' as any } browser.webContents.sendInputEvent({ ...opts, type: 'mouseDown' }) browser.webContents.sendInputEvent({ ...opts, type: 'mouseUp' }) - return await promise + const [, params] = await contextMenuPromise + return browser.extensions.getContextMenuItems(browser.webContents, params) } describe('create()', () => { @@ -74,7 +70,7 @@ describe('chrome.contextMenus', () => { onclick: { __IPC_FN__: ipcName }, }) const items = await getContextMenuItems() - const p = emittedOnce(ipcMain, ipcName) + const p = once(ipcMain, ipcName) items[0].click() await p }) diff --git a/packages/electron-chrome-extensions/spec/spec-helpers.ts b/packages/electron-chrome-extensions/spec/spec-helpers.ts index 38312ca..266fbb6 100644 --- a/packages/electron-chrome-extensions/spec/spec-helpers.ts +++ b/packages/electron-chrome-extensions/spec/spec-helpers.ts @@ -19,9 +19,10 @@ // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -import * as childProcess from 'child_process' -import * as path from 'path' -import * as http from 'http' +import * as childProcess from 'node:child_process' +import * as nodeCrypto from 'node:crypto' +import * as path from 'node:path' +import * as http from 'node:http' import * as v8 from 'v8' import { SuiteFunction, TestFunction } from 'mocha' @@ -132,4 +133,4 @@ export async function getFiles(directoryPath: string, { filter = null }: any = { return files } -export const uuid = () => require('uuid').v4() +export const uuid = () => nodeCrypto.randomUUID() diff --git a/packages/electron-chrome-extensions/src/browser/api/browser-action.ts b/packages/electron-chrome-extensions/src/browser/api/browser-action.ts index 3a774b5..612e1d5 100644 --- a/packages/electron-chrome-extensions/src/browser/api/browser-action.ts +++ b/packages/electron-chrome-extensions/src/browser/api/browser-action.ts @@ -42,8 +42,8 @@ const getBrowserActionDefaults = (extension: Electron.Extension): ExtensionActio manifest.manifest_version === 3 ? manifest.action : manifest.manifest_version === 2 - ? manifest.browser_action - : undefined + ? manifest.browser_action + : undefined if (typeof browserAction === 'object') { const manifestAction: chrome.runtime.ManifestAction = browserAction const action: ExtensionAction = {} @@ -106,7 +106,7 @@ export class BrowserActionAPI { const { tabId } = details let value = details[propName] - if (typeof value === 'undefined') { + if (typeof value === 'undefined' || value === null) { const defaults = getBrowserActionDefaults(extension) value = defaults ? defaults[propName] : value } diff --git a/packages/electron-chrome-extensions/src/browser/api/context-menus.ts b/packages/electron-chrome-extensions/src/browser/api/context-menus.ts index 2402821..106b985 100644 --- a/packages/electron-chrome-extensions/src/browser/api/context-menus.ts +++ b/packages/electron-chrome-extensions/src/browser/api/context-menus.ts @@ -54,7 +54,11 @@ const matchesConditions = ( const { contextTypes, targetUrl, documentUrl } = conditions - const contexts = (Array.isArray(props.contexts) ? props.contexts : [props.contexts]) || DEFAULT_CONTEXTS + const contexts = props.contexts + ? Array.isArray(props.contexts) + ? props.contexts + : [props.contexts] + : DEFAULT_CONTEXTS const inContext = contexts.some((context) => contextTypes.has(context as ContextMenuType)) if (!inContext) return false diff --git a/yarn.lock b/yarn.lock index f6cf00f..385ac5f 100644 --- a/yarn.lock +++ b/yarn.lock @@ -7207,7 +7207,7 @@ utils-merge@1.0.1: resolved "https://registry.yarnpkg.com/utils-merge/-/utils-merge-1.0.1.tgz#9f95710f50a267947b2ccc124741c1028427e713" integrity sha512-pMZTvIkT1d+TFGvDOqodOclx0QWkkgi6Tdoa8gC8ffGAAqz9pzPTZWAybbsHHoED/ztMtkv/VoYTYyShUn81hA== -uuid@^8.3.1, uuid@^8.3.2: +uuid@^8.3.2: version "8.3.2" resolved "https://registry.yarnpkg.com/uuid/-/uuid-8.3.2.tgz#80d5b5ced271bb9af6c445f21a1a04c606cefbe2" integrity sha512-+NYs2QeMWy+GWFOEm9xnn6HCDp0l7QBD7ml8zLUmJ+93Q5NF0NocErnwkTkXVFNiX3/fpC6afS8Dhb/gz7R7eg==