docs: update clipboard fiddles (#36946)
This commit is contained in:
parent
55c818d0a8
commit
4e4ae9ff53
10 changed files with 55 additions and 20 deletions
|
@ -2,7 +2,7 @@
|
||||||
|
|
||||||
> Perform copy and paste operations on the system clipboard.
|
> Perform copy and paste operations on the system clipboard.
|
||||||
|
|
||||||
Process: [Main](../glossary.md#main-process), [Renderer](../glossary.md#renderer-process)
|
Process: [Main](../glossary.md#main-process), [Renderer](../glossary.md#renderer-process) (non-sandboxed only)
|
||||||
|
|
||||||
On Linux, there is also a `selection` clipboard. To manipulate it
|
On Linux, there is also a `selection` clipboard. To manipulate it
|
||||||
you need to pass `selection` to each method:
|
you need to pass `selection` to each method:
|
||||||
|
|
|
@ -2,12 +2,13 @@
|
||||||
<html>
|
<html>
|
||||||
<head>
|
<head>
|
||||||
<meta charset="UTF-8">
|
<meta charset="UTF-8">
|
||||||
|
<meta http-equiv="Content-Security-Policy" content="script-src 'self' 'unsafe-inline';" />
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
<div>
|
<div>
|
||||||
<div>
|
<div>
|
||||||
<h1>Clipboard copy</h1>
|
<h1>Clipboard copy</h1>
|
||||||
<i>Supports: Win, macOS, Linux <span>|</span> Process: Both</i>
|
<i>Supports: Win, macOS, Linux <span>|</span> Process: Main, Renderer (non-sandboxed only)</i>
|
||||||
<div>
|
<div>
|
||||||
<div>
|
<div>
|
||||||
<button id="copy-to">Copy</button>
|
<button id="copy-to">Copy</button>
|
||||||
|
@ -17,8 +18,6 @@
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
<script src="./renderer.js"></script>
|
||||||
</body>
|
</body>
|
||||||
<script>
|
|
||||||
require('./renderer.js')
|
|
||||||
</script>
|
|
||||||
</html>
|
</html>
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
const { app, BrowserWindow } = require('electron')
|
const { app, BrowserWindow, ipcMain, clipboard } = require('electron')
|
||||||
|
const path = require('path')
|
||||||
|
|
||||||
let mainWindow = null
|
let mainWindow = null
|
||||||
|
|
||||||
|
@ -8,7 +9,7 @@ function createWindow () {
|
||||||
height: 400,
|
height: 400,
|
||||||
title: 'Clipboard copy',
|
title: 'Clipboard copy',
|
||||||
webPreferences: {
|
webPreferences: {
|
||||||
nodeIntegration: true
|
preload: path.join(__dirname, 'preload.js')
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -20,6 +21,18 @@ function createWindow () {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ipcMain.handle('clipboard:writeText', (event, text) => {
|
||||||
|
clipboard.writeText(text)
|
||||||
|
})
|
||||||
|
|
||||||
app.whenReady().then(() => {
|
app.whenReady().then(() => {
|
||||||
createWindow()
|
createWindow()
|
||||||
|
|
||||||
|
app.on('activate', function () {
|
||||||
|
if (BrowserWindow.getAllWindows().length === 0) createWindow()
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
app.on('window-all-closed', function () {
|
||||||
|
if (process.platform !== 'darwin') app.quit()
|
||||||
})
|
})
|
||||||
|
|
5
docs/fiddles/system/clipboard/copy/preload.js
Normal file
5
docs/fiddles/system/clipboard/copy/preload.js
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
const { contextBridge, ipcRenderer } = require('electron')
|
||||||
|
|
||||||
|
contextBridge.exposeInMainWorld('clipboard', {
|
||||||
|
writeText: (text) => ipcRenderer.invoke('clipboard:writeText', text)
|
||||||
|
})
|
|
@ -1,5 +1,3 @@
|
||||||
const { clipboard } = require('electron')
|
|
||||||
|
|
||||||
const copyBtn = document.getElementById('copy-to')
|
const copyBtn = document.getElementById('copy-to')
|
||||||
const copyInput = document.getElementById('copy-to-input')
|
const copyInput = document.getElementById('copy-to-input')
|
||||||
|
|
||||||
|
|
|
@ -2,12 +2,13 @@
|
||||||
<html>
|
<html>
|
||||||
<head>
|
<head>
|
||||||
<meta charset="UTF-8">
|
<meta charset="UTF-8">
|
||||||
|
<meta http-equiv="Content-Security-Policy" content="script-src 'self' 'unsafe-inline';" />
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
<div>
|
<div>
|
||||||
<div>
|
<div>
|
||||||
<h1>Clipboard paste</h1>
|
<h1>Clipboard paste</h1>
|
||||||
<i>Supports: Win, macOS, Linux <span>|</span> Process: Both</i>
|
<i>Supports: Win, macOS, Linux <span>|</span> Process: Main, Renderer (non-sandboxed only)</i>
|
||||||
<div>
|
<div>
|
||||||
<div>
|
<div>
|
||||||
<button id="paste-to">Paste</button>
|
<button id="paste-to">Paste</button>
|
||||||
|
@ -17,8 +18,6 @@
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
<script src="./renderer.js"></script>
|
||||||
</body>
|
</body>
|
||||||
<script>
|
|
||||||
require('./renderer.js')
|
|
||||||
</script>
|
|
||||||
</html>
|
</html>
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
const { app, BrowserWindow } = require('electron')
|
const { app, BrowserWindow, ipcMain, clipboard } = require('electron')
|
||||||
|
const path = require('path')
|
||||||
|
|
||||||
let mainWindow = null
|
let mainWindow = null
|
||||||
|
|
||||||
|
@ -8,7 +9,7 @@ function createWindow () {
|
||||||
height: 400,
|
height: 400,
|
||||||
title: 'Clipboard paste',
|
title: 'Clipboard paste',
|
||||||
webPreferences: {
|
webPreferences: {
|
||||||
nodeIntegration: true
|
preload: path.join(__dirname, 'preload.js')
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -20,6 +21,22 @@ function createWindow () {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ipcMain.handle('clipboard:readText', () => {
|
||||||
|
return clipboard.readText()
|
||||||
|
})
|
||||||
|
|
||||||
|
ipcMain.handle('clipboard:writeText', (event, text) => {
|
||||||
|
clipboard.writeText(text)
|
||||||
|
})
|
||||||
|
|
||||||
app.whenReady().then(() => {
|
app.whenReady().then(() => {
|
||||||
createWindow()
|
createWindow()
|
||||||
|
|
||||||
|
app.on('activate', function () {
|
||||||
|
if (BrowserWindow.getAllWindows().length === 0) createWindow()
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
app.on('window-all-closed', function () {
|
||||||
|
if (process.platform !== 'darwin') app.quit()
|
||||||
})
|
})
|
||||||
|
|
6
docs/fiddles/system/clipboard/paste/preload.js
Normal file
6
docs/fiddles/system/clipboard/paste/preload.js
Normal file
|
@ -0,0 +1,6 @@
|
||||||
|
const { contextBridge, ipcRenderer } = require('electron')
|
||||||
|
|
||||||
|
contextBridge.exposeInMainWorld('clipboard', {
|
||||||
|
readText: () => ipcRenderer.invoke('clipboard:readText'),
|
||||||
|
writeText: (text) => ipcRenderer.invoke('clipboard:writeText', text)
|
||||||
|
})
|
|
@ -1,9 +1,7 @@
|
||||||
const { clipboard } = require('electron')
|
|
||||||
|
|
||||||
const pasteBtn = document.getElementById('paste-to')
|
const pasteBtn = document.getElementById('paste-to')
|
||||||
|
|
||||||
pasteBtn.addEventListener('click', () => {
|
pasteBtn.addEventListener('click', async () => {
|
||||||
clipboard.writeText('What a demo!')
|
await clipboard.writeText('What a demo!')
|
||||||
const message = `Clipboard contents: ${clipboard.readText()}`
|
const message = `Clipboard contents: ${await clipboard.readText()}`
|
||||||
document.getElementById('paste-from').innerHTML = message
|
document.getElementById('paste-from').innerHTML = message
|
||||||
})
|
})
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue