在上海樂字節(jié)學(xué)習(xí)的三十六天

如何用Web前端技術(shù)就能開發(fā)出桌面應(yīng)用程序曲饱?

Web前端技術(shù)開發(fā)桌面應(yīng)用的方式

  • CEF:用 Chromium&Webkit 來呈現(xiàn) web 頁面,是客戶端里面嵌瀏覽器蕴潦,瀏覽器里面跑網(wǎng)頁奋隶。

  • heX:基于CEF童漩,內(nèi)部整合了開源項(xiàng)目 Chromium 及 node.js荣恐。

  • nw:基于Chromium 和 node.js火惊,利用 web 方式開發(fā)跨平臺(tái)桌面應(yīng)用的平臺(tái)技術(shù)求类。

  • electron:底層也是基于Chromium 和 node.js。

  • 等等屹耐。尸疆。。

案例實(shí)操~electron

electron 是 github 開發(fā)的惶岭,用來開發(fā)桌面應(yīng)用的一款前端框架

開發(fā)環(huán)境

安裝 node.js

nodejs下載地址

為了避免網(wǎng)絡(luò)問題對(duì) Node 工作時(shí)的影響寿弱,我們安裝國內(nèi)的 node 鏡像 cnpm。

npm install -g cnpm --registry=https://registry.npm.taobao.org

安裝 electron

npm install --save-dev electron

或者全局安裝

npm install -g electron

開發(fā)工具

VSCode

其實(shí) vscode 就是 electron 開發(fā)的

開發(fā)一個(gè)簡易的 electron

Electron 應(yīng)用使用 JavaScript 開發(fā)按灶,其工作原理和方法與 Node.js 開發(fā)相同症革。 electron 模塊包含了 Electron 提供的所有 API 和功能,引入方法和普通 Node.js 模塊一樣:

<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="javascript" contenteditable="false" cid="n37" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9rem; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(255, 255, 255); position: relative !important; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">const electron = require('electron')</pre>

electron 模塊所提供的功能都是通過命名空間暴露出來的兆衅。 比如說: electron.app負(fù)責(zé)管理 Electron 應(yīng)用程序的生命周期, electron.BrowserWindow類負(fù)責(zé)創(chuàng)建窗口嗜浮。下面是一個(gè)簡單的main.js文件羡亩,它將在應(yīng)用程序準(zhǔn)備就緒后打開一個(gè)窗口:

<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="javascript" contenteditable="false" cid="n40" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9rem; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(255, 255, 255); position: relative !important; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">const { app, BrowserWindow } = require('electron')
?
function createWindow () {
// 創(chuàng)建瀏覽器窗口
let win = new BrowserWindow({
width: 800,
height: 600,
webPreferences: {
nodeIntegration: true
}
})
?
// 加載index.html文件
win.loadFile('index.html')
}
?
app.whenReady().then(createWindow)</pre>

創(chuàng)建你想展示的 index.html

<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="html" contenteditable="false" cid="n42" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9rem; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(255, 255, 255); position: relative !important; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;"><!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Hello World!</title>

<meta http-equiv="Content-Security-Policy" content="script-src 'self' 'unsafe-inline';" />
</head>
<body>
<h1>Hello World!</h1>
We are using node <script>document.write(process.versions.node)</script>,
Chrome <script>document.write(process.versions.chrome)</script>,
and Electron <script>document.write(process.versions.electron)</script>.
</body>
</html></pre>

啟動(dòng)

我們?cè)?code>package.json中已經(jīng)寫好了啟動(dòng)命令,所以這里直接用 node 啟動(dòng)命令npm start就可以了危融,如果沒有配畏铆,也可以用electron .命令啟動(dòng)。

主進(jìn)程和渲染進(jìn)程

我們可以理解package.json中定義的入口文件就是主進(jìn)程吉殃,那一般一個(gè)程序只有一個(gè)主進(jìn)程辞居,而我們可以利用一個(gè)主進(jìn)程,打開多個(gè)子窗口蛋勺。

由于 Electron 使用了 Chromium 來展示 web 頁面瓦灶,所以 Chromium 的多進(jìn)程架構(gòu)也被使用到。每個(gè) Electron 中的 web 頁面運(yùn)行在它自己的渲染進(jìn)程中抱完,也就是我們說的渲染進(jìn)程贼陶。

也就是說主進(jìn)程控制渲染進(jìn)程,一個(gè)主進(jìn)程可以控制多個(gè)渲染進(jìn)程巧娱。

您應(yīng)當(dāng)在 main.js 中創(chuàng)建窗口碉怔,并處理程序中可能遇到的所有系統(tǒng)事件。下面我們將完善上述例子禁添,添加以下功能:打開開發(fā)者工具撮胧、處理窗口關(guān)閉事件、在 macOS 用戶點(diǎn)擊 dock 上圖標(biāo)時(shí)重建窗口老翘,添加后芹啥,main. js 就像下面這樣:

<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="javascript" contenteditable="false" cid="n54" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9rem; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(255, 255, 255); position: relative !important; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">const { app, BrowserWindow } = require('electron')
?
function createWindow () {
// 創(chuàng)建瀏覽器窗口
const win = new BrowserWindow({
width: 800,
height: 600,
webPreferences: {
nodeIntegration: true
}
})
?
// 并且為你的應(yīng)用加載index.html
win.loadFile('index.html')
?
// 打開開發(fā)者工具
win.webContents.openDevTools()
}
?
app.whenReady().then(createWindow)
?
// 當(dāng)所有窗口都關(guān)閉時(shí)觸發(fā)此事件.
app.on('window-all-closed', () => {
// 在 macOS 上锻离,除非用戶用 Cmd + Q 確定地退出,
// 否則絕大部分應(yīng)用及其菜單欄會(huì)保持激活叁征。
if (process.platform !== 'darwin') {
app.quit()
}
})
?
app.on('activate', () => {
// 在macOS上纳账,當(dāng)單擊dock圖標(biāo)并且沒有其他窗口打開時(shí),
// 通常在應(yīng)用程序中重新創(chuàng)建一個(gè)窗口捺疼。
if (BrowserWindow.getAllWindows().length === 0) {
createWindow()
}
})
?
// In this file you can include the rest of your app's specific main process
// code. 也可以拆分成幾個(gè)文件疏虫,然后用 require 導(dǎo)入。
?</pre>

git 上面的 demo 示例

<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="shell" contenteditable="false" cid="n57" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9rem; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(255, 255, 255); position: relative !important; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;"># 克隆這倉庫
git clone https://github.com/electron/electron-quick-start

進(jìn)入倉庫

cd electron-quick-start

安裝依賴庫

npm install

運(yùn)行應(yīng)用

npm start</pre>

electron-forge 構(gòu)建項(xiàng)目

<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="shell" contenteditable="false" cid="n60" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9rem; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(255, 255, 255); position: relative !important; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;"># 全局安裝electron-forge
npm install electron-forge -g
?

用electron-forge初始化一個(gè)項(xiàng)目

electron-forge init demo02
?

進(jìn)入到項(xiàng)目目錄

cd demo02
?

啟動(dòng)項(xiàng)目

npm start</pre>

擴(kuò)展~開發(fā)桌面百度

主進(jìn)程代碼

我們創(chuàng)建了主進(jìn)程對(duì)象 win 后啤呼,讓它直接加載百度的地址卧秘。

<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="js" contenteditable="false" cid="n65" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9rem; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(255, 255, 255); position: relative !important; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">const { app, BrowserWindow } = require('electron')
?
function createWindow () {
// 創(chuàng)建瀏覽器窗口
let win = new BrowserWindow({
width: 800,
height: 800,
webPreferences: {
nodeIntegration: true
}
})
?
// 加載index.html文件
// win.loadFile('index.html')
win.loadURL("http://www.baidu.com")
}
?
app.whenReady().then(createWindow)</pre>

打包發(fā)布

我們希望使用electron-packager對(duì)應(yīng)用進(jìn)行打包發(fā)布,electron-packager的安裝方式如下:

<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="shell" contenteditable="false" cid="n68" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9rem; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(255, 255, 255); position: relative !important; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">#下載electron-packager打包插件
npm install electron-packager -g

開始打包

electron-packager ./ demo01 --win --out ./ --arch=x64 --app-version=0.0.1 --electron-version=8.2.5</pre>

我們還可以進(jìn)一步打包成可執(zhí)行文件

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末官扣,一起剝皮案震驚了整個(gè)濱河市翅敌,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌惕蹄,老刑警劉巖蚯涮,帶你破解...
    沈念sama閱讀 222,252評(píng)論 6 516
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異卖陵,居然都是意外死亡遭顶,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,886評(píng)論 3 399
  • 文/潘曉璐 我一進(jìn)店門泪蔫,熙熙樓的掌柜王于貴愁眉苦臉地迎上來棒旗,“玉大人,你說我怎么就攤上這事撩荣∠橙啵” “怎么了?”我有些...
    開封第一講書人閱讀 168,814評(píng)論 0 361
  • 文/不壞的土叔 我叫張陵餐曹,是天一觀的道長逛拱。 經(jīng)常有香客問我,道長台猴,這世上最難降的妖魔是什么橘券? 我笑而不...
    開封第一講書人閱讀 59,869評(píng)論 1 299
  • 正文 為了忘掉前任,我火速辦了婚禮卿吐,結(jié)果婚禮上旁舰,老公的妹妹穿的比我還像新娘。我一直安慰自己嗡官,他們只是感情好箭窜,可當(dāng)我...
    茶點(diǎn)故事閱讀 68,888評(píng)論 6 398
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著衍腥,像睡著了一般磺樱。 火紅的嫁衣襯著肌膚如雪纳猫。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 52,475評(píng)論 1 312
  • 那天竹捉,我揣著相機(jī)與錄音芜辕,去河邊找鬼。 笑死块差,一個(gè)胖子當(dāng)著我的面吹牛侵续,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播憨闰,決...
    沈念sama閱讀 41,010評(píng)論 3 422
  • 文/蒼蘭香墨 我猛地睜開眼状蜗,長吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來了鹉动?” 一聲冷哼從身側(cè)響起轧坎,我...
    開封第一講書人閱讀 39,924評(píng)論 0 277
  • 序言:老撾萬榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎泽示,沒想到半個(gè)月后缸血,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 46,469評(píng)論 1 319
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡械筛,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,552評(píng)論 3 342
  • 正文 我和宋清朗相戀三年捎泻,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片变姨。...
    茶點(diǎn)故事閱讀 40,680評(píng)論 1 353
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡族扰,死狀恐怖厌丑,靈堂內(nèi)的尸體忽然破棺而出定欧,到底是詐尸還是另有隱情,我是刑警寧澤怒竿,帶...
    沈念sama閱讀 36,362評(píng)論 5 351
  • 正文 年R本政府宣布砍鸠,位于F島的核電站,受9級(jí)特大地震影響耕驰,放射性物質(zhì)發(fā)生泄漏爷辱。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 42,037評(píng)論 3 335
  • 文/蒙蒙 一朦肘、第九天 我趴在偏房一處隱蔽的房頂上張望饭弓。 院中可真熱鬧,春花似錦媒抠、人聲如沸弟断。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,519評(píng)論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽阀趴。三九已至昏翰,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間刘急,已是汗流浹背棚菊。 一陣腳步聲響...
    開封第一講書人閱讀 33,621評(píng)論 1 274
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留叔汁,地道東北人统求。 一個(gè)月前我還...
    沈念sama閱讀 49,099評(píng)論 3 378
  • 正文 我出身青樓,卻偏偏與公主長得像攻柠,于是被迫代替她去往敵國和親球订。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,691評(píng)論 2 361

推薦閱讀更多精彩內(nèi)容