clipboard
在系统剪贴板上执行复制和粘贴操作。
¥Perform copy and paste operations on the system clipboard.
¥Process: Main, Renderer (non-sandboxed only)
[!重要] 如果你想从启用了上下文隔离的渲染进程调用此 API,请将 API 调用放在预加载脚本中,并使用
contextBridgeAPI 对其进行 expose 操作。¥[!IMPORTANT] If you want to call this API from a renderer process with context isolation enabled, place the API call in your preload script and expose it using the
contextBridgeAPI.
在 Linux 上,还有一个 selection 剪贴板。要操作它,你需要将 selection 传递给每个方法:
¥On Linux, there is also a selection clipboard. To manipulate it
you need to pass selection to each method:
const { clipboard } = require('electron')
clipboard.writeText('Example string', 'selection')
console.log(clipboard.readText('selection'))
方法
¥Methods
clipboard 模块有以下方法:
¥The clipboard module has the following methods:
[!NOTE] 实验性 API 被标记为实验性 API,将来可能会被移除。
¥[!NOTE] Experimental APIs are marked as such and could be removed in future.
clipboard.readText([type])
-
type字符串(可选) - 可以是selection或clipboard;默认为 'clipboard'。selection仅在 Linux 上可用。¥
typestring (optional) - Can beselectionorclipboard; default is 'clipboard'.selectionis only available on Linux.
返回 string - 剪贴板中的内容为纯文本。
¥Returns string - The content in the clipboard as plain text.
const { clipboard } = require('electron')
clipboard.writeText('hello i am a bit of text!')
const text = clipboard.readText()
console.log(text)
// hello i am a bit of text!'
clipboard.writeText(text[, type])
-
text字符串¥
textstring -
type字符串(可选) - 可以是selection或clipboard;默认为 'clipboard'。selection仅在 Linux 上可用。¥
typestring (optional) - Can beselectionorclipboard; default is 'clipboard'.selectionis only available on Linux.
将 text 作为纯文本写入剪贴板。
¥Writes the text into the clipboard as plain text.
const { clipboard } = require('electron')
const text = 'hello i am a bit of text!'
clipboard.writeText(text)
clipboard.readHTML([type])
-
type字符串(可选) - 可以是selection或clipboard;默认为 'clipboard'。selection仅在 Linux 上可用。¥
typestring (optional) - Can beselectionorclipboard; default is 'clipboard'.selectionis only available on Linux.
返回 string - 剪贴板中的内容作为标记。
¥Returns string - The content in the clipboard as markup.
const { clipboard } = require('electron')
clipboard.writeHTML('<b>Hi</b>')
const html = clipboard.readHTML()
console.log(html)
// <meta charset='utf-8'><b>Hi</b>
clipboard.writeHTML(markup[, type])
-
markup字符串¥
markupstring -
type字符串(可选) - 可以是selection或clipboard;默认为 'clipboard'。selection仅在 Linux 上可用。¥
typestring (optional) - Can beselectionorclipboard; default is 'clipboard'.selectionis only available on Linux.
将 markup 写入剪贴板。
¥Writes markup to the clipboard.
const { clipboard } = require('electron')
clipboard.writeHTML('<b>Hi</b>')
clipboard.readImage([type])
-
type字符串(可选) - 可以是selection或clipboard;默认为 'clipboard'。selection仅在 Linux 上可用。¥
typestring (optional) - Can beselectionorclipboard; default is 'clipboard'.selectionis only available on Linux.
返回 NativeImage - 剪贴板中的图片内容。
¥Returns NativeImage - The image content in the clipboard.
clipboard.writeImage(image[, type])
-
imageNativeImage -
type字符串(可选) - 可以是selection或clipboard;默认为 'clipboard'。selection仅在 Linux 上可用。¥
typestring (optional) - Can beselectionorclipboard; default is 'clipboard'.selectionis only available on Linux.
将 image 写入剪贴板。
¥Writes image to the clipboard.
clipboard.readRTF([type])
-
type字符串(可选) - 可以是selection或clipboard;默认为 'clipboard'。selection仅在 Linux 上可用。¥
typestring (optional) - Can beselectionorclipboard; default is 'clipboard'.selectionis only available on Linux.
返回 string - 剪贴板中的内容为 RTF。
¥Returns string - The content in the clipboard as RTF.
const { clipboard } = require('electron')
clipboard.writeRTF('{\\rtf1\\ansi{\\fonttbl\\f0\\fswiss Helvetica;}\\f0\\pard\nThis is some {\\b bold} text.\\par\n}')
const rtf = clipboard.readRTF()
console.log(rtf)
// {\\rtf1\\ansi{\\fonttbl\\f0\\fswiss Helvetica;}\\f0\\pard\nThis is some {\\b bold} text.\\par\n}
clipboard.writeRTF(text[, type])
-
text字符串¥
textstring -
type字符串(可选) - 可以是selection或clipboard;默认为 'clipboard'。selection仅在 Linux 上可用。¥
typestring (optional) - Can beselectionorclipboard; default is 'clipboard'.selectionis only available on Linux.
将 text 以 RTF 格式写入剪贴板。
¥Writes the text into the clipboard in RTF.
const { clipboard } = require('electron')
const rtf = '{\\rtf1\\ansi{\\fonttbl\\f0\\fswiss Helvetica;}\\f0\\pard\nThis is some {\\b bold} text.\\par\n}'
clipboard.writeRTF(rtf)
clipboard.readBookmark() macOS Windows
返回 Object:
¥Returns Object:
-
title字符串¥
titlestring -
url字符串¥
urlstring
返回一个包含代表剪贴板中书签的 title 和 url 键的对象。当书签不可用时,title 和 url 值将为空字符串。在 Windows 上,title 值始终为空。
¥Returns an Object containing title and url keys representing the bookmark in
the clipboard. The title and url values will be empty strings when the
bookmark is unavailable. The title value will always be empty on Windows.
clipboard.writeBookmark(title, url[, type]) macOS Windows
-
title字符串 - Windows 上未使用¥
titlestring - Unused on Windows -
url字符串¥
urlstring -
type字符串(可选) - 可以是selection或clipboard;默认为 'clipboard'。selection仅在 Linux 上可用。¥
typestring (optional) - Can beselectionorclipboard; default is 'clipboard'.selectionis only available on Linux.
将 title(仅限 macOS)和 url 作为书签写入剪贴板。
¥Writes the title (macOS only) and url into the clipboard as a bookmark.
[!NOTE] Windows 上的大多数应用不支持粘贴书签,因此你可以使用
clipboard.write将书签和后备文本写入剪贴板。¥[!NOTE] Most apps on Windows don't support pasting bookmarks into them so you can use
clipboard.writeto write both a bookmark and fallback text to the clipboard.
const { clipboard } = require('electron')
clipboard.writeBookmark('Electron Homepage', 'https://electron.nodejs.cn')
clipboard.readFindText() macOS
返回 string - 查找粘贴板上的文本,该粘贴板保存有关活动应用查找面板当前状态的信息。
¥Returns string - The text on the find pasteboard, which is the pasteboard that holds information about the current state of the active application’s find panel.
当从渲染器进程调用时,此方法使用同步 IPC。每当激活应用时,都会从查找粘贴板中重新读取缓存的值。
¥This method uses synchronous IPC when called from the renderer process. The cached value is reread from the find pasteboard whenever the application is activated.
clipboard.writeFindText(text) macOS
-
text字符串¥
textstring
将 text 以纯文本形式写入查找粘贴板(保存有关活动应用查找面板当前状态信息的粘贴板)。当从渲染器进程调用时,此方法使用同步 IPC。
¥Writes the text into the find pasteboard (the pasteboard that holds information about the current state of the active application’s find panel) as plain text. This method uses synchronous IPC when called from the renderer process.
clipboard.clear([type])
-
type字符串(可选) - 可以是selection或clipboard;默认为 'clipboard'。selection仅在 Linux 上可用。¥
typestring (optional) - Can beselectionorclipboard; default is 'clipboard'.selectionis only available on Linux.
清除剪贴板内容。
¥Clears the clipboard content.
clipboard.availableFormats([type])
-
type字符串(可选) - 可以是selection或clipboard;默认为 'clipboard'。selection仅在 Linux 上可用。¥
typestring (optional) - Can beselectionorclipboard; default is 'clipboard'.selectionis only available on Linux.
返回 string[] - 剪贴板 type 支持的一系列格式。
¥Returns string[] - An array of supported formats for the clipboard type.
const { clipboard } = require('electron')
const formats = clipboard.availableFormats()
console.log(formats)
// [ 'text/plain', 'text/html' ]
clipboard.has(format[, type]) 实验性的
¥clipboard.has(format[, type]) Experimental
-
format字符串¥
formatstring -
type字符串(可选) - 可以是selection或clipboard;默认为 'clipboard'。selection仅在 Linux 上可用。¥
typestring (optional) - Can beselectionorclipboard; default is 'clipboard'.selectionis only available on Linux.
返回 boolean - 剪贴板是否支持指定的 format。
¥Returns boolean - Whether the clipboard supports the specified format.
const { clipboard } = require('electron')
const hasFormat = clipboard.has('public/utf8-plain-text')
console.log(hasFormat)
// 'true' or 'false'
clipboard.read(format) 实验性的
¥clipboard.read(format) Experimental
-
format字符串¥
formatstring
返回 string - 从剪贴板读取 format 类型。
¥Returns string - Reads format type from the clipboard.
format 应包含有效的 ASCII 字符并具有 / 分隔符。a/c、a/bc 是有效格式,而 /abc、abc/、a/、/a、a 无效。
¥format should contain valid ASCII characters and have / separator.
a/c, a/bc are valid formats while /abc, abc/, a/, /a, a
are not valid.
clipboard.readBuffer(format) 实验性的
¥clipboard.readBuffer(format) Experimental
-
format字符串¥
formatstring
返回 Buffer - 从剪贴板读取 format 类型。
¥Returns Buffer - Reads format type from the clipboard.
const { clipboard } = require('electron')
const buffer = Buffer.from('this is binary', 'utf8')
clipboard.writeBuffer('public/utf8-plain-text', buffer)
const ret = clipboard.readBuffer('public/utf8-plain-text')
console.log(buffer.equals(ret))
// true
clipboard.writeBuffer(format, buffer[, type]) 实验性的
¥clipboard.writeBuffer(format, buffer[, type]) Experimental
-
format字符串¥
formatstring -
buffer缓冲¥
bufferBuffer -
type字符串(可选) - 可以是selection或clipboard;默认为 'clipboard'。selection仅在 Linux 上可用。¥
typestring (optional) - Can beselectionorclipboard; default is 'clipboard'.selectionis only available on Linux.
将 buffer 作为 format 写入剪贴板。
¥Writes the buffer into the clipboard as format.
const { clipboard } = require('electron')
const buffer = Buffer.from('writeBuffer', 'utf8')
clipboard.writeBuffer('public/utf8-plain-text', buffer)
clipboard.write(data[, type])
-
data对象¥
dataObject-
text字符串(可选)¥
textstring (optional) -
html字符串(可选)¥
htmlstring (optional) -
imageNativeImage(可选)¥
imageNativeImage (optional) -
rtf字符串(可选)¥
rtfstring (optional) -
bookmark字符串(可选) -text处的 URL 标题。¥
bookmarkstring (optional) - The title of the URL attext.
-
-
type字符串(可选) - 可以是selection或clipboard;默认为 'clipboard'。selection仅在 Linux 上可用。¥
typestring (optional) - Can beselectionorclipboard; default is 'clipboard'.selectionis only available on Linux.
将 data 写入剪贴板。
¥Writes data to the clipboard.
const { clipboard } = require('electron')
clipboard.write({
text: 'test',
html: '<b>Hi</b>',
rtf: '{\\rtf1\\utf8 text}',
bookmark: 'a title'
})
console.log(clipboard.readText())
// 'test'
console.log(clipboard.readHTML())
// <meta charset='utf-8'><b>Hi</b>
console.log(clipboard.readRTF())
// '{\\rtf1\\utf8 text}'
console.log(clipboard.readBookmark())
// { title: 'a title', url: 'test' }