From 3c6b302fcc101ac44bd0a58af2e80daa883f72be Mon Sep 17 00:00:00 2001 From: Tim Nguyen Date: Sun, 20 Aug 2017 21:44:54 +0200 Subject: [PATCH] Add support for Edit commands --- src/background.js | 1 - src/content/content.js | 46 ++++++++++++++++++++++++++++++++---------- src/popup/popup.xhtml | 6 +++--- 3 files changed, 38 insertions(+), 15 deletions(-) diff --git a/src/background.js b/src/background.js index d67341f..46636c4 100644 --- a/src/background.js +++ b/src/background.js @@ -113,7 +113,6 @@ async function handlePopupMessage(message) { let result = { disable: [ "*", - "edit*", "dev*", "workOffline", "openHelpHealthReport", diff --git a/src/content/content.js b/src/content/content.js index 8d119a0..fe5cbf6 100644 --- a/src/content/content.js +++ b/src/content/content.js @@ -21,7 +21,17 @@ var amLoaded = amLoaded || false; if (!amLoaded) { amLoaded = true; browser.runtime.onMessage.addListener(async (message, sender) => { + let focusedElement = document.activeElement; + let isFocusable = focusedElement && focusedElement !== document.body; let method = String(message.method); + let execCommand = (command) => { + window.focus(); + if (isFocusable) { + focusedElement.focus(); + } + document.execCommand(command); + }; + switch (method) { case "init": { let result = { @@ -31,27 +41,41 @@ if (!amLoaded) { "saveAs" ] } - if (document.documentElement.requestFullScreen || document.documentElement.mozRequestFullScreen) { - //result.enable.push("fullscreen"); - } - if (document.querySelector(":focus")) { + + if (isFocusable && focusedElement.isContentEditable) { result.enable.push("edit*"); + } else if (isFocusable) { + result.enable.push("editCut", "editCopy", "editSelectAll"); + } else { + result.enable.push("editCopy", "editSelectAll"); } return result; } case "editCut": { - return document.execCommand("cut"); + return execCommand("cut"); } case "editCopy": { - return document.execCommand("copy"); + return execCommand("copy"); } case "editPaste": { - return document.execCommand("paste"); + return execCommand("paste"); } case "editUndo": { - return document.execCommand("undo"); + return execCommand("undo"); } case "editRedo": { - return document.execCommand("redo"); + return execCommand("redo"); } case "editSelectAll": { - return document.execCommand("selectAll"); + // Select all doesn't work reliably on inputs yet + if (isFocusable) { + window.focus(); + focusedElement.focus(); + return focusedElement.select(); + } + + // ...so we select everything on the page + let selection = window.getSelection(); + let range = document.createRange(); + range.selectNodeContents(document.documentElement); + selection.removeAllRanges(); + return selection.addRange(range); } case "editDelete": { - return document.execCommand("delete"); + return execCommand("delete"); } case "print": { return window.print(); } default: { diff --git a/src/popup/popup.xhtml b/src/popup/popup.xhtml index ec366bd..ab14449 100644 --- a/src/popup/popup.xhtml +++ b/src/popup/popup.xhtml @@ -44,13 +44,13 @@ along with this program. If not, see .
-
+
-
+
-
+