Skip to main content

clipboard

Realizar cópia e colar operações na área de transferência do sistema.

Processo: Main, Renderer

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

Métodos

O módulo clipboard possui os seguintes métodos:

Nota: APIs experimentais são marcadas como tal e podem ser removidas no futuro.

clipboard.readText([type])

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

Retorna String - o conteúdo da área de transferência como texto sem formatação.

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 String (optional) - Can be selection or clipboard; default is 'clipboard'. selection is only available on Linux.

Escreve o text na área de transferência como texto sem formatação.

const { clipboard } = require('electron')

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

clipboard.readHTML([type])

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

Retorna String - o conteúdo da área de transferência como marcação.

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 String
  • type String (optional) - Can be selection or clipboard; default is 'clipboard'. selection is only available on Linux.

Escreve markup na área de transferência.

const { clipboard } = require('electron')

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

clipboard.readImage([type])

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

Retorna Nativeimage - o conteúdo da imagem na área de transferência.

clipboard.writeImage(image[, type])

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

Escreve image na área de transferência.

clipboard.readRTF([type])

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

Retorna String - o conteúdo da área de transferência como 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 String (optional) - Can be selection or clipboard; default is 'clipboard'. selection is only available on Linux.

Escreve o text na área de transferência em 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

Retorna Object:

  • title String
  • String url

Retorna um Objeto que contém as chaves title e url representando o bookmark na área de transferência. Os valores de title e url serão strings vazias quando o bookmark estiver indisponível. The title value will always be empty on Windows.

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

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

Writes the title (macOS only) 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.

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. The cached value is reread from the find pasteboard whenever the application is activated.

clipboard.writeFindText(text) macOS

  • text String

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. This method uses synchronous IPC when called from the renderer process.

clipboard.clear([type])

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

Clears the clipboard content.

clipboard.availableFormats([type])

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

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

const { clipboard } = require('electron')

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

clipboard.has(format[, type]) Experimental

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

Returns Boolean - Whether the clipboard supports the specified format.

const { clipboard } = require('electron')

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

clipboard.read(format) Experimental

  • format String

Returns String - Reads format type from the clipboard.

format should contain valid ASCII characters and have / separator. a/c, a/bc are valid formats while /abc, abc/, a/, /a, a are not valid.

clipboard.readBuffer(format) Experimental

  • format String

Returns Buffer - Reads format type from the clipboard.

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

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

Writes the buffer into the clipboard as format.

const { clipboard } = require('electron')

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

clipboard.write(data[, type])

  • data Object
    • text String (opcional)
    • html String (opcional)
    • image NativeImage (opcional)
    • rtf String (opcional)
    • bookmark String (optional) - The title of the URL at text.
  • type String (optional) - Can be selection or clipboard; default is 'clipboard'. selection is only available on Linux.

Escreve data na área de transferência.

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