From ffc0092da365bd6badd777bfec6194a1569c2fe0 Mon Sep 17 00:00:00 2001 From: Son NK Date: Mon, 25 Mar 2024 23:47:11 +0100 Subject: [PATCH] handle right click alias creation --- src/background/context-menu.js | 105 ++++++++++++++++----------------- src/background/index.js | 8 ++- src/popup/APIService.js | 4 +- 3 files changed, 57 insertions(+), 60 deletions(-) diff --git a/src/background/context-menu.js b/src/background/context-menu.js index c6d7682..7f2b834 100644 --- a/src/background/context-menu.js +++ b/src/background/context-menu.js @@ -1,67 +1,62 @@ import { handleNewRandomAlias } from "./create-alias"; import browser from "webextension-polyfill"; -function generateDialogJS(message) { - return ` - function showSLDialog() { - let slDialog = document.createElement("div"); - slDialog.style.position = "fixed"; - slDialog.style.bottom = "0"; - slDialog.style.right = "0"; - slDialog.style.margin = "0.7em"; - slDialog.style.padding = "0.7em"; - slDialog.style.fontFamily = "Verdana, Arial, Helvetica, sans-serif"; - slDialog.style.fontSize = "1em"; - slDialog.style.pointerEvents = "none"; - slDialog.style.zIndex = "999999"; - slDialog.style.background = "white"; - slDialog.style.border = "2px solid #777"; - slDialog.style.borderRadius = "5px"; - slDialog.innerText = ${JSON.stringify(message)}; +function displayAndCopy(alias, error) { + function copyTextToClipboard(text) { + if (!text) return; + var textArea = document.createElement("textarea"); + textArea.value = text; - document.body.appendChild(slDialog); + textArea.style.top = "0"; + textArea.style.left = "0"; + textArea.style.position = "fixed"; - setTimeout(function () { - document.body.removeChild(slDialog); - }, 3000); - } + document.body.appendChild(textArea); + textArea.focus(); + textArea.select(); - showSLDialog(); - `; + try { + document.execCommand("copy"); + } catch (err) {} + + document.body.removeChild(textArea); + } + + function showSLDialog(message) { + let slDialog = document.createElement("div"); + slDialog.style.position = "fixed"; + slDialog.style.bottom = "0"; + slDialog.style.right = "0"; + slDialog.style.margin = "0.7em"; + slDialog.style.padding = "0.7em"; + slDialog.style.fontFamily = "Verdana, Arial, Helvetica, sans-serif"; + slDialog.style.fontSize = "1em"; + slDialog.style.pointerEvents = "none"; + slDialog.style.zIndex = "999999"; + slDialog.style.background = "white"; + slDialog.style.border = "2px solid #777"; + slDialog.style.borderRadius = "5px"; + slDialog.innerText = JSON.stringify(message); + + document.body.appendChild(slDialog); + + setTimeout(function () { + document.body.removeChild(slDialog); + }, 3000); + } + + showSLDialog(alias ? alias + " copied to clipboard" : "ERROR: " + error); + copyTextToClipboard(alias); } function generateAliasHandlerJS(tab, res) { - const dialogJS = generateDialogJS( - res.alias ? res.alias + " copied to clipboard" : "ERROR: " + res.error - ); - const js = ` - function copyTextToClipboard(text) { - if (!text) return; - var textArea = document.createElement("textarea"); - textArea.value = text; - - textArea.style.top = "0"; - textArea.style.left = "0"; - textArea.style.position = "fixed"; - - document.body.appendChild(textArea); - textArea.focus(); - textArea.select(); - - try { - document.execCommand("copy"); - } catch (err) {} - - document.body.removeChild(textArea); - } - - ${dialogJS} - - copyTextToClipboard(${JSON.stringify(res.alias)}); - `; - browser.tabs.executeScript(tab.id, { - code: js, - }); + chrome.scripting + .executeScript({ + target: { tabId: tab.id }, + func: displayAndCopy, + args: [res.alias || null, res.error || null], + }) + .then(() => console.log("injected a function")); } async function handleOnClickContextMenu(info, tab) { diff --git a/src/background/index.js b/src/background/index.js index 93c0464..bb982b0 100644 --- a/src/background/index.js +++ b/src/background/index.js @@ -28,7 +28,7 @@ async function handleGetAppSettings() { async function handleExtensionSetup() { const apiUrl = await SLStorage.get(SLStorage.SETTINGS.API_URL); - const url = apiUrl + API_ROUTE.GET_API_KEY_FROM_COOKIE.path; + const url = apiUrl + API_ROUTE.GET_API_KEY_FROM_COOKIE.path; const res = await fetch(url, { method: "POST", headers: { @@ -37,7 +37,7 @@ async function handleExtensionSetup() { body: JSON.stringify({ device: Utils.getDeviceName(), }), - }); + }); if (res.ok) { const apiRes = await res.json(); @@ -131,11 +131,13 @@ browser.runtime.onMessage.addListener(async function (request, sender) { * Register context menu */ browser.contextMenus.create({ + id: "sl-random", title: "Create random email alias (copied)", contexts: ["all"], - onclick: handleOnClickContextMenu, }); +browser.contextMenus.onClicked.addListener(handleOnClickContextMenu); + /** * Shortcuts and hotkeys listener */ diff --git a/src/popup/APIService.js b/src/popup/APIService.js index 8eb1569..bdb53c2 100644 --- a/src/popup/APIService.js +++ b/src/popup/APIService.js @@ -112,8 +112,8 @@ const callAPI = async function ( throw { response: { status: res.status, - data: await res.json() - } + data: await res.json(), + }, }; } }