Skip to content

Commit 998bab4

Browse files
NI57721philc
authored andcommitted
Use Clipboard API instead of execCommand("Paste")
1 parent c5f232c commit 998bab4

File tree

3 files changed

+7
-5
lines changed

3 files changed

+7
-5
lines changed

content_scripts/hud.js

+3
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ const HUD = {
3636
this.hudUI.toggleIframeElementClasses("vimiumUIComponentHidden", "vimiumUIComponentVisible");
3737
// Force the re-computation of styles, so Chrome sends a visibility change message to the child frame.
3838
// See https://github.com/philc/vimium/pull/3277#issuecomment-487363284
39+
40+
// Allow to access the clipboard through iframes.
41+
this.hudUI.iframeElement.allow = "clipboard-read";
3942
getComputedStyle(this.hudUI.iframeElement).display;
4043
} else {
4144
this.hudUI.toggleIframeElementClasses("vimiumClickable", "vimiumNonClickable");

lib/clipboard.js

+2-3
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,11 @@ const Clipboard = {
2020
},
2121

2222
// Returns a string representing the clipboard contents. Supports rich text clipboard values.
23-
paste() {
23+
async paste() {
2424
const textArea = this._createTextArea("div"); // Use a <div> so Firefox pastes rich text.
2525
document.body.appendChild(textArea);
2626
textArea.focus();
27-
document.execCommand("Paste");
28-
const value = textArea.innerText;
27+
const value = await navigator.clipboard.readText();
2928
document.body.removeChild(textArea);
3029
// When copying &nbsp; characters, they get converted to \xa0. Convert to space instead. See #2217.
3130
return value.replace(/\xa0/g, " ");

pages/hud.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -153,9 +153,9 @@ const handlers = {
153153
},
154154

155155
pasteFromClipboard() {
156-
Utils.setTimeout(TIME_TO_WAIT_FOR_IPC_MESSAGES, function() {
156+
Utils.setTimeout(TIME_TO_WAIT_FOR_IPC_MESSAGES, async function() {
157157
const focusedElement = document.activeElement;
158-
const data = Clipboard.paste();
158+
const data = await Clipboard.paste();
159159
if (focusedElement != null)
160160
focusedElement.focus();
161161
window.parent.focus();

0 commit comments

Comments
 (0)