From 13cfb1d031a1e947bc237a3b4c51aef4960bddac Mon Sep 17 00:00:00 2001 From: David Tejada Date: Fri, 28 Apr 2023 13:39:46 +0200 Subject: [PATCH] Fix bug introduced in v0.4.1 where no command is able to run if there is no content script --- e2e/noContentScript.test.ts | 21 ++++++++++++++++++++- src/background/commands/dispatchCommand.ts | 7 ++++++- 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/e2e/noContentScript.test.ts b/e2e/noContentScript.test.ts index 8aab31db..39acd4cc 100644 --- a/e2e/noContentScript.test.ts +++ b/e2e/noContentScript.test.ts @@ -1,6 +1,9 @@ import { clipboard } from "@nut-tree/nut-js"; import { ResponseToTalon } from "../src/typings/RequestFromTalon"; -import { rangoCommandWithTarget } from "./utils/rangoCommands"; +import { + rangoCommandWithTarget, + rangoCommandWithoutTarget, +} from "./utils/rangoCommands"; import { sleep } from "./utils/testHelpers"; beforeEach(async () => { @@ -20,3 +23,19 @@ describe("Direct clicking", () => { expect(found).toBeTruthy(); }); }); + +describe("Background commands", () => { + test("Commands that don't need the content script are still able to run", async () => { + await rangoCommandWithoutTarget("copyLocationProperty", "href"); + const clip = await clipboard.getContent(); + const response = JSON.parse(clip) as ResponseToTalon; + const action = response.actions[0]!; + + expect(action).toBeDefined(); + + const textToCopy = action.textToCopy; + + expect(textToCopy).toBeDefined(); + expect(textToCopy).toBe("chrome://new-tab-page/"); + }); +}); diff --git a/src/background/commands/dispatchCommand.ts b/src/background/commands/dispatchCommand.ts index 40b4bdaa..f8580e37 100644 --- a/src/background/commands/dispatchCommand.ts +++ b/src/background/commands/dispatchCommand.ts @@ -39,7 +39,12 @@ const backgroundCommands = new Set([ export async function dispatchCommand( command: RangoAction ): Promise { - await sendRequestToContent({ type: "allowToastNotification" }); + try { + await sendRequestToContent({ type: "allowToastNotification" }); + } catch { + // No content script. We do nothing. + } + const result = (await (backgroundCommands.has(command.type) ? runBackgroundCommand(command) : sendRequestToContent(command))) as string | TalonAction[] | undefined;