使用 Nativefier 和 Electron 從任何網站創建桌面應用。
Mastodon 是一個很好的開源、去中心化的社交網絡。我每天都在使用 Mastodon,通過它的網頁界面使用 Mastodon 可能是最常見的方式(盡管因為開源,它有許多不同的交互方式,包括基于終端的應用和移動應用),但我更喜歡專門的應用窗口。
最近,我發現了 Nativefier,現在我可以在我的 Linux 桌面上把 Mastodon 或其他任何網頁應用作為桌面應用來使用。Nativefier 將一個 URL 用 Electron 框架包裝起來,它將開源的 Chromium 瀏覽器作為后端,但使用自己的可執行程序運行。Nativefier 采用 MIT 許可證,可用于 Linux、Windows 和 MacOS。
安裝 Nativefier
Nativefier 需要 Node.js。
安裝 Nativefier 只需運行:
-
$ sudo npm install -g nativefier
在我的 Ubuntu 桌面上,我必須先升級 NodeJS,所以當你安裝 Nativefier 時,一定要檢查需要哪個 Node 版本。
安裝完畢后,你可以檢查你的 Nativefier 的版本,以驗證它是否已經安裝:
-
$ nativefier --version
-
45.0.4
運行 nativefier --help
列出了應用支持的所有選項。
設置
我建議你在開始用 Nativefier 創建應用之前,創建一個名為 ~/NativeApps
的新文件夾。這有助于保持你的應用有序。
-
$ mkdir ~/NativeApps
-
cd ~/NativeApps
為 Mastodon 創建一個應用程序
我將首先為 mastodon.technology 創建一個應用。
使用以下命令:
-
$ nativefier --name Mastodon \
-
--platform linux --arch x64 \
-
--width 1024 --height 768 \
-
--tray --disable-dev-tools \
-
--single-instance https://mastodon.technology
這個例子中的選項做了以下工作:
-
--name
:設置應用的名稱為 Mastodon -
--platform
:設置應用程序的平臺為 Linux -
--arch x64
:設置架構為 x64 -
--width 1024 --height 768
:設置應用啟動時的大小 -
--tray
:為應用創建一個托盤圖標 -
--disable-dev-tools
:禁用 Chrome 開發工具 -
--single-instance
:只允許應用有一個實例
運行這條命令會顯示以下輸出:
-
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
。當你 cd
進入這個文件夾,你會看到一個名為 Mastodon
的文件。這是啟動該應用的主要可執行文件。在你啟動它之前,你必須給它適當的權限。
-
$ cd Mastodon-linux-x64
-
chmod +x Mastodon
現在,執行 ./Mastodon
就可以看到你的 Linux 應用啟動了!
Mastodon app launched
為我的博客創建一個應用
為了好玩,我也要為我的博客創建一個應用。如果沒有 Linux 應用,擁有一個技術博客有什么用?
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 創建一個應用
最后,這是為我的寵物項目 findmymastodon.com 制作的應用。
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
創建 Linux 桌面圖標
應用已經創建并可以執行了,現在是創建桌面圖標的時候了。
作為示范,以下是如何為 Mastodon 啟動器創建一個桌面圖標。首先,下載一個 Mastodon 的圖標。將該圖標放在其 Nativefier 應用目錄下,名為 icon.png
。
然后創建一個名為 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 桌面上,把它作為一個桌面啟動器。你也可以把它復制到 ~/.local/share/applications
中,這樣它就會出現在你的應用菜單或活動啟動器中。
總結
我喜歡為我經常使用的工具配備專門的應用。我最喜歡的一個 Mastodon 應用特點是,當我登錄到 Mastodon 之后,我就不必再次登錄了! Nativefier 在底層運行 Chromium。所以它能夠像其他瀏覽器一樣記住你的會話。我想特別感謝 Nativefier 團隊,他們讓 Linux 桌面離完美更近了一步。
原文鏈接:https://linux.cn/article-13975-1.html