当前位置: 首页 > 文档资料 > Electron 中文文档 >

剪贴板

优质
小牛编辑
162浏览
2023-12-01

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

进程: Main, Renderer

下面的示例演示如何将字符串写入剪贴板:

const { clipboard } = require('electron')
clipboard.writeText('Example String')

On Linux, there is also a selection clipboard. To manipulate it you need to pass selection to each method:

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

clipboard

Perform copy and paste operations on the system clipboard.

Process: Main, Renderer

The following example shows how to write a string to the clipboard:

const { clipboard } = require('electron')
clipboard.writeText('Example String')

On Linux, there is also a selection clipboard. To manipulate it you need to pass selection to each method:

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

方法

clipboard 对象具有以下方法:

注意: 被标记为实验性的 api 将来可能被删除。

Methods

The clipboard module has the following methods:

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

clipboard.readText([type])

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

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

clipboard.readText([type])

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

Returns String - The content in the clipboard as plain text.

clipboard.writeText(text[, type])

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

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

clipboard.writeText(text[, type])

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

Writes the text into the clipboard as plain text.

clipboard.readHTML([type])

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

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

clipboard.readHTML([type])

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

Returns String - The content in the clipboard as markup.

clipboard.writeHTML(markup[, type])

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

markup 写入剪贴板。

clipboard.writeHTML(markup[, type])

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

Writes markup to the clipboard.

clipboard.readImage([type])

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

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

clipboard.readImage([type])

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

Returns NativeImage - The image content in the clipboard.

clipboard.writeImage(image[, type])

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

image 写入剪贴板。

clipboard.writeImage(image[, type])

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

Writes image to the clipboard.

clipboard.readRTF([type])

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

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

clipboard.readRTF([type])

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

Returns String - The content in the clipboard as RTF.

clipboard.writeRTF(text[, type])

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

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

clipboard.writeRTF(text[, type])

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

Writes the text into the clipboard in RTF.

clipboard.readBookmark() macOS Windows

返回 Object:

  • title String
  • url String

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

clipboard.readBookmark() macOS Windows

Returns Object:

  • title String
  • url String

Returns an Object containing title and url keys representing the bookmark in the clipboard. The title and url values will be empty strings when the bookmark is unavailable.

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

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

将书签的 titleurl 写入剪贴板。

注意:Windows上的大多数应用程序不支持粘贴书签,因此您可以使用 clipboard.write 将书签和后备文本写入剪贴板。

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

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

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

Writes the title and url into the clipboard as a bookmark.

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.

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

clipboard.readFindText() macOS

返回 String- 查找粘贴板上的文本。 此方法在从渲染进程调用时使用同步 IPC。 每当激活应用程序时, 都会从查找粘贴板中重新读取缓存值。

clipboard.readFindText() macOS

Returns String - The text on the find pasteboard. 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

text 作为纯文本写入查找粘贴板。此方法在从渲染进程调用时使用同步 IPC。

clipboard.writeFindText(text) macOS

  • text String

Writes the text into the find pasteboard 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. selection is only available on Linux.

清除剪贴板内容。

clipboard.clear([type])

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

Clears the clipboard content.

clipboard.availableFormats([type])

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

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

clipboard.availableFormats([type])

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

Returns String[] - An array of supported formats for the clipboard type.

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

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

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

const { clipboard } = require('electron')
console.log(clipboard.has('<p>selection</p>'))

clipboard.has(format[, type]) Experimental

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

Returns Boolean - Whether the clipboard supports the specified format.

const { clipboard } = require('electron')
console.log(clipboard.has('<p>selection</p>'))

clipboard.read(format) 实验功能

  • format String

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

clipboard.read(format) Experimental

  • format String

Returns String - Reads format type from the clipboard.

clipboard.readBuffer(format) 实验功能

  • format String

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

clipboard.readBuffer(format) Experimental

  • format String

Returns Buffer - Reads format type from the clipboard.

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

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

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

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

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

Writes the buffer into the clipboard as format.

clipboard.write(data[, type])

  • data Object

    • text String(可选)
    • html String(可选)
    • image NativeImage (可选)
    • rtf String (可选)
    • bookmark String (可选)- url 的标题 text
  • type String (optional) - Can be selection or clipboard. selection is only available on Linux.
const { clipboard } = require('electron')
clipboard.write({ text: 'test', html: '<b>test</b>' })

data 写入剪贴板。

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.
  • type String (optional) - Can be selection or clipboard. selection is only available on Linux.
const { clipboard } = require('electron')
clipboard.write({ text: 'test', html: '<b>test</b>' })

Writes data to the clipboard.