clipboard
Perform copy and paste operations on the system clipboard.在系统剪贴板上执行复制和粘贴操作。
On Linux, there is also a 在Linux上,还有一个selection clipboard. 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])
typestring (optional) -Can be可以是selectionorclipboard; default is 'clipboard'.selection或clipboard;默认为clipboard。selectionis 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])
textstringtypestring (optional) -Can be可以是selectionorclipboard; default is 'clipboard'.selection或clipboard;默认为clipboard。selectionis 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])
typestring (optional) -Can be可以是selectionorclipboard; default is 'clipboard'.selection或clipboard;默认为clipboard。selectionis 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])
markupstringtypestring (optional) -Can be可以是selectionorclipboard; default is 'clipboard'.selection或clipboard;默认为clipboard。selectionis only available on Linux.仅在Linux上可用。
Writes 将markup to the clipboard.markup写入剪贴板。
const { clipboard } = require('electron')
clipboard.writeHTML('<b>Hi</b>')
clipboard.readImage([type])
typestring (optional) -Can be可以是selectionorclipboard; default is 'clipboard'.selection或clipboard;默认为clipboard。selectionis only available on Linux.仅在Linux上可用。
Returns 返回NativeImage - The image content in the clipboard.剪贴板中的图像内容。
clipboard.writeImage(image[, type])
imageNativeImagetypestring (optional) -Can be可以是selectionorclipboard; default is 'clipboard'.selection或clipboard;默认为clipboard。selectionis only available on Linux.仅在Linux上可用。
Writes 将image to the clipboard.image写入剪贴板。
clipboard.readRTF([type])
typestring (optional) -Can be可以是selectionorclipboard; default is 'clipboard'.selection或clipboard;默认为clipboard。selectionis 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])
textstringtypestring (optional) -Can be可以是selectionorclipboard; default is 'clipboard'.selection或clipboard;默认为clipboard。selectionis 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:
titlestringurlstring
Returns 返回an Object containing 一个对象,包含表示剪贴板中书签的title and url keys representing the bookmark in the clipboard. title和url键。The 当书签不可用时,title and url values will be empty strings when the bookmark is unavailable. title和url值将为空字符串。The 在Windows上,title value will always be empty on Windows.title值将始终为空。
clipboard.writeBookmark(title, url[, type]) macOS Windows
titlestring -Unused on Windows在Windows上未使用urlstringtypestring (optional) -Can be可以是selectionorclipboard; default is 'clipboard'.selection或clipboard;默认为clipboard。selectionis 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 Windows上的大多数应用程序都不支持将书签粘贴到其中,因此您可以使用clipboard.write to write both a bookmark and fallback text to the clipboard.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
textstring
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])
typestring (optional) -Can be可以是selectionorclipboard; default is 'clipboard'.selection或clipboard;默认为clipboard。selectionis only available on Linux.仅在Linux上可用。
Clears the clipboard content.清除剪贴板内容。
clipboard.availableFormats([type])
typestring (optional) -Can be可以是selectionorclipboard; default is 'clipboard'.selection或clipboard;默认为clipboard。selectionis 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
formatstringtypestring (optional) -Can be可以是selectionorclipboard; default is 'clipboard'.selection或clipboard;默认为clipboard。selectionis 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
formatstring
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/c、a/bc是有效格式,而/abc、abc/、a/、/a、a则无效。
clipboard.readBuffer(format) Experimental
formatstring
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
formatstringbufferBuffertypestring (optional) -Can be可以是selectionorclipboard; default is 'clipboard'.selection或clipboard;默认为clipboard。selectionis 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])
dataObjecttextstring (optional)htmlstring (optional)imageNativeImage (optional)rtfstring (optional)bookmarkstring (optional) -The title of the URL attext.text处URL的标题。
typestring (optional) -Can be可以是selectionorclipboard; default is 'clipboard'.selection或clipboard;默认为clipboard。selectionis 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' }