-
Notifications
You must be signed in to change notification settings - Fork 3
Add Edit commands #3
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Copy is still broken in this context on Windows. That’s why I implemented commit Do note that I only tested this on Stable and Developer Edition, but not on Nightly. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I use Nightly, which may have a different behaviour. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hmm, actually, it doesn't work for me either in this context. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Actually it does work, it seems like your latest commit breaks this though. |
||
} | ||
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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Note to self: This comment needs to updated, select all works properly on inputs. |
||
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: { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -44,13 +44,13 @@ along with this program. If not, see <http://www.gnu.org/licenses/>. | |
<div class="panel-section"> | ||
<i class="eb-icon-placeholder"/> | ||
<i class="panel-list-item text" data-i18n="popup_edit"/> | ||
<div class="panel-list-item icon" data-ipc-message="editCut" data-remain-open="true"> | ||
<div class="panel-list-item icon" data-ipc-message="editCut"> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It doesn't work with data-remain-open=true |
||
<i class="eb-icon-placeholder" data-icon="clipboard-cut"/> | ||
</div> | ||
<div class="panel-list-item icon" data-ipc-message="editCopy" data-remain-open="true"> | ||
<div class="panel-list-item icon" data-ipc-message="editCopy"> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ditto |
||
<i class="eb-icon-placeholder" data-icon="clipboard-copy"/> | ||
</div> | ||
<div class="panel-list-item icon" data-ipc-message="editPaste" data-remain-open="true"> | ||
<div class="panel-list-item icon" data-ipc-message="editPaste"> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ditto |
||
<i class="eb-icon-placeholder" data-icon="clipboard-paste"/> | ||
</div> | ||
<div class="panel-list-item icon" data-sub-menu="edit"> | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should stay here, because it ensures that the edit menu isn’t enabled when the content script can’t start (i.e. the page is privileged (
about:
,chrome://
orresource://
)).Yes, I do realize that this is contained in the
*
selector above, but that one will be removed once the menu is fully implemented, while theedit*
selector and any other selector dependant on the content script will be kept.