handle right click alias creation

This commit is contained in:
Son NK
2024-03-25 23:47:11 +01:00
parent 34077e16d9
commit ffc0092da3
3 changed files with 57 additions and 60 deletions
+50 -55
View File
@@ -1,67 +1,62 @@
import { handleNewRandomAlias } from "./create-alias"; import { handleNewRandomAlias } from "./create-alias";
import browser from "webextension-polyfill"; import browser from "webextension-polyfill";
function generateDialogJS(message) { function displayAndCopy(alias, error) {
return ` function copyTextToClipboard(text) {
function showSLDialog() { if (!text) return;
let slDialog = document.createElement("div"); var textArea = document.createElement("textarea");
slDialog.style.position = "fixed"; textArea.value = text;
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); textArea.style.top = "0";
textArea.style.left = "0";
textArea.style.position = "fixed";
setTimeout(function () { document.body.appendChild(textArea);
document.body.removeChild(slDialog); textArea.focus();
}, 3000); 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) { function generateAliasHandlerJS(tab, res) {
const dialogJS = generateDialogJS( chrome.scripting
res.alias ? res.alias + " copied to clipboard" : "ERROR: " + res.error .executeScript({
); target: { tabId: tab.id },
const js = ` func: displayAndCopy,
function copyTextToClipboard(text) { args: [res.alias || null, res.error || null],
if (!text) return; })
var textArea = document.createElement("textarea"); .then(() => console.log("injected a function"));
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,
});
} }
async function handleOnClickContextMenu(info, tab) { async function handleOnClickContextMenu(info, tab) {
+5 -3
View File
@@ -28,7 +28,7 @@ async function handleGetAppSettings() {
async function handleExtensionSetup() { async function handleExtensionSetup() {
const apiUrl = await SLStorage.get(SLStorage.SETTINGS.API_URL); 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, { const res = await fetch(url, {
method: "POST", method: "POST",
headers: { headers: {
@@ -37,7 +37,7 @@ async function handleExtensionSetup() {
body: JSON.stringify({ body: JSON.stringify({
device: Utils.getDeviceName(), device: Utils.getDeviceName(),
}), }),
}); });
if (res.ok) { if (res.ok) {
const apiRes = await res.json(); const apiRes = await res.json();
@@ -131,11 +131,13 @@ browser.runtime.onMessage.addListener(async function (request, sender) {
* Register context menu * Register context menu
*/ */
browser.contextMenus.create({ browser.contextMenus.create({
id: "sl-random",
title: "Create random email alias (copied)", title: "Create random email alias (copied)",
contexts: ["all"], contexts: ["all"],
onclick: handleOnClickContextMenu,
}); });
browser.contextMenus.onClicked.addListener(handleOnClickContextMenu);
/** /**
* Shortcuts and hotkeys listener * Shortcuts and hotkeys listener
*/ */
+2 -2
View File
@@ -112,8 +112,8 @@ const callAPI = async function (
throw { throw {
response: { response: {
status: res.status, status: res.status,
data: await res.json() data: await res.json(),
} },
}; };
} }
} }