简体   繁体   English

如何让 JavaScript execCommand("paste") 在 Electron 中工作?

[英]How do I allow JavaScript execCommand("paste") to work in Electron?

When the user clicks on a specific button on my screen I want to paste the content of the clipboard into a specific text field.当用户单击屏幕上的特定按钮时,我想将剪贴板的内容粘贴到特定文本字段中。

I currently use the code:我目前使用的代码:

pasteSelectedTextFromClipboard() {
    const input = this.$refs.input as any
    input.focus()
    document.execCommand("paste")
}

When I Google I find that in Firefox it's necessary to set user_pref("capability.policy.policynames", "allowclipboard");当我 Google 时,我发现在Firefox 中需要设置user_pref("capability.policy.policynames", "allowclipboard"); to allow this functionality to work.以允许此功能工作。 What do I have to do to get it to work in Electron?我需要做什么才能让它在 Electron 中工作?

Electron 为剪贴板的所有操作公开了非常好的API

You can useclipboard.readText() from the Electron clipboard API for this:您可以使用 Electron 剪贴板 API 中的clipboard.readText()来实现:

const { clipboard } = require("electron")

const input = document.getElementById("textbox")
input.value = clipboard.readText()

An added advantage: you will no longer need to focus the input element, as you are programmatically setting its value.一个额外的优势:您将不再需要关注输入元素,因为您正在以编程方式设置其值。

Note that this will replace all of the input's current text by what is in the clipboard.请注意,这将用剪贴板中的内容替换所有输入的当前文本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM