Electron in Practice: Clipboard
Preface
Electron provides operations for reading from and writing to the system clipboard.
See: https://www.electronjs.org/docs/latest/api/clipboard
Process
clipboard is supported in the main process and in renderer processes without sandbox enabled.
const { clipboard } = require("electron");
System
Most clipboard APIs support Windows, Mac, and Linux.
On Linux, there is a selection clipboard.
You can use the type parameter to access the selection clipboard content.
// default
clipboard.readText();
// Linux selection clipboard
clipboard.readText("selection");
API
The read/write related APIs in clipboard are as follows
// text
clipboard.readText();
clipboard.writeText(text);
// html
clipboard.readHTML();
clipboard.writeHTML(markup);
// image
clipboard.readImage();
clipboard.writeImage(image);
// rtf
clipboard.readRTF();
clipboard.writeRTF(text);
// bookmark, Windows and Mac only
clipboard.readBookmark();
clipboard.writeBookmark(title, url);
// find text, Mac only
clipboard.readFindText();
clipboard.writeFindText(text);
Other APIs
// Clear clipboard
clipboard.clear();
// Get supported clipboard formats, e.g.: [ 'text/plain', 'text/html' ]
clipboard.availableFormats();
sandbox
As mentioned above, clipboard is not supported in renderer processes with sandbox disabled.
So how do you operate the clipboard in a renderer process without sandbox enabled?
Via IPC
One approach is to use IPC between the renderer process and the main process to operate the clipboard.
In the renderer process
// preload.js
const { ipcRenderer } = require("electron");
ipcRenderer.send("clipboard-write-text", text);
In the main process
// main.js
const { ipcMain, clipboard } = require("electron");
ipcMain.on("clipboard-write-text", (_, text) => {
clipboard.writeText(text);
});
Via browser clipboard API
Browsers support navigator.clipboard.
See: https://developer.mozilla.org/zh-CN/docs/Web/API/Clipboard_API
In the renderer process
navigator.clipboard.readText().then((clipText) => {
console.log(clipText);
});
Comparison
1. The IPC approach is transparent to the user, but may cause lag if the clipboard content is large.
2. The navigator.clipboard approach triggers a security authorization prompt, requiring user permission before use.
Related Articles
Electron in Practice: Local Logging
Preface: After developing a local app with Electron, local logging is essential. It helps capture user actions and locate bugs. Local logs differ from real-time analytics — they are larger, more detailed, and not all information is suitable for reporting. Similarities and Differences with Node.js Logging: Writing local logs in Electron is similar to Node.js.
Electron in Practice: Registering Shortcuts
Preface: This article introduces how to register keyboard shortcuts in an Electron app. Main Process: Registering shortcuts in the main process is straightforward — just make sure to do it after app ready. Common key mappings: https://www.electronjs.org/docs/latest/api/accelerator. Also remember to unregister all shortcuts when the app quits.
Electron in Practice: Update Strategies
Preface: After developing a desktop app with Electron, the next question is how to update subsequent versions. This article introduces several update strategies. Electron Architecture: An Electron desktop app consists of several parts: the Electron runtime itself, the main process (Node.js and non-Node.js parts), and the renderer process.
Electron in Practice: Customizing Mac Menus
Preface: Previous articles covered developing an Electron app through to Mac packaging. Related articles: Learn Electron in One Article, Electron in Practice: Local Database SQLite, Electron in Practice: Managing Electron Projects with Monorepo, Electron in Practice: Mac Packaging, Electron in Practice: Registering an Apple Developer Account.
Electron in Practice: Package Size Optimization
Preface The previous articles covered developing an Electron app through to Mac packaging. Related articles: Learn Electron in One Article, Electron in Practice: Local Database SQLite, Electron in Practice: Managing Electron Projects with Monorepo, Electron in Practice: Mac Packaging, Electron in Practice: Register