使用开源工具将任何网站变成 Linux 桌面应用

Nativefier 和 Electron 可以从任何网站创建桌面应用。
65 位读者喜欢这篇文章。
Text editor on a browser, in blue

Mastodon 是一个很棒的开源、去中心化的社交网络。我每天都使用 Mastodon,并且通常通过其 Web 界面使用(虽然它是开源的,有很多不同的方式与之互动,包括基于终端的应用程序和移动应用程序),但我更喜欢专用的应用程序窗口。

最近,我发现了 Nativefier,现在我可以在我的 Linux 桌面上,像桌面应用程序一样享受 Mastodon 或任何其他 Web 应用程序。 Nativefier 接受一个 URL 并用 Electron 框架将其包装起来,该框架运行开源 Chromium 浏览器作为其后端,但作为其自身的exe可执行应用程序运行。 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.cn/index.php/Desktop_entries

输出显示文件被放置在 /home/tux/NativeApps/Mastodon-linux-x64 中。 当您 cd 进入此文件夹时,您会看到一个名为 Mastodon 的文件。 这是启动应用的主可执行文件。 在您可以启动它之前,您必须赋予它适当的权限。

$ cd Mastodon-linux-x64
chmod +x Mastodon

现在,执行 ./Mastodon 以查看您的 Linux 应用启动!

为我的博客创建一个应用

为了好玩,我还将为我的博客网站创建一个应用。 如果没有适用于它的 Linux 应用,拥有一个技术博客有什么用?

命令

$ 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 创建的应用。

命令

$ 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://ayushsharma.in
我是一名作家和 AWS 解决方案架构师。 我与初创公司和企业合作开展软件工程、DevOps、SRE 和云架构。 我在 https://ayushsharma.in 上写下我的经历。

1 条评论

很酷的文章! 感谢分享。

Creative Commons License本作品根据知识共享署名-相同方式共享 4.0 国际许可协议授权。
© . All rights reserved.