跳转到主要内容

clipboard

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

进程:主进程渲染进程 已弃用(仅限非沙盒模式)

注意

从渲染进程使用 clipboard API 已被弃用。

信息

如果您想从渲染进程调用此 API,请将 API 调用放在您的预加载脚本中,并使用 暴露 它,通过 contextBridge API。

在 Linux 上,还有一个 selection 剪贴板。要操作它,您需要将 selection 传递给每个方法

const { clipboard } = require('electron')

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

方法

clipboard 模块包含以下方法

注意

实验性 API 会被标记为实验性,并且可能在未来被移除。

clipboard.readText([type])

  • type 字符串 (可选) - 可以是 selectionclipboard;默认值为 'clipboard'。selection 仅在 Linux 上可用。

返回 string - 剪贴板中的内容,以纯文本形式。

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 字符串 (可选) - 可以是 selectionclipboard;默认值为 'clipboard'。selection 仅在 Linux 上可用。

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

const { clipboard } = require('electron')

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

clipboard.readHTML([type])

  • type 字符串 (可选) - 可以是 selectionclipboard;默认值为 'clipboard'。selection 仅在 Linux 上可用。

返回 string - 剪贴板中的内容,以标记形式。

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 字符串
  • type 字符串 (可选) - 可以是 selectionclipboard;默认值为 'clipboard'。selection 仅在 Linux 上可用。

markup 写入剪贴板。

const { clipboard } = require('electron')

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

clipboard.readImage([type])

  • type 字符串 (可选) - 可以是 selectionclipboard;默认值为 'clipboard'。selection 仅在 Linux 上可用。

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

clipboard.writeImage(image[, type])

  • image NativeImage
  • type 字符串 (可选) - 可以是 selectionclipboard;默认值为 'clipboard'。selection 仅在 Linux 上可用。

image 写入剪贴板。

clipboard.readRTF([type])

  • type 字符串 (可选) - 可以是 selectionclipboard;默认值为 'clipboard'。selection 仅在 Linux 上可用。

返回 string - 剪贴板中的内容,以 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 字符串 (可选) - 可以是 selectionclipboard;默认值为 'clipboard'。selection 仅在 Linux 上可用。

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

返回 Object

  • title string
  • url string

返回一个包含 titleurl 键的对象,表示剪贴板中的书签。当书签不可用时,titleurl 的值将为空字符串。在 Windows 上,title 的值始终为空。

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

  • title 字符串 - 在 Windows 上未使用
  • url string
  • type 字符串 (可选) - 可以是 selectionclipboard;默认值为 'clipboard'。selection 仅在 Linux 上可用。

title (仅限 macOS) 和 url 写入剪贴板,作为书签。

注意

Windows 上的大多数应用程序不支持将书签粘贴到其中,因此您可以使用 clipboard.write 将书签和回退文本同时写入剪贴板。

const { clipboard } = require('electron')

clipboard.writeBookmark('Electron Homepage', 'https://electron.js.cn')

clipboard.readFindText() macOS

返回 string - 查找粘贴板上的文本,该粘贴板保存有关活动应用程序的查找面板的当前状态的信息。

从渲染进程调用时,此方法使用同步 IPC。缓存的值会在应用程序激活时从查找粘贴板重新读取。

clipboard.writeFindText(text) macOS

  • text string

text 写入查找粘贴板(保存有关活动应用程序的查找面板的当前状态的信息),作为纯文本。从渲染进程调用时,此方法使用同步 IPC。

clipboard.clear([type])

  • type 字符串 (可选) - 可以是 selectionclipboard;默认值为 'clipboard'。selection 仅在 Linux 上可用。

清除剪贴板内容。

clipboard.availableFormats([type])

  • type 字符串 (可选) - 可以是 selectionclipboard;默认值为 'clipboard'。selection 仅在 Linux 上可用。

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

const { clipboard } = require('electron')

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

clipboard.has(format[, type]) 实验性

  • format 字符串
  • type 字符串 (可选) - 可以是 selectionclipboard;默认值为 'clipboard'。selection 仅在 Linux 上可用。

返回 boolean - 剪贴板是否支持指定的 format

const { clipboard } = require('electron')

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

clipboard.read(format) 实验性

  • format 字符串

返回 string - 从剪贴板读取 format 类型。

format 应包含有效的 ASCII 字符,并具有 / 分隔符。a/ca/bc 是有效的格式,而 /abcabc/a//aa 无效。

clipboard.readBuffer(format) 实验性

  • format 字符串

返回 Buffer - 从剪贴板读取 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(ret))
// true

clipboard.writeBuffer(format, buffer[, type]) 实验性

  • format 字符串
  • buffer Buffer
  • type 字符串 (可选) - 可以是 selectionclipboard;默认值为 'clipboard'。selection 仅在 Linux 上可用。

buffer 写入剪贴板,作为 format

const { clipboard } = require('electron')

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

clipboard.write(data[, type])

  • data 对象
    • text 字符串 (可选)
    • html 字符串 (可选)
    • image NativeImage (可选)
    • rtf 字符串 (可选)
    • bookmark 字符串 (可选) - text 处的 URL 的标题。
  • type 字符串 (可选) - 可以是 selectionclipboard;默认值为 'clipboard'。selection 仅在 Linux 上可用。

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