講者:土豆
時間:2020/03/21
Any application that cat be written in JavaScript, will eventually be written in JavaScript.
(Stack Overflow 創辦人)
npx create-electron-app electron_clipboard
npx:使用完即刪除,不佔空間
npm install -g create-electron-app # 可能需要加sudo
npx create-electron-app electron_clipboard
如果上面指令不行,試試下面的
需要先裝好 node 以及 git
const { app, BrowserWindow } = require('electron');
const path = require('path');
// Handle creating/removing shortcuts on Windows when installing/uninstalling.
if (require('electron-squirrel-startup')) { // eslint-disable-line global-require
app.quit();
}
const createWindow = () => {
// Create the browser window.
const mainWindow = new BrowserWindow({
width: 800,
height: 600,
// 加入下面三行開啟在browser端import module的功能
webPreferences: {
contextIsolation: false,
nodeIntegration: true
}
});
// and load the index.html of the app.
mainWindow.loadFile(path.join(__dirname, 'index.html'));
// // Open the DevTools.
// mainWindow.webContents.openDevTools();
};
// This method will be called when Electron has finished
// initialization and is ready to create browser windows.
// Some APIs can only be used after this event occurs.
app.on('ready', createWindow);
// Quit when all windows are closed, except on macOS. There, it's common
// for applications and their menu bar to stay active until the user quits
// explicitly with Cmd + Q.
app.on('window-all-closed', () => {
if (process.platform !== 'darwin') {
app.quit();
}
});
app.on('activate', () => {
// On OS X it's common to re-create a window in the app when the
// dock icon is clicked and there are no other windows open.
if (BrowserWindow.getAllWindows().length === 0) {
createWindow();
}
});
// In this file you can include the rest of your app's specific main process
// code. You can also put them in separate files and import them here.
請修改成以下程式碼
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Hello World!</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.0-beta2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-BmbxuPwQa2lc/FVzBcNJ7UAyJxM6wuqIj61tLrc4wSX0szH/Ev+nYRRuWlolflfl" crossorigin="anonymous">
<link rel="stylesheet" href="index.css">
<script defer src="render.js"></script>
</head>
<body>
<h1>剪貼簿</h1>
<button id="pasteBtn" class="btn btn-primary">貼入剪貼簿</button>
<div id="textList"></div>
</body>
</html>
請修改成以下html
const { clipboard } = require('electron');
const textList = document.getElementById('textList');
const pasteBtn = document.getElementById('pasteBtn');
let itemId = 0;
function copyTextToClipboard(itemId) {
let text = document.getElementById(itemId).getElementsByTagName('span')[0].textContent;
clipboard.writeText(text);
}
function removeTextFromPage(itemId) {
document.getElementById(itemId).remove();
}
function pastCliboarTextToPage() {
let textFromClipboard = clipboard.readText();
textList.innerHTML += '<div id="' + itemId + '"class="m-3"><span class="mx-2">' +
textFromClipboard +
'</span><button class="btn btn-sm btn-success" onclick="copyTextToClipboard(' + itemId + ')">複製</button>' +
'<button class="btn btn-sm btn-danger" onclick="removeTextFromPage(' + itemId + ')">刪除</button>' +
'</div>';
itemId += 1;
}
pasteBtn.addEventListener('click', () => {
pastCliboarTextToPage();
});
請新增檔案並新增以下程式碼