From 466152ed42c701903ad2a67f4cd8406460135e7e Mon Sep 17 00:00:00 2001 From: Ajay Date: Sat, 4 Jun 2022 01:56:00 -0400 Subject: [PATCH] Fix popup copy to clipboard --- src/content.ts | 3 +++ src/messageTypes.ts | 7 ++++++- src/popup.ts | 15 +++++++++++++-- 3 files changed, 22 insertions(+), 3 deletions(-) diff --git a/src/content.ts b/src/content.ts index 75f16766..46dbd913 100644 --- a/src/content.ts +++ b/src/content.ts @@ -220,6 +220,9 @@ function messageListener(request: Message, sender: unknown, sendResponse: (respo case "closePopup": closeInfoMenu(); break; + case "copyToClipboard": + navigator.clipboard.writeText(request.text); + break; } } diff --git a/src/messageTypes.ts b/src/messageTypes.ts index 45941ee4..470c95a6 100644 --- a/src/messageTypes.ts +++ b/src/messageTypes.ts @@ -42,7 +42,12 @@ interface HideSegmentMessage { UUID: SegmentUUID; } -export type Message = BaseMessage & (DefaultMessage | BoolValueMessage | IsInfoFoundMessage | SubmitVoteMessage | HideSegmentMessage); +interface CopyToClipboardMessage { + message: "copyToClipboard"; + text: string; +} + +export type Message = BaseMessage & (DefaultMessage | BoolValueMessage | IsInfoFoundMessage | SubmitVoteMessage | HideSegmentMessage | CopyToClipboardMessage); export interface IsInfoFoundMessageResponse { found: boolean; diff --git a/src/popup.ts b/src/popup.ts index 0cb156b5..927454d6 100644 --- a/src/popup.ts +++ b/src/popup.ts @@ -178,7 +178,7 @@ async function runThePopup(messageListener?: MessageListener): Promise { PageElements.optionsButton.addEventListener("click", openOptions); PageElements.helpButton.addEventListener("click", openHelp); PageElements.refreshSegmentsButton.addEventListener("click", refreshSegments); - PageElements.sbPopupIconCopyUserID.addEventListener("click", async () => navigator.clipboard.writeText(await utils.getHash(Config.config.userID))); + PageElements.sbPopupIconCopyUserID.addEventListener("click", async () => copyToClipboard(await utils.getHash(Config.config.userID))); //show proper disable skipping button const disableSkipping = Config.config.disableSkipping; @@ -496,7 +496,7 @@ async function runThePopup(messageListener?: MessageListener): Promise { uuidButton.src = chrome.runtime.getURL("icons/clipboard.svg"); uuidButton.title = chrome.i18n.getMessage("copySegmentID"); uuidButton.addEventListener("click", () => { - navigator.clipboard.writeText(UUID); + copyToClipboard(UUID); const stopAnimation = AnimationUtils.applyLoadingAnimation(uuidButton, 0.3); stopAnimation(); }); @@ -850,6 +850,17 @@ async function runThePopup(messageListener?: MessageListener): Promise { hiddenButton.style.display = "none"; } + function copyToClipboard(text: string): void { + if (window === window.top) { + window.navigator.clipboard.writeText(text); + } else { + sendTabMessage({ + message: "copyToClipboard", + text + }); + } + } + /** * Converts time in minutes to 2d 5h 25.1 * If less than 1 hour, just returns minutes