clipboard
在系统剪贴板上执行复制和粘贴操作。
Process: Main, Renderer (non-sandboxed only)
[!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 参数。
const { clipboard } = require('electron')
clipboard.writeText('Example string', 'selection')
console.log(clipboard.readText('selection'))
方法
clipboard 对象具有以下方法:
[!NOTE] Experimental APIs are marked as such and could be removed in future.
clipboard.readText([type])
typestring (optional) -可以是selection或clipboard; 默认为 '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])
textstringtypestring (optional) -可以是selection或clipboard; 默认为 'clipboard'.selection仅在 Linux 中可用。
将 text 作为纯文本写入剪贴板。
const { clipboard } = require('electron')
const text = 'hello i am a bit of text!'
clipboard.writeText(text)
clipboard.readHTML([type])
typestring (optional) -可以是selection或clipboard; 默认为 '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])
markupstringtypestring (optional) -可以是selection或clipboard; 默认为 'clipboard'.selection仅在 Linux 中可用。
将 markup 写入剪贴板。
const { clipboard } = require('electron')
clipboard.writeHTML('<b>Hi</b>')
clipboard.readImage([type])
typestring (optional) -可以是selection或clipboard; 默认为 'clipboard'.selection仅在 Linux 中可用。
Returns NativeImage - The image content in the clipboard.
clipboard.writeImage(image[, type])
imageNativeImagetypestring (optional) -可以是selection或clipboard; 默认为 'clipboard'.selection仅在 Linux 中可用。
将 image 写入剪贴板。
clipboard.readRTF([type])
typestring (optional) -可以是selection或clipboard; 默认为 '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])
textstringtypestring (optional) -可以是selection或clipboard; 默认为 'clipboard'.selection仅在 Linux 中可用。
向剪贴板中写入 RTF 格式的 text.
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:
titlestringurlstring
返回一个对象, 其中包含表示剪贴板中书签 title 和 url 。 当书签不可用时, title 和 url 值将为空字符串。 Windows上的 title 值将永远是空的。
clipboard.writeBookmark(title, url[, type]) macOS Windows
titlestring - Windows 未使用urlstringtypestring (optional) -可以是selection或clipboard; 默认为 'clipboard'.selection仅在 Linux 中可用。
将 title (仅限 macOS) 和 url 写入粘贴板,并作为书签。
[!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://electronjs.org')
clipboard.readFindText() macOS
返回 string - "查找粘贴板"上的文本,这是保存有关活动应用程序查找面板当前状态信息的粘贴板。
此方法从渲染进程调用时使用同步 IPC。 每当激活应用程序时, 都会从查找粘贴板中重新读取缓存值。
clipboard.writeFindText(text) macOS
textstring
将 text 作为纯文本写入查找粘贴板(粘贴板上有关于当前应用程序查找面板状态的信息)。 此方法从渲染进程调用时使用同步 IPC。
clipboard.clear([type])
typestring (optional) -可以是selection或clipboard; 默认为 'clipboard'.selection仅在 Linux 中可用。
清除剪贴板内容。
clipboard.availableFormats([type])
typestring (optional) -可以是selection或clipboard; 默认为 'clipboard'.selection仅在 Linux 中可用。
返回 string[] - 剪贴板 type 支持格式的数组。
const { clipboard } = require('electron')
const formats = clipboard.availableFormats()
console.log(formats)
// [ 'text/plain', 'text/html' ]
clipboard.has(format[, type]) 实验功能
formatstringtypestring (optional) -可以是selection或clipboard; 默认为 'clipboard'.selection仅在 Linux 中可用。
返回 boolean - 剪贴板是否支持指定的 format。
const { clipboard } = require('electron')
const hasFormat = clipboard.has('public/utf8-plain-text')
console.log(hasFormat)
// 'true' 或 'false'
clipboard.read(format) 实验功能
formatstring
返回 string - 从剪贴板读取指定 format 类型内容。
format 应该包含有效的 ASCII 字符和 / 分隔符。 a/c, a/bc are valid formats while /abc, abc/, a/, /a, a are not valid.
clipboard.readBuffer(format) 实验功能
formatstring
返回 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]) 实验功能
formatstringbufferBuffertypestring (optional) -可以是selection或clipboard; 默认为 '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])
dataObjecttextstring(可选)htmlstring(可选)imageNativeImage (optional)rtfstring (可选)bookmarkstring (可选)- URL 的标题text。
typestring (optional) -可以是selection或clipboard; 默认为 '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' }