剪贴板

    进程: , Renderer

    在 Linux 上,还有一个 selection 粘贴板 。 想要操作该剪切板,你需要为每个函数传递 selection 参数。

    clipboard 对象具有以下方法:

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

    • type String (optional) -可以是 selectionclipboard; 默认为 ‘clipboard’. selection 仅在 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) -可以是 selectionclipboard; 默认为 ‘clipboard’. selection 仅在 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) -可以是 selectionclipboard; 默认为 ‘clipboard’. selection 仅在 Linux 中可用。

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

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

    clipboard.writeHTML(markup[, type])

    • markup String
    • type String (optional) -可以是 selectionclipboard; 默认为 ‘clipboard’. selection 仅在 Linux 中可用。

    markup 写入剪贴板。

    • type String (optional) -可以是 selectionclipboard; 默认为 ‘clipboard’. selection 仅在 Linux 中可用。

    clipboard.writeImage(image[, type])

    • image
    • type String (optional) -可以是 selectionclipboard; 默认为 ‘clipboard’. selection 仅在 Linux 中可用。

    image 写入剪贴板。

    • type String (optional) -可以是 selectionclipboard; 默认为 ‘clipboard’. selection 仅在 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}

    • text String
    • type String (optional) -可以是 selectionclipboard; 默认为 ‘clipboard’. selection 仅在 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)

    返回 Object:

    • title String
    • url String

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

    • title String
    • url String
    • type String (optional) -可以是 selectionclipboard; 默认为 ‘clipboard’. selection 仅在 Linux 中可用。

    将书签的 titleurl 写入剪贴板。

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

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

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

    • text String

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

    • type String (optional) -可以是 selectionclipboard; 默认为 ‘clipboard’. selection 仅在 Linux 中可用。

    清除剪贴板内容。

    • type String (optional) -可以是 selectionclipboard; 默认为 ‘clipboard’. selection 仅在 Linux 中可用。

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

    • format String
    • type String (optional) -可以是 selectionclipboard; 默认为 ‘clipboard’. selection 仅在 Linux 中可用。

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

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

    • format String

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

    • 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

    • format String
    • buffer Buffer
    • type String (optional) -可以是 selectionclipboard; 默认为 ‘clipboard’. selection 仅在 Linux 中可用。

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

    1. const { clipboard } = require('electron')
    2. const buffer = Buffer.from('writeBuffer', 'utf8')
    3. clipboard.writeBuffer('public.utf8-plain-text', buffer)
    • data Object
      • text String(可选)
      • html String(可选)
      • image NativeImage (可选)
      • rtf String (可选)
      • bookmark String (可选)- URL 的标题 text
    • type String (optional) -可以是 selectionclipboard; 默认为 ‘clipboard’. 仅在 Linux 中可用。

    data 写入剪贴板。