From 4ba2b730de174e4db2fe15dabc4e640a2c082c45 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Timeless=E5=B0=8F=E5=B8=85?= <1250076672@qq.com> Date: Thu, 21 May 2026 00:12:44 +0800 Subject: [PATCH] init --- .gitignore | 3 +++ .npmrc | 18 +++++++++++++ index.html | 75 ++++++++++++++++++++++++++++++++++++++++++++++++++++ main.js | 34 ++++++++++++++++++++++++ package.json | 18 +++++++++++++ preload.js | 7 +++++ renderer.js | 16 +++++++++++ 7 files changed, 171 insertions(+) create mode 100644 .gitignore create mode 100644 .npmrc create mode 100644 index.html create mode 100644 main.js create mode 100644 package.json create mode 100644 preload.js create mode 100644 renderer.js diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..1421050 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +package-lock.json +node_modules +.idea diff --git a/.npmrc b/.npmrc new file mode 100644 index 0000000..4708f6c --- /dev/null +++ b/.npmrc @@ -0,0 +1,18 @@ +# 全局npm镜像 +registry=https://registry.npmmirror.com + +# electron 镜像 +electron_mirror=https://npmmirror.com/mirrors/electron/ +electron_builder_binaries_mirror=https://npmmirror.com/mirrors/electron-builder-binaries/ + +# node-sass 镜像 +sass_binary_site=https://npmmirror.com/mirrors/node-sass/ + +# puppeteer 镜像 +puppeteer_download_host=https://npmmirror.com/mirrors + +# sharp 镜像 +sharp_dist_base_url=https://npmmirror.com/mirrors/sharp-libvips/ + +# 禁止严格ssl +strict-ssl=false \ No newline at end of file diff --git a/index.html b/index.html new file mode 100644 index 0000000..06a7e59 --- /dev/null +++ b/index.html @@ -0,0 +1,75 @@ + + + + + + 实时计数器 + + + +
+

实时计数器

+
0
+
+ 运行中 +
+
+ + + \ No newline at end of file diff --git a/main.js b/main.js new file mode 100644 index 0000000..aa82437 --- /dev/null +++ b/main.js @@ -0,0 +1,34 @@ +const { app, BrowserWindow } = require('electron'); +const path = require('path'); + +let win; + +function createWindow() { + win = new BrowserWindow({ + width: 500, + height: 400, + webPreferences: { + nodeIntegration: false, + contextIsolation: true, + preload: path.join(__dirname, 'preload.js') + } + }); + + win.loadFile('index.html'); + // 打开开发者工具方便调试 + // win.webContents.openDevTools(); +} + +app.whenReady().then(createWindow); + +app.on('window-all-closed', () => { + if (process.platform !== 'darwin') { + app.quit(); + } +}); + +app.on('activate', () => { + if (BrowserWindow.getAllWindows().length === 0) { + createWindow(); + } +}); \ No newline at end of file diff --git a/package.json b/package.json new file mode 100644 index 0000000..ca83c5a --- /dev/null +++ b/package.json @@ -0,0 +1,18 @@ +{ + "name": "electron-counter", + "version": "1.0.0", + "description": "Electron app with live counter", + "main": "main.js", + "scripts": { + "start": "electron ." + }, + "devDependencies": { + "electron": "^28.3.3" + }, + "build": { + "appId": "com.example.electron-counter", + "win": { + "target": "nsis" + } + } +} diff --git a/preload.js b/preload.js new file mode 100644 index 0000000..fa7eed2 --- /dev/null +++ b/preload.js @@ -0,0 +1,7 @@ +const { contextBridge, ipcRenderer } = require('electron'); + +// 暴露安全的 API 给渲染进程 +contextBridge.exposeInMainWorld('electronAPI', { + // 可扩展更多功能 + platform: process.platform +}); \ No newline at end of file diff --git a/renderer.js b/renderer.js new file mode 100644 index 0000000..24fac08 --- /dev/null +++ b/renderer.js @@ -0,0 +1,16 @@ +// 死循环计数器,每秒增加并刷新 UI +let count = 0; +const counterElement = document.getElementById('counter'); + +function updateCounter() { + count++; + counterElement.textContent = count.toLocaleString(); + + // 立即安排下一次更新 + setTimeout(updateCounter, 1000); +} + +// 启动计数器 +updateCounter(); + +console.log('计数器已启动'); \ No newline at end of file