Skip to main content

clipboard

Perform copy and paste operations on the system clipboard.在系统剪贴板上执行复制和粘贴操作。

Process: 进程:Main, Renderer

On Linux, there is also a selection clipboard. 在Linux上,还有一个selection剪贴板。To manipulate it you need to pass selection to each method:要操作它,您需要将selection传递给每个方法:

const { clipboard } = require('electron')

clipboard.writeText('Example string', 'selection')
console.log(clipboard.readText('selection'))

Methods方法

The clipboard module has the following methods:clipboard模块具有以下方法:

Note: Experimental APIs are marked as such and could be removed in future.实验API被标记为这样,将来可能会被删除。

clipboard.readText([type])

  • type string (optional) - Can be selection or clipboard; default is 'clipboard'. 可以是selectionclipboard;默认为clipboardselection is only available on Linux.仅在Linux上可用。

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 string
  • type string (optional) - Can be selection or clipboard; default is 'clipboard'. 可以是selectionclipboard;默认为clipboardselection is only available on Linux.仅在Linux上可用。

Writes the text into the clipboard as plain text.text以纯文本形式写入剪贴板。

const { clipboard } = require('electron')

const text = 'hello i am a bit of text!'
clipboard.writeText(text)

clipboard.readHTML([type])

  • type string (optional) - Can be selection or clipboard; default is 'clipboard'. 可以是selectionclipboard;默认为clipboardselection is only available on Linux.仅在Linux上可用。

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 string
  • type string (optional) - Can be selection or clipboard; default is 'clipboard'. 可以是selectionclipboard;默认为clipboardselection is only available on Linux.仅在Linux上可用。

Writes markup to the clipboard.markup写入剪贴板。

const { clipboard } = require('electron')

clipboard.writeHTML('<b>Hi</b>')

clipboard.readImage([type])

  • type string (optional) - Can be selection or clipboard; default is 'clipboard'. 可以是selectionclipboard;默认为clipboardselection is only available on Linux.仅在Linux上可用。

Returns 返回NativeImage - The image content in the clipboard.剪贴板中的图像内容。

clipboard.writeImage(image[, type])

  • image NativeImage
  • type string (optional) - Can be selection or clipboard; default is 'clipboard'. 可以是selectionclipboard;默认为clipboardselection is only available on Linux.仅在Linux上可用。

Writes image to the clipboard.image写入剪贴板。

clipboard.readRTF([type])

  • type string (optional) - Can be selection or clipboard; default is 'clipboard'. 可以是selectionclipboard;默认为clipboardselection is only available on Linux.仅在Linux上可用。

Returns 返回string - The content in the clipboard as RTF.剪贴板中的内容为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 string
  • type string (optional) - Can be selection or clipboard; default is 'clipboard'. 可以是selectionclipboard;默认为clipboardselection is only available on Linux.仅在Linux上可用。

Writes the text into the clipboard in RTF.text以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

Returns 返回Object:

  • title string
  • url string

Returns 返回an Object containing title and url keys representing the bookmark in the clipboard. 一个对象,包含表示剪贴板中书签的titleurl键。The title and url values will be empty strings when the bookmark is unavailable. 当书签不可用时,titleurl值将为空字符串。The title value will always be empty on Windows.在Windows上,title值将始终为空。

clipboard.writeBookmark(title, url[, type]) macOS Windows

  • title string - Unused on Windows在Windows上未使用
  • url string
  • type string (optional) - Can be selection or clipboard; default is 'clipboard'. 可以是selectionclipboard;默认为clipboardselection is only available on Linux.仅在Linux上可用。

Writes the title (macOS only) and url into the clipboard as a bookmark.title(仅限macOS)和url作为书签写入剪贴板。

Note: Most apps on Windows don't support pasting bookmarks into them so you can use clipboard.write to write both a bookmark and fallback text to the clipboard.Windows上的大多数应用程序都不支持将书签粘贴到其中,因此您可以使用clipboard.write将书签和回退文本写入剪贴板。

const { clipboard } = require('electron')

clipboard.writeBookmark({
text: 'https://electronjs.org',
bookmark: 'Electron Homepage'
})

clipboard.readFindText() macOS

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.查找粘贴板上的文本,该粘贴板保存有关活动应用程序的查找面板的当前状态的信息。

This method uses synchronous IPC when called from the renderer process. 当从渲染器进程调用时,此方法使用同步IPC。The cached value is reread from the find pasteboard whenever the application is activated.每当应用程序被激活时,缓存的值都会从find粘贴板中重新读取。

clipboard.writeFindText(text) macOS

  • text string

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. text以纯文本形式写入查找粘贴板(保存有关活动应用程序的查找面板的当前状态的信息的粘贴板)。This method uses synchronous IPC when called from the renderer process.当从渲染器进程调用时,此方法使用同步IPC。

clipboard.clear([type])

  • type string (optional) - Can be selection or clipboard; default is 'clipboard'. 可以是selectionclipboard;默认为clipboardselection is only available on Linux.仅在Linux上可用。

Clears the clipboard content.清除剪贴板内容。

clipboard.availableFormats([type])

  • type string (optional) - Can be selection or clipboard; default is 'clipboard'. 可以是selectionclipboard;默认为clipboardselection is only available on Linux.仅在Linux上可用。

Returns 返回string[] - An array of supported formats for the clipboard type.剪贴板type支持的格式数组。

const { clipboard } = require('electron')

const formats = clipboard.availableFormats()
console.log(formats)
// [ 'text/plain', 'text/html' ]

clipboard.has(format[, type]) Experimental

  • format string
  • type string (optional) - Can be selection or clipboard; default is 'clipboard'. 可以是selectionclipboard;默认为clipboardselection is only available on Linux.仅在Linux上可用。

Returns 返回boolean - Whether the clipboard supports the specified format.剪贴板是否支持指定的format

const { clipboard } = require('electron')

const hasFormat = clipboard.has('public/utf8-plain-text')
console.log(hasFormat)
// 'true' or 'false'

clipboard.read(format) Experimental

  • format string

Returns 返回string - Reads format type from the clipboard.从剪贴板中读取format类型。

format should contain valid ASCII characters and have / separator. format应包含有效的ASCII字符并带有/分隔符。a/c, a/bc are valid formats while /abc, abc/, a/, /a, a are not valid.a/ca/bc是有效格式,而/abcabc/a//aa则无效。

clipboard.readBuffer(format) Experimental

  • format string

Returns 返回Buffer - Reads format type from the clipboard.从剪贴板中读取format类型。

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(out))
// true

clipboard.writeBuffer(format, buffer[, type]) Experimental

  • format string
  • buffer Buffer
  • type string (optional) - Can be selection or clipboard; default is 'clipboard'. 可以是selectionclipboard;默认为clipboardselection is only available on Linux.仅在Linux上可用。

Writes the buffer into the clipboard as format.buffer作为format写入剪贴板。

const { clipboard } = require('electron')

const buffer = Buffer.from('writeBuffer', 'utf8')
clipboard.writeBuffer('public/utf8-plain-text', buffer)

clipboard.write(data[, type])

  • data Object
    • text string (optional)
    • html string (optional)
    • image NativeImage (optional)
    • rtf string (optional)
    • bookmark string (optional) - The title of the URL at text.text处URL的标题。
  • type string (optional) - Can be selection or clipboard; default is 'clipboard'. 可以是selectionclipboard;默认为clipboardselection is only available on Linux.仅在Linux上可用。

Writes data to the clipboard.data写入剪贴板。

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' }