剪贴板

在系统剪贴板上执行复制和粘贴操作。

进程: Main, Renderer

在 Linux 上,还有一个 selection粘贴板 。 To manipulate it you need to pass selection to each method:

  1. const { clipboard } = require('electron')
  2. clipboard.writeText('Example String', 'selection')
  3. console.log(clipboard.readText('selection'))

方法

clipboard 对象具有以下方法:

Note: Experimental APIs are marked as such and could be removed in future.

clipboard.readText([type])

  • type String (optional) - Can be selection or clipboard; default is ‘clipboard’. selection is only available on Linux.

返回 String- 剪贴板中的纯文本内容。

  1. const { clipboard } = require('electron')
  2. clipboard.writeText('hello i am a bit of text!')
  3. const text = clipboard.readText()
  4. console.log(text)
  5. // hello i am a bit of text!'

clipboard.writeText(text[, type])

  • text String
  • type String (optional) - Can be selection or clipboard; default is ‘clipboard’. selection is only available on Linux.

text 作为纯文本写入剪贴板。

  1. const { clipboard } = require('electron')
  2. const text = 'hello i am a bit of text!'
  3. clipboard.writeText(text)

clipboard.readHTML([type])

  • type String (optional) - Can be selection or clipboard; default is ‘clipboard’. selection is only available on Linux.

返回 String- 剪贴板中的HTML内容。

  1. const { clipboard } = require('electron')
  2. clipboard.writeHTML('<b>Hi</b>')
  3. const html = clipboard.readHTML()
  4. console.log(html)
  5. // <meta charset='utf-8'><b>Hi</b>

clipboard.writeHTML(markup[, type])

  • markup String
  • type String (optional) - Can be selection or clipboard; default is ‘clipboard’. selection is only available on Linux.

markup 写入剪贴板。

  1. const { clipboard } = require('electron')
  2. clipboard.writeHTML('<b>Hi</b')

clipboard.readImage([type])

  • type String (optional) - Can be selection or clipboard; default is ‘clipboard’. selection is only available on Linux.

返回 NativeImage- 剪贴板中的图像内容。

clipboard.writeImage(image[, type])

  • image NativeImage
  • type String (optional) - Can be selection or clipboard; default is ‘clipboard’. selection is only available on Linux.

image 写入剪贴板。

clipboard.readRTF([type])

  • type String (optional) - Can be selection or clipboard; default is ‘clipboard’. selection is only available on Linux.

返回 String- 剪贴板中的RTF内容。

  1. const { clipboard } = require('electron')
  2. clipboard.writeRTF('{\\rtf1\\ansi{\\fonttbl\\f0\\fswiss Helvetica;}\\f0\\pard\nThis is some {\\b bold} text.\\par\n}')
  3. const rtf = clipboard.readRTF()
  4. console.log(rtf)
  5. // {\\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’. selection is only available on Linux.

向剪贴板中写入 RTF 格式的 text.

  1. const { clipboard } = require('electron')
  2. const rtf = '{\\rtf1\\ansi{\\fonttbl\\f0\\fswiss Helvetica;}\\f0\\pard\nThis is some {\\b bold} text.\\par\n}'
  3. clipboard.writeRTF(rtf)

clipboard.readBookmark() macOS Windows

返回 Object:

  • title String
  • url String

返回一个对象, 其中包含表示剪贴板中书签 titleurl 。 当书签不可用时, titleurl 值将为空字符串。

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

  • title String
  • url String
  • type String (optional) - Can be selection or clipboard; default is ‘clipboard’. selection is only available on Linux.

将书签的 titleurl 写入剪贴板。

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.

  1. const { clipboard } = require('electron')
  2. clipboard.writeBookmark({
  3. text: 'https://electronjs.org',
  4. bookmark: 'Electron Homepage'
  5. })

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. The cached value is reread from the find pasteboard whenever the application is activated.

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. This method uses synchronous IPC when called from the renderer process.

clipboard.clear([type])

  • type String (optional) - Can be selection or clipboard; default is ‘clipboard’. selection is only available on Linux.

清除剪贴板内容。

clipboard.availableFormats([type])

  • type String (optional) - Can be selection or clipboard; default is ‘clipboard’. selection is only available on Linux.

返回 String []- 剪贴板 type 所支持的格式的数组。

  1. const { clipboard } = require('electron')
  2. const formats = clipboard.availableFormats()
  3. console.log(formats)
  4. // [ 'text/plain', 'text/html' ]

clipboard.has(format[, type]) Experimental

  • format String
  • type String (optional) - Can be selection or clipboard; default is ‘clipboard’. selection is only available on Linux.

返回 Boolean, 剪贴板是否支持指定的 format

  1. const { clipboard } = require('electron')
  2. const hasFormat = clipboard.has('<p>selection</p>')
  3. console.log(hasFormat)
  4. // 'true' or 'false

clipboard.read(format) Experimental

  • format String

返回 String- 从剪贴板中读取 format 类型的内容。

clipboard.readBuffer(format) Experimental

  • format String

返回 Buffer- 从剪贴板中读取 format 类型的内容。

  1. const { clipboard } = require('electron')
  2. const buffer = Buffer.from('this is binary', 'utf8')
  3. clipboard.writeBuffer('public.utf8-plain-text', buffer)
  4. const ret = clipboard.readBuffer('public.utf8-plain-text')
  5. console.log(buffer.equals(out))
  6. // true

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

  • format String
  • buffer Buffer
  • type String (optional) - Can be selection or clipboard; default is ‘clipboard’. selection is only available on Linux.

buffer 作为 format 类型写入剪贴板。

  1. const { clipboard } = require('electron')
  2. const buffer = Buffer.from('writeBuffer', 'utf8')
  3. clipboard.writeBuffer('public.utf8-plain-text', buffer)

clipboard.write(data[, type])

  • data Object
    • text String(可选)
    • html String(可选)
    • image NativeImage (可选)
    • rtf String (可选)
    • bookmark String (optional) - The title of the URL at text.
  • type String (optional) - Can be selection or clipboard; default is ‘clipboard’. selection is only available on Linux.

data 写入剪贴板。

  1. const { clipboard } = require('electron')
  2. clipboard.write({
  3. text: 'test',
  4. html: '<b>Hi</b>',
  5. rtf: '{\\rtf1\\utf8 text}',
  6. bookmark: 'a title'
  7. })
  8. console.log(clipboard.readText())
  9. // 'test'
  10. console.log(clipboard.readHTML())
  11. // <meta charset='utf-8'><b>Hi</b>
  12. console.log(clipboard.readRTF())
  13. // '{\\rtf1\\utf8 text}'
  14. console.log(clipboard.readBookmark())
  15. // { title: 'a title', url: 'test' }