当前位置:网站首页>Modules that can be used by both the electron main process and the rendering process
Modules that can be used by both the electron main process and the rendering process
2022-07-06 22:56:00 【Follow the road to the end】
shell modular
index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title></title>
<style>
* {
margin: 0;
padding: 0;
}
</style>
</head>
<body>
<h2>shell And iframe</h2>
<a id="openUrl" href="https://kaiwu.lagou.com/"> open URL</a>
<br><br>
<button id="openFolder"> Open Directory </button>
<script src="index.js"></script>
</body>
</html>index.js
const { shell } = require('electron')
const path = require('path')
window.addEventListener('DOMContentLoaded', () => {
const oBtn1 = document.getElementById('openUrl')
const oBtn2 = document.getElementById('openFolder')
oBtn1.addEventListener('click', (e) => {
e.preventDefault()
const urlPath = oBtn1.getAttribute('href')
shell.openExternal(urlPath) // Open the link using the default browser
})
oBtn2.addEventListener('click', () => {
shell.showItemInFolder(path.resolve(__filename))
})
})
iframe
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title></title>
<style>
* {
margin: 0;
padding: 0;
}
iframe {
top: 0;
left: 0;
width: 100%;
height: 100%;
position: fixed;
}
</style>
</head>
<body>
<h2>shell And iframe</h2>
<iframe src="https://kaiwu.lagou.com/" frameborder="0" id="webview"></iframe>
<script src="index.js"></script>
</body>
</html>Customize menu usage shell
main.js
const { app, BrowserWindow, Menu, shell } = require('electron')
console.log(process.platform)
// Define global variables to store in the main window id
let mainWinId = null
// create a window
function createWindow () {
console.log('ready')
// Create the main process
const mainWin = new BrowserWindow({
title: ' Custom menu ',
show: false, // true: Display Form ,false: Don't show forms
width: 800,
height: 400,
webPreferences: {
nodeIntegration: true, // Allow browser environments to use Node API
enableRemoteModule: true, // Allow pages to use remote
}
})
let tmp = [
{
label: ' menu ',
submenu: [
{
label: ' About ',
click() {
shell.openExternal('https://kaiwu.lagou.com/')
}
},
{
label: ' open ',
click() {
BrowserWindow.getFocusedWindow().webContents.send('openUrl')
}
},
]
}
]
let menu = Menu.buildFromTemplate(tmp)
Menu.setApplicationMenu(menu)
// Load the specified interface in the current window and let it display the specific content
mainWin.loadFile('index.html')
mainWinId = mainWin.id
mainWin.on('ready-to-show', () => {
mainWin.show() // After the form is fully loaded , Display Form , Avoid white pages
})
mainWin.on('close', () => {
console.log('close')
//mainWin = null
})
}
// When app After starting , Perform window creation and other operations
app.on('ready', createWindow)
app.on('window-all-closed', () => {
console.log('window-all-closed')
app.quit()
})
Notice of news
index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title></title>
</head>
<body>
<h2> be based on H5 Implement message notification </h2>
<button id="btn"> Trigger message </button>
<script src="index.js"></script>
</body>
</html>index.js
window.addEventListener('DOMContentLoaded', () => {
const oBtn = document.getElementById('btn')
oBtn.addEventListener('click', () => {
const option = {
title: ' Notification message 123',
body: ' The message content abc',
icon: './msg.png',
}
const myNotification = new window.Notification(option.title ,option)
myNotification.onclick = function() {
console.log(' Click on the message page card ')
}
})
})
1

Shortcut key registration
main.js
const { app, BrowserWindow, globalShortcut } = require('electron')
console.log(process.platform)
// Define global variables to store in the main window id
let mainWinId = null
// create a window
function createWindow () {
console.log('ready')
// Create the main process
const mainWin = new BrowserWindow({
title: ' Custom menu ',
show: false, // true: Display Form ,false: Don't show forms
width: 800,
height: 400,
webPreferences: {
nodeIntegration: true, // Allow browser environments to use Node API
enableRemoteModule: true, // Allow pages to use remote
}
})
// Load the specified interface in the current window and let it display the specific content
mainWin.loadFile('index.html')
mainWinId = mainWin.id
mainWin.on('ready-to-show', () => {
mainWin.show() // After the form is fully loaded , Display Form , Avoid white pages
})
mainWin.on('close', () => {
console.log('close')
//mainWin = null
})
}
// When app After starting , Perform window creation and other operations
app.on('ready', createWindow)
// Register shortcuts
app.on('ready', () => {
const ret = globalShortcut.register('ctrl + q', () => {
console.log(' Shortcut key registration ')
})
if (!ret) {
console.log(' Registration failed ')
}
console.log(globalShortcut.isRegistered('ctrl + q'))
console.log(ret)
})
app.on('will-quit', () => {
// Shortcut key to cancel registration
globalShortcut.unregister('ctrl + q')
// Cancel all registered shortcuts
globalShortcut.registerAll()
})
app.on('window-all-closed', () => {
console.log('window-all-closed')
app.quit()
})
Shear board module
index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title></title>
</head>
<body>
<h2> Clippings </h2>
<input type="text" placeholder=" Enter the content to be copied "> <button> Copy </button>
<br><br>
<input type="text" placeholder=" Paste the content here "> <button> Paste </button>
<br><br>
<button id='clipImg'> Copy the picture to the cutout and paste it into the interface </button>
<br><br>
<script src="index.js"></script>
</body>
</html>index.js
const { clipboard, nativeImage } = require('electron')
window.addEventListener('DOMContentLoaded', () => {
const aBtn = document.getElementsByTagName('button')
const aInput = document.getElementsByTagName('input')
const oBtn = document.getElementById('clipImg')
let ret = null
aBtn[0].addEventListener('click', () => {
// Copy content to clipboard
ret = clipboard.writeText(aInput[0].value)
})
aBtn[1].addEventListener('click', () => {
// Paste the contents of the clipboard
aInput[1].value = clipboard.readText(ret)
})
oBtn.addEventListener('click', () => {
// When placing the picture in the clipboard, it is required that the picture type belongs to nativeImage example
const oImage = nativeImage.createFromPath('./msg.png')
clipboard.writeImage(oImage)
// Make the picture in the clipboard as DOM Elements are displayed on the interface
const oImg = clipboard.readImage()
const oImgDom = new Image()
oImgDom.src = oImg.toDataURL()
document.body.append(oImgDom)
})
})
1
边栏推荐
- TypeScript获取函数参数类型
- POJ 1258 Agri-Net
- Config:invalid signature solution and troubleshooting details
- POJ 1094 sorting it all out
- Aardio - does not declare the method of directly passing float values
- Case recommendation: An Qing works with partners to ensure that the "smart court" is more efficient
- The statement that allows full table scanning does not seem to take effect set odps sql. allow. fullscan=true; I
- OpenSSL: a full-featured toolkit for TLS and SSL protocols, and a general encryption library
- Dayu200 experience officer runs the intelligent drying system page based on arkui ETS on dayu200
- 同构+跨端,懂得小程序+kbone+finclip就够了!
猜你喜欢

Thinkphp5 multi table associative query method join queries two database tables, and the query results are spliced and returned

#DAYU200体验官# 在DAYU200运行基于ArkUI-eTS的智能晾晒系统页面

Aardio - integrate variable values into a string of text through variable names

Case recommendation: An Qing works with partners to ensure that the "smart court" is more efficient
DockerMySQL无法被宿主机访问的问题解决

Slide the uniapp to a certain height and fix an element to the top effect demo (organize)
The problem that dockermysql cannot be accessed by the host machine is solved

Machine test question 1

Rust knowledge mind map XMIND

European Bioinformatics Institute 2021 highlights report released: nearly 1million proteins have been predicted by alphafold
随机推荐
Matlab tips (27) grey prediction
mysql查看表结构的三种方法总结
MySQL实现字段分割一行转多行的示例代码
【全网首发】Redis系列3:高可用之主从架构的
Traversal of a tree in first order, middle order, and then order
CUDA exploration
Redis 持久化机制
Jafka来源分析——Processor
three.js绚烂的气泡效果
欧洲生物信息研究所2021亮点报告发布:采用AlphaFold已预测出近1百万个蛋白质
Improving Multimodal Accuracy Through Modality Pre-training and Attention
Rust knowledge mind map XMIND
Volatile keyword
Comparison between variable and "zero value"
Flutter life cycle
Balanced Multimodal Learning via On-the-fly Gradient Modulation(CVPR2022 oral)
Detailed explanation of ThreadLocal
OpenNMS separation database
#DAYU200体验官# 首页aito视频&Canvas绘制仪表盘(ets)
允许全表扫描 那个语句好像不生效set odps.sql.allow.fullscan=true;我