Skip to content

Commit

Permalink
第一次提交
Browse files Browse the repository at this point in the history
  • Loading branch information
cyamoyed committed Aug 28, 2023
0 parents commit 48ed38f
Show file tree
Hide file tree
Showing 10 changed files with 13,962 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.idea
node_modules
out
88 changes: 88 additions & 0 deletions electron/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
// main.js
const {
app,
BrowserWindow,
Menu,
ipcMain
} = require('electron');
const path = require('path');
const {
exec
} = require('child_process');
const sudo = require('sudo-prompt');

const NODE_ENV = process.env.NODE_ENV

function createWindow() {
const win = new BrowserWindow({
width: 300,
height: 300,
webPreferences: {
nodeIntegration: true,
preload: path.join(__dirname, 'preload.js')
}
});

win.loadFile('index.html');
// 关闭菜单
Menu.setApplicationMenu(null);
// 打开开发工具
if (NODE_ENV === "development") {
win.webContents.openDevTools();
}
}


function executeCommand(command) {
return new Promise((resolve, reject) => {
sudo.exec(command, (error, stdout, stderr) => {
if (error) {
console.error(`exec error: ${error}`);
reject(error);
}
resolve(stdout);
console.log(`stdout: ${stdout}`);
console.error(`stderr: ${stderr}`);
});
});
}

function startTimeService() {
const command = 'net start w32time';
return executeCommand(command);
}

app.whenReady().then(() => {
ipcMain.handle('set-time', async (event, time) => {
try {
let command;
if (time === 'now') {
command = 'w32tm /resync';
} else {
command = `date ${time}`;
}
return await executeCommand(command);
} catch (error) {
if (time === 'now') {
try {
await startTimeService();
return await executeCommand('w32tm /resync');
} catch (e) {
throw e;
}
} else {
throw error;
}
}
});

createWindow();

app.on('activate', function () {
if (BrowserWindow.getAllWindows().length === 0) createWindow();
});
});

app.on('window-all-closed', function () {
if (process.platform !== 'darwin') app.quit();
});
21 changes: 21 additions & 0 deletions electron/preload.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
const {
contextBridge,
ipcRenderer
} = require('electron')

window.addEventListener('DOMContentLoaded', () => {
const replaceText = (selector, text) => {
const element = document.getElementById(selector)
if (element) element.innerText = text
}

for (const dependency of ['chrome', 'node', 'electron']) {
replaceText(`${dependency}-version`, process.versions[dependency])
}
})

contextBridge.exposeInMainWorld('electronAPI', {
setTime: (time) => {
return ipcRenderer.invoke('set-time', time)
}
})
30 changes: 30 additions & 0 deletions forge.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
module.exports = {
packagerConfig: {
asar: true,
},
rebuildConfig: {},
makers: [
{
name: '@electron-forge/maker-squirrel',
config: {},
},
{
name: '@electron-forge/maker-zip',
platforms: ['darwin'],
},
{
name: '@electron-forge/maker-deb',
config: {},
},
{
name: '@electron-forge/maker-rpm',
config: {},
},
],
plugins: [
{
name: '@electron-forge/plugin-auto-unpack-natives',
config: {},
},
],
};
60 changes: 60 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<!-- index.html -->
<!DOCTYPE html>
<html>

<head>
<meta charset="UTF-8">
<title>Set System Time</title>
<style>
body {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
height: 100vh;
margin: 0;
font-family: Arial, sans-serif;
}

h1 {
margin-bottom: 20px;
}

.input_div {
margin-bottom: 20px;
}

.btn_div {
margin-bottom: 20px;
text-align: center;
}

input[type="date"] {
width: 100%;
padding: 5px;
font-size: 16px;
}

button {
padding: 10px 20px;
font-size: 16px;
background-color: #007bff;
color: #fff;
border: none;
cursor: pointer;
}
</style>
</head>

<body>
<h1>设置系统时间</h1>
<div>
<div class="input_div"><input type="date" value="2020-10-29" id="timeInput" /></div>
<div class="btn_div">
<button id="setTimeBtn">设置</button>
<button id="resetTimeBtn">恢复</button></div>
</div>
<script src="./renderer.js"></script>
</body>

</html>
Loading

0 comments on commit 48ed38f

Please sign in to comment.