用開源工具將任何網(wǎng)站變成 Linux 桌面應(yīng)用
使用 Nativefier 和 Electron 從任何網(wǎng)站創(chuàng)建桌面應(yīng)用。
Mastodon 是一個(gè)很好的開源、去中心化的社交網(wǎng)絡(luò)。我每天都在使用 Mastodon,通過它的網(wǎng)頁界面使用 Mastodon 可能是最常見的方式(盡管因?yàn)殚_源,它有許多不同的交互方式,包括基于終端的應(yīng)用和移動應(yīng)用),但我更喜歡專門的應(yīng)用窗口。
最近,我發(fā)現(xiàn)了 Nativefier,現(xiàn)在我可以在我的 Linux 桌面上把 Mastodon 或其他任何網(wǎng)頁應(yīng)用作為桌面應(yīng)用來使用。Nativefier 將一個(gè) URL 用 Electron 框架包裝起來,它將開源的 Chromium 瀏覽器作為后端,但使用自己的可執(zhí)行程序運(yùn)行。Nativefier 采用 MIT 許可證,可用于 Linux、Windows 和 MacOS。
安裝 Nativefier
Nativefier 需要 Node.js。
安裝 Nativefier 只需運(yùn)行:
$ sudo npm install -g nativefier
在我的 Ubuntu 桌面上,我必須先升級 NodeJS,所以當(dāng)你安裝 Nativefier 時(shí),一定要檢查需要哪個(gè) Node 版本。
安裝完畢后,你可以檢查你的 Nativefier 的版本,以驗(yàn)證它是否已經(jīng)安裝:
$ nativefier --version
45.0.4
運(yùn)行 nativefier --help
列出了應(yīng)用支持的所有選項(xiàng)。
設(shè)置
我建議你在開始用 Nativefier 創(chuàng)建應(yīng)用之前,創(chuàng)建一個(gè)名為 ~/NativeApps
的新文件夾。這有助于保持你的應(yīng)用有序。
$ mkdir ~/NativeApps
cd ~/NativeApps
為 Mastodon 創(chuàng)建一個(gè)應(yīng)用程序
我將首先為 mastodon.technology 創(chuàng)建一個(gè)應(yīng)用。
使用以下命令:
$ nativefier --name Mastodon \
--platform linux --arch x64 \
--width 1024 --height 768 \
--tray --disable-dev-tools \
--single-instance https://mastodon.technology
這個(gè)例子中的選項(xiàng)做了以下工作:
--name
:設(shè)置應(yīng)用的名稱為 Mastodon--platform
:設(shè)置應(yīng)用程序的平臺為 Linux--arch x64
:設(shè)置架構(gòu)為 x64--width 1024 --height 768
:設(shè)置應(yīng)用啟動時(shí)的大小--tray
:為應(yīng)用創(chuàng)建一個(gè)托盤圖標(biāo)--disable-dev-tools
:禁用 Chrome 開發(fā)工具--single-instance
:只允許應(yīng)用有一個(gè)實(shí)例
運(yùn)行這條命令會顯示以下輸出:
Preparing Electron app...
Converting icons...
Packaging... This will take a few seconds, maybe minutes if the requested Electron isn't cached yet...
Packaging app for platform linux x64 using electron v13.4.0 Finalizing build...
App built to /home/tux/NativeApps/Mastodon-linux-x64, move to wherever it makes sense for you and run the contained executable file (prefixing with ./ if necessary)
Menu/desktop shortcuts are up to you, because Nativefier cannot know where you're going to move the app. Search for "linux .desktop file" for help, or see https://wiki.archlinux.org/index.php/Desktop_entries
輸出顯示,文件被放置在 /home/tux/NativeApps/Mastodon-linux-x64
。當(dāng)你 cd
進(jìn)入這個(gè)文件夾,你會看到一個(gè)名為 Mastodon
的文件。這是啟動該應(yīng)用的主要可執(zhí)行文件。在你啟動它之前,你必須給它適當(dāng)?shù)臋?quán)限。
$ cd Mastodon-linux-x64
chmod +x Mastodon
現(xiàn)在,執(zhí)行 ./Mastodon
就可以看到你的 Linux 應(yīng)用啟動了!
Mastodon app launched
為我的博客創(chuàng)建一個(gè)應(yīng)用
為了好玩,我也要為我的博客創(chuàng)建一個(gè)應(yīng)用。如果沒有 Linux 應(yīng)用,擁有一個(gè)技術(shù)博客有什么用?
Ayush Sharma blog
命令是:
$ nativefier -n ayushsharma \
-p linux -a x64 \
--width 1024 --height 768 \
--tray --disable-dev-tools \
--single-instance https://ayushsharma.in
$ cd ayushsharma-linux-x64
chmod +x ayushsharma
為 findmymastodon.com 創(chuàng)建一個(gè)應(yīng)用
最后,這是為我的寵物項(xiàng)目 findmymastodon.com 制作的應(yīng)用。
Find my mastodon website
命令是:
$ nativefier -n findmymastodon \
-p linux -a x64 \
--width 1024 --height 768 \
--tray --disable-dev-tools \
--single-instance https://findmymastodon.com
$ cd findmymastodon-linux-x64
chmod +x findmymastodon
創(chuàng)建 Linux 桌面圖標(biāo)
應(yīng)用已經(jīng)創(chuàng)建并可以執(zhí)行了,現(xiàn)在是創(chuàng)建桌面圖標(biāo)的時(shí)候了。
作為示范,以下是如何為 Mastodon 啟動器創(chuàng)建一個(gè)桌面圖標(biāo)。首先,下載一個(gè) Mastodon 的圖標(biāo)。將該圖標(biāo)放在其 Nativefier 應(yīng)用目錄下,名為 icon.png
。
然后創(chuàng)建一個(gè)名為 Mastodon.desktop
的文件并輸入以下文本:
[Desktop Entry]
Type=Application
Name=Mastodon
Path=/home/tux/NativeApps/Mastodon-linux-x64
Exec=/home/tux/NativeApps/Mastodon-linux-x64/Mastodon
Icon=/home/tux/NativeApps/Mastodon-linux-x64/icon.png
你可以把 .desktop
文件移到你的 Linux 桌面上,把它作為一個(gè)桌面啟動器。你也可以把它復(fù)制到 ~/.local/share/applications
中,這樣它就會出現(xiàn)在你的應(yīng)用菜單或活動啟動器中。
總結(jié)
我喜歡為我經(jīng)常使用的工具配備專門的應(yīng)用。我最喜歡的一個(gè) Mastodon 應(yīng)用特點(diǎn)是,當(dāng)我登錄到 Mastodon 之后,我就不必再次登錄了! Nativefier 在底層運(yùn)行 Chromium。所以它能夠像其他瀏覽器一樣記住你的會話。我想特別感謝 Nativefier 團(tuán)隊(duì),他們讓 Linux 桌面離完美更近了一步。