英文博客伪原创(使用Nativefier和Electron从任何网站创建桌面应用中的使用)
优采云 发布时间: 2022-01-13 14:21英文博客伪原创(使用Nativefier和Electron从任何网站创建桌面应用中的使用)
使用 Nativefier 和 Electron 从任何 网站 创建桌面应用程序。
Mastodon 是一个伟大的开源、去中心化的社交网络。我每天都在使用 Mastodon,通过它的 Web 界面使用 Mastodon 可能是最常见的方式(虽然因为它是开源的,所以它有很多不同的交互方式,包括基于终端的应用程序和移动应用程序),但我更喜欢专用的应用程序窗户。
最近,我发现了 Nativefier,现在我可以在我的 Linux 桌面上使用 Mastodon 或任何其他 Web 应用程序作为桌面应用程序。Nativefier 使用 Electron 框架包装 URL,该框架使用开源 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
此示例中的选项执行以下操作:
运行此命令将显示以下输出:
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 应用程序已启动!
为我的博客创建应用程序
为了好玩,我还将为我的博客创建一个应用程序。如果您没有 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
创建应用程序
最后,这是为我的宠物项目制作的应用程序。
命令是:
$ 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 图标。将图标作为 icon.png 放置在其 Nativefier 应用程序目录中。
然后创建一个名为 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 桌面更接近完美。
本文首发于作者网站,经授权转载。
通过:
作者:Ayush Sharma 主题:lujun9972 译者:geekpi 校对:wxy
本文由LCTT原创编译,Linux中国荣幸推出