剪貼簿
在系統剪貼簿上執行複製和貼上操作。
在 Linux 上,還有一個 selection
剪貼簿。要操作它,您需要將 selection
傳遞給每個方法
const { clipboard } = require('electron')
clipboard.writeText('Example string', 'selection')
console.log(clipboard.readText('selection'))
方法
clipboard
模組具有以下方法
注意: 實驗性 API 會標記為實驗性,並且可能會在未來移除。
clipboard.readText([type])
type
字串 (選填) - 可以是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])
text
字串type
字串 (選填) - 可以是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])
type
字串 (選填) - 可以是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])
markup
字串type
字串 (選填) - 可以是selection
或clipboard
;預設為 'clipboard'。selection
僅適用於 Linux。
將 markup
寫入剪貼簿。
const { clipboard } = require('electron')
clipboard.writeHTML('<b>Hi</b>')
clipboard.readImage([type])
type
字串 (選填) - 可以是selection
或clipboard
;預設為 'clipboard'。selection
僅適用於 Linux。
返回 NativeImage
- 剪貼簿中的影像內容。
clipboard.writeImage(image[, type])
image
NativeImagetype
字串 (選填) - 可以是selection
或clipboard
;預設為 'clipboard'。selection
僅適用於 Linux。
將 image
寫入剪貼簿。
clipboard.readRTF([type])
type
字串 (選填) - 可以是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])
text
字串type
字串 (選填) - 可以是selection
或clipboard
;預設為 'clipboard'。selection
僅適用於 Linux。
將 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
返回 Object
title
字串url
字串
返回一個包含 title
和 url
鍵的物件,表示剪貼簿中的書籤。當書籤不可用時,title
和 url
值將為空字串。title
值在 Windows 上將始終為空。
clipboard.writeBookmark(title, url[, type])
macOS Windows
title
字串 - 在 Windows 上未使用url
字串type
字串 (選填) - 可以是selection
或clipboard
;預設為 'clipboard'。selection
僅適用於 Linux。
將 title
(僅限 macOS) 和 url
作為書籤寫入剪貼簿。
注意: Windows 上的大多數應用程式不支援將書籤貼到其中,因此您可以使用 clipboard.write
將書籤和備用文字都寫入剪貼簿。
const { clipboard } = require('electron')
clipboard.writeBookmark('Electron Homepage', 'https://electron.dev.org.tw')
clipboard.readFindText()
macOS
返回 string
- 尋找剪貼簿上的文字,該剪貼簿保留了目前作用中應用程式尋找面板的目前狀態資訊。
從渲染器進程呼叫時,此方法使用同步 IPC。每當應用程式被啟動時,都會從尋找剪貼簿中重新讀取快取值。
clipboard.writeFindText(text)
macOS
text
字串
將 text
以純文字的形式寫入尋找剪貼簿 (該剪貼簿保留了目前作用中應用程式尋找面板的目前狀態資訊)。從渲染器進程呼叫時,此方法使用同步 IPC。
clipboard.clear([type])
type
字串 (選填) - 可以是selection
或clipboard
;預設為 'clipboard'。selection
僅適用於 Linux。
清除剪貼簿內容。
clipboard.availableFormats([type])
type
字串 (選填) - 可以是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])
實驗性
format
字串type
字串 (選填) - 可以是selection
或clipboard
;預設為 'clipboard'。selection
僅適用於 Linux。
返回 boolean
- 剪貼簿是否支援指定的 format
。
const { clipboard } = require('electron')
const hasFormat = clipboard.has('public/utf8-plain-text')
console.log(hasFormat)
// 'true' or 'false'
clipboard.read(format)
實驗性
format
字串
返回 string
- 從剪貼簿讀取 format
類型。
format
應包含有效的 ASCII 字元,並具有 /
分隔符。a/c
、a/bc
是有效的格式,而 /abc
、abc/
、a/
、/a
、a
則不是有效的格式。
clipboard.readBuffer(format)
實驗性
format
字串
返回 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])
實驗性
format
字串buffer
Buffertype
字串 (選填) - 可以是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])
data
物件text
字串 (選填)html
字串 (選填)image
NativeImage (選填)rtf
字串 (選填)bookmark
字串 (選填) -text
中 URL 的標題。
type
字串 (選填) - 可以是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' }