Skip to content

Commit

Permalink
Send response back with every request but change order in Chromium to…
Browse files Browse the repository at this point in the history
… avoid stealing focus
  • Loading branch information
david-tejada committed May 24, 2022
1 parent 728fe49 commit 5f72d68
Showing 1 changed file with 21 additions and 8 deletions.
29 changes: 21 additions & 8 deletions src/background/background.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import browser from "webextension-polyfill";
import { Message } from "../types/types";
import {
getMessageFromClipboard,
writeResponseToClipboard,
Expand All @@ -21,14 +22,12 @@ browser.commands.onCommand.addListener(async (internalCommand: string) => {
if (internalCommand === "get-talon-request") {
try {
const request = await getMessageFromClipboard();
let response = await dispatchCommand(request.action);
// Because of the way I had to implement copying and pasting to the clipboard in Chromium,
// sending a response requires focusing the textarea element dedicated for it, which might
// close popup elements or have other unintended consequences, therefore I will only send
// a response back when it's absolutely necessary. Even though for firefox this is not a problem,
// I will not differentiate for simplicity
const commandsThatRequireResponse = ["copyLink"];
if (commandsThatRequireResponse.includes(request.action.type)) {
const commandsThatChangeTheClipboard = new Set(["copyLink"]);
if (
navigator.clipboard ||
commandsThatChangeTheClipboard.has(request.action.type)
) {
let response = await dispatchCommand(request.action);
const changedClipboard = await getClipboardIfChanged();
if (changedClipboard) {
response = {
Expand All @@ -42,6 +41,20 @@ browser.commands.onCommand.addListener(async (internalCommand: string) => {

const adaptedResponse = adaptResponse(response, request.version ?? 0);
await writeResponseToClipboard(adaptedResponse);
} else {
const response: Message = {
type: "response",
action: {
type: "ok",
},
};
const adaptedResponse = adaptResponse(response, request.version ?? 0);
await writeResponseToClipboard(adaptedResponse);
// Because of the way I had to implement copying and pasting to the clipboard in Chromium,
// sending a response requires focusing the textarea element dedicated for it, which might
// close popup elements or have other unintended consequences, therefore I will first send
// the response back and then execute the command
await dispatchCommand(request.action);
}
} catch (error: unknown) {
let errorMessage = "Error: There was an error";
Expand Down

0 comments on commit 5f72d68

Please sign in to comment.