Contents

System Clipboard

How to access the system clipboard to read and write data.

The system clipboard is a temporary storage area used by most operating systems to transfer data between applications, via copy and paste operations. The clipboard is usually temporary and unnamed, and its contents reside in the computer’s RAM. The clipboard is sometimes called the paste buffer.

The framework allows you to programmatically read and write text and other kinds of data to and from the system clipboard. The system clipboard can contain multiple data types at the same time.

Writing to the clipboard 

You can write different kinds of data to the system clipboard.

The following example shows how to write plain text to the clipboard:

import { clipboard } from '@mobrowser/api';

clipboard.write('text/plain', 'Hello, world!')
clipboard.write('text/html', '<h1>Hello, world!</h1>')

Reading from the clipboard 

To read all available data types from the clipboard, use the following approach:

const types = clipboard.getTypes()
for (const type of types) {
  const data = clipboard.read(type)
  console.log('Clipboard type: ', type, 'data: ', data)
}

You can check the data type and read only the data you need. For example, the following code shows how to read only plain text from the clipboard:

if (clipboard.has('text/plain')) {
  const text = clipboard.read('text/plain')
  console.log('Clipboard text: ', text)
}

Clearing the clipboard 

You can clear the system clipboard content using the following approach:

clipboard.clear()

If the system clipboard is already empty, then this method does nothing.