跳转到主内容

Electron Simple Samples

· 阅读时间:约 2 分钟

We recently hosted an Electron hackathon at GitHub HQ for members of Hackbright Academy, a coding school for women founded in San Francisco. To help attendees get a head start on their projects, our own Kevin Sawicki created a few sample Electron applications.


If you're new to Electron development or haven't yet tried it out, these sample applications are a great place to start. They are small, easy to read, and the code is heavily commented to explain how everything works.

To get started, clone this repository:

git clone https://github.com/electron/simple-samples

To run any of the apps below, change into the app's directory, install dependencies, then start:

cd activity-monitor
npm install
npm start

Activity Monitor

Shows a doughnut chart of the CPU system, user, and idle activity time.

截屏

哈希

Shows the hash values of entered text using different algorithms.

截屏

镜像

Plays a video of the computer's camera at a maximized size like looking into a mirror. Includes an optional rainbow filter effect that uses CSS animations.

Prices

Shows the current price of oil, gold, and silver using the Yahoo Finance API.

截屏

URL

Loads a URL passed on the command line in a window.

Other Resources

We hope these apps help you get started using Electron. Here are a handful other resources for learning more:

Electron Userland

· 阅读时间:约 3 分钟

We've added a new userland section to the Electron website to help users discover the people, packages, and apps that make up our flourishing open-source ecosystem.


github-contributors

Origins of Userland

Userland is where people in software communities come together to share tools and ideas. The term originated in the Unix community, where it referred to any program that ran outside of the kernel, but today it means something more. When people in today's Javascript community refer to userland, they're usually talking about the npm package registry. This is where the majority of experimentation and innovation happens, while Node and the JavaScript language (like the Unix kernel) retain a relatively small and stable set of core features.

Node and Electron

Like Node, Electron has a small set of core APIs. These provide the basic features needed for developing multi-platform desktop applications. 这个设计理念让 Electron 能够保持灵活而不被过多的规定有关于如何应该被使用。

Userland is the counterpart to "core", enabling users to create and share tools that extend Electron's functionality.

Collecting data

To better understand the trends in our ecosystem, we analyzed metadata from 15,000 public GitHub repositories that depend on electron or electron-prebuilt

We used the GitHub API, the libraries.io API, and the npm registry to gather info about dependencies, development dependencies, dependents, package authors, repo contributors, download counts, fork counts, stargazer counts, etc.

We then used this data to generate the following reports:

Filtering Results

Reports like app dependencies and starred apps which list packages, apps, and repos have a text input that can be used to filter the results.

As you type into this input, the URL of the page is updated dynamically. This allows you to copy a URL representing a particular slice of userland data, then share it with others.

babel

More to come

This first set of reports is just the beginning. We will continue to collect data about how the community is building Electron, and will be adding new reports to the website.

All of the tools used to collect and display this data are open-source:

If you have ideas about how to improve these reports, please let us know opening an issue on the website repository or any of the above-mentioned repos.

Thanks to you, the Electron community, for making userland what it is today!

证书透明度的修改

· 阅读时间:约 3 分钟

Electron 1.4. 2 包含一个重要的补丁,修复了一些Smantec、GeoTrust的上游Chrome 问题。 和 Thawte SSL/TLS 证书 被错误地拒绝了从 libchromiumcontent生成时间起10周内的 Chrome 库 Electron。 在受影响的网站上使用的 证书没有问题,替换这些证书并不会做什么。


在 Electron 1.4.0 — 1.4.11 对使用这些受影响的 证书的站点的HTTPS 请求将在某个日期之后由于网络错误而失败。 This affects HTTPS requests made using Chrome's underlying networking APIs such as window.fetch, Ajax requests, Electron's net API, BrowserWindow.loadURL, webContents.loadURL, the src attribute on a <webview> tag, and others.

将你的应用程序升级到1.4.12以防止请求失败。

**注意:**这个版本是在 Chrome 53 版本引入的,所以早入 1.4.0 版本之前的 Electron 版本不受影响。

Impact Dates

Below is a table of each Electron 1.4 version and the date when requests to sites using these affected certificates will start to fail.

Electron VersionImpact Date
1.3.x不受影响
1.4.0已经失败
1.4.1已经失败
1.4.2已经失败
1.4.312月 10日 2016年 9:00 PM PST
1.4.412月 10日 2016年 9:00 PM PST
1.4.512月 10日 2016年 9:00 PM PST
1.4.61月 14日 2017年 9:00 PM PST
1.4.71月 14日 2017年 9:00 PM PST
1.4.81月 14日 2017年 9:00 PM PST
1.4.91月 14日 2017年 9:00 PM PST
1.4.101月 14日 2017年 9:00 PM PST
1.4.112月 11日 2017年 9:00 PM PST
1.4.12不受影响

您可以通过设置您的计算机的时钟提前来验证您的应用程序的影响日期,然后检测https://symbeta.symantec.com/welcome/是否能够成功加载。

更多資料

您可以在以下链接阅读有关此主题、原始问题以及问题修复的更多信息:

September 2016: New Apps

· 阅读时间:约 3 分钟

Here are the new Electron apps and talks that were added to the site in September.


This site is updated with new apps and meetups through pull requests from the community. 你可以查看库以获取新添加内容的通知,或如果你对_所有_更新不感兴趣,关注博客的RSS订阅

如果您已经制作了一个 Electron 应用程序或主办了一次会议,提交一个 拉取请求 添加到站点,它将被包括进下一次统计。

New Talks

In September, GitHub held its GitHub Universe conference billed as the event for people building the future of software. There were a couple of interesting Electron talks at the event.

Also, if you happen to be in Paris on December 5, Zeke will be giving an Electron talk at dotJS 2016.

新应用

PexelsSearch for completely free photos and copy them into your clipboard
时间戳A better macOS menu bar clock with a customizable date/time display and a calendar
HarmonyMusic player compatible with Spotify, Soundcloud, Play Music and your local files
uPhoneWebRTC Desktop Phone
SealTalkInstant-messaging App powered by RongCloud IM Cloud Service and IM SDK
InfinityAn easy way to make presentation
Cycligent Git ToolStraightforward, graphic GUI for your Git projects
FocoStay focused and boost productivity with Foco
StrawberryWin Diners for Life Know and serve them better with the all-in-one restaurant software suite.
MixmaxSee every action on your emails in real-time Compose anywhere.
Firebase AdminA Firebase data management tool
ANoteA Simple Friendly Markdown Note
TempsA simple but smart menubar weather app
AmiumA work collaboration product that brings conversation to your files
SoubeSimple music player
(Un)coloredNext generation desktop rich content editor that saves documents with themes HTML & Markdown compatible. For Windows, OS X & Linux.
quickcalcMenubar Calculator
Forestpin AnalyticsFinancial data analytics tool for businesses
LingREST Client
ShortextsShortcuts for texts you copy frequently, folders and emojis
Front-End BoxSet of front-end-code generators

结构化数据式的Electron API文档

· 阅读时间:约 5 分钟

今天我们正式宣布对Electron文档的一些改进。 现在,每个新的发布版本都将包括一份对Electron公共API详细描述的JSON文件 我们创建该文件,以此让开发者能够用新潮有趣的方式来访问Electron的API文档。


架构概述

每个 API 都是一个对象,具有名称、描述、类型等属性。 BrowserWindowMenu 类具有描述其实例方法、实例属性、实例事件等 附加属性。

下面是描述 BrowserWindow 类的架构摘录:

{
name: 'BrowserWindow',
description: 'Create and control browser windows.',
process: {
main: true,
renderer: false
},
type: 'Class',
instanceName: 'win',
slug: 'browser-window',
websiteUrl: 'https://electronjs.org/docs/api/browser-window',
repoUrl: 'https://github.com/electron/electron/blob/v1.4.0/docs/api/browser-window.md',
staticMethods: [...],
instanceMethods: [...],
instanceProperties: [...],
instanceEvents: [...]
}
}

下面是一个方法描述的示例,在本例中为 apis.BrowserWindow.instanceMethods.setMaximumSize 实例方法:

{
name: 'setMaximumSize',
signature: '(width, height)',
description: 'Sets the maximum size of window to width and height.',
parameters: [{
name: 'width',
type: 'Integer'
}, {
name: 'height',
type: 'Integer'
}]
}

使用新数据

为了让开发人员能够轻松地在他们的项目中使用这些结构化数据, 我们创建了 electron-docs-api,一个小型 npm包,每当有新的Electron 发布时,它都会自动发布。

npm install electron-api-docs --save

为了及时尝试,请尝试Node.js REPL中的模块:

npm i -g trymodule && trymodule electron-api-docs=apis

如何收集数据

Electron的API文档遵循 Electron 编码风格Electron 风格指南, 因此其内容可以通过编程方式解析。

electron-docs-linterelectron/electron 库的新开发依赖项。 它是一个命令行工具,会校验所有Markdown文件并强制执行styleguid中的规则。 如果发现错误,则会列出这些错误,并暂停发布 过程。 如果 API 文档有效,则会创建 electron-json.api 文件 ,并作为 Electron 版本的一部分上传到 GitHub

标准 Javascript 和标准 Markdown

今年早些时候,Electron的代码库进行了更新,以在所有JavaScript中使用 standard 代码纠错工具(linter)。 Standard的 README文件总结了这一选择背后的原因:

采用标准风格意味着代码清晰度和社区约定的重要性比个人风格更高。 也许这对所有项目和发展文化来说是难以理解的,但对新手来说开源可能是一个令人生畏的地方。 设定明确、自动的代码贡献者期望能使一个项目更加健康。

我们最近还创建了[standard-markdown](https://github. com/zeke/standard-markdown) ,以验证文档中所有JavaScript代码片段是否有效且与代码库本身的样式一致。

这些工具一起帮助我们使用连续集成 (CI) 来自动找到 拉取请求(Pull Request)中的错误。 这减轻了人类在代码 审查方面的负担,并使我们对文档的准确性更有信心。

社区的努力

因我们可敬的开源社区的不懈努力,Electron的文档在不断改进。 在撰写本文时, 近300人为文档做出了贡献。

我们很期待看到人们如何处理这个新的结构化数据。 可能的用途包括:

Electron Internals: Weak References

· 阅读时间:约 6 分钟

As a language with garbage collection, JavaScript frees users from managing resources manually. But because Electron hosts this environment, it has to be very careful avoiding both memory and resources leaks.

This post introduces the concept of weak references and how they are used to manage resources in Electron.


Weak references

In JavaScript, whenever you assign an object to a variable, you are adding a reference to the object. As long as there is a reference to the object, it will always be kept in memory. Once all references to the object are gone, i.e. there are no longer variables storing the object, the JavaScript engine will recoup the memory on next garbage collection.

A weak reference is a reference to an object that allows you to get the object without effecting whether it will be garbage collected or not. You will also get notified when the object is garbage collected. It then becomes possible to manage resources with JavaScript.

Using the NativeImage class in Electron as an example, every time you call the nativeImage.create() API, a NativeImage instance is returned and it is storing the image data in C++. Once you are done with the instance and the JavaScript engine (V8) has garbage collected the object, code in C++ will be called to free the image data in memory, so there is no need for users manage this manually.

另一个例子是 窗口消失的问题, 哪些 视觉显示当所有引用都消失时窗口是如何收集垃圾的

Testing weak references in Electron

There is no way to directly test weak references in raw JavaScript since the language doesn't have a way to assign weak references. The only API in JavaScript related to weak references is WeakMap, but since it only creates weak-reference keys, it is impossible to know when an object has been garbage collected.

In versions of Electron prior to v0.37.8, you can use the internal v8Util.setDestructor API to test weak references, which adds a weak reference to the passed object and calls the callback when the object is garbage collected:

// 下面的代码只能在 Electron < v0.37.8. 上运行。
var v8Util = process.atomBinding('v8_util');

var object = {};
v8Util.setDestructor(object, function () {
console.log('The object is garbage collected');
});

// Remove all references to the object.
object = undefined;
// Manually starts a GC.
gc();
// Console prints "The object is garbage collected".

Note that you have to start Electron with the --js-flags="--expose_gc" command switch to expose the internal gc function.

The API was removed in later versions because V8 actually does not allow running JavaScript code in the destructor and in later versions doing so would cause random crashes.

Weak references in the remote module

Apart from managing native resources with C++, Electron also needs weak references to manage JavaScript resources. Electron的 远程 模块就是一个例子。 这是一个 远程程序调用 (RPC) 模块 允许在主进程中使用渲染器进程中的物体。

One key challenge with the remote module is to avoid memory leaks. When users acquire a remote object in the renderer process, the remote module must guarantee the object continues to live in the main process until the references in the renderer process are gone. Additionally, it also has to make sure the object can be garbage collected when there are no longer any reference to it in renderer processes.

For example, without proper implementation, following code would cause memory leaks quickly:

const { remote } = require('electron');

for (let i = 0; i < 10000; ++i) {
remote.nativeImage.createEmpty();
}

The resource management in the remote module is simple. Whenever an object is requested, a message is sent to the main process and Electron will store the object in a map and assign an ID for it, then send the ID back to the renderer process. In the renderer process, the remote module will receive the ID and wrap it with a proxy object and when the proxy object is garbage collected, a message will be sent to the main process to free the object.

Using remote.require API as an example, a simplified implementation looks like this:

remote.require = function (name) {
// Tell the main process to return the metadata of the module.
const meta = ipcRenderer.sendSync('REQUIRE', name);
// Create a proxy object.
const object = metaToValue(meta);
// Tell the main process to free the object when the proxy object is garbage
// collected.
v8Util.setDestructor(object, function () {
ipcRenderer.send('FREE', meta.id);
});
return object;
};

在主进程:

const map = {};
const id = 0;

ipcMain.on('REQUIRE', function (event, name) {
const object = require(name);
// Add a reference to the object.
map[++id] = object;
// 将对象转换为元数据
event.returnValue = valueToMeta(id, object);
});

ipcMain.on('FREE', function (event, id) {
delete map[id];
});

Maps with weak values

With the previous simple implementation, every call in the remote module will return a new remote object from the main process, and each remote object represents a reference to the object in the main process.

The design itself is fine, but the problem is when there are multiple calls to receive the same object, multiple proxy objects will be created and for complicated objects this can add huge pressure on memory usage and garbage collection.

For example, the following code:

const { remote } = require('electron');

for (let i = 0; i < 10000; ++i) {
remote.getCurrentWindow();
}

It first uses a lot of memory creating proxy objects and then occupies the CPU (Central Processing Unit) for garbage collecting them and sending IPC messages.

An obvious optimization is to cache the remote objects: when there is already a remote object with the same ID, the previous remote object will be returned instead of creating a new one.

This is not possible with the API in JavaScript core. 使用普通的Map对象保存对象将会防止被V8垃圾回收机制回收,但是,WeakMap 只能将对象作为弱引用。

To solve this, a map type with values as weak references is added, which is perfect for caching objects with IDs. Now the remote.require looks like this:

const remoteObjectCache = v8Util.createIDWeakMap()

remote.require = function (name) {
// Tell the main process to return the meta data of the module.
...
if (remoteObjectCache.has(meta.id))
return remoteObjectCache.get(meta.id)
// Create a proxy object.
...
remoteObjectCache.set(meta.id, object)
return object
}

Note that the remoteObjectCache stores objects as weak references, so there is no need to delete the key when the object is garbage collected.

Native code

For people interested in the C++ code of weak references in Electron, it can be found in following files:

The setDestructor API:

The createIDWeakMap API:

August 2016: New Apps

· 阅读时间:约 3 分钟

这里是8月份添加到网站的新的 Electron 应用。


此网站通过社区的拉取请求更新了新的应用线下聚会 你可以查看库以获取新添加内容的通知,或如果你对_所有_更新不感兴趣,关注博客的RSS订阅

如果您已经制作了一个 Electron 应用程序或主办了一次会议,提交一个 拉取请求 添加到站点,它将被包括进下一次统计。

新应用

Code RPGifyRPG风格应用开发程序
PamFax用于发送和接收传真的跨平台应用
BlankUp又一清新的Markdown编辑器
Rambox将常见的即时通讯类和邮件类应用的网页版集成于一处的免费开源软件。
Gordie最棒的卡包应用
Ionic Creator更快构建应用程序
TwitchAlertsKeep your viewers happy with beautiful alerts and notifications
Museeks一个简单、干净和跨平台音乐播放器
SeaPigMarkdown转HTML器
GroupMe非官方GroupMe应用程式
MoeditorYour all-purpose markdown editor
SoundnodeSoundnode App is the Soundcloud for desktop
QMUI WebQMUI Web 桌面是一个基于QMUI Web 框架管理项目的应用程序
SvgsusOrganize, clean and transform your SVGs
Ramme非官方 Instagram 桌面应用程序
InsomniaREST API 客户端
Correo窗口、 macOS 和 Linux 的菜单栏/任务栏 Gmail 应用程序
KongDashKong Admin API 桌面客户端
翻译编辑器INTL ICU 信息的翻译文件编辑器(见formatjsio)
5EClient5EPlay CSGO 客户端
Theme Juice使本地 WordPress 开发变得更加容易。

辅助工具

· 阅读时间:约 3 分钟

创建具有辅助功能的应用程序是很重要的,我们很乐意介绍DevtronSpectron,这两个新功能能让开发者们有机会让它们的应用程序对每个人都更加可用。


Electron 应用中有关辅助功能的开发和网站是相似的因为两者最终使用的都是HTML. 然而, 对于Electron应用, 你不能使用在线的辅助功能审查者, 因为你的应用没有一个URL可以提供给审查者.

这些新功能将这些审计工具带到您的Electron应用程序中。 您可以选择使用 Spectron 将审计工具添加到测试中,也可以在 DevTron 的 DevTools 中使用它们。 继续阅读可简要了解这两个工具或阅读 辅助功能文档 以获取更加详细的信息。

Spectron

在测试框架Spectron中,你可以审查应用程序中的每个 window 和 <webview> 标签。 例如:

app.client.auditAccessibility().then(function (audit) {
if (audit.failed) {
console.error(audit.message);
}
});

你可以从这里Spectron文档阅读到更多有关于这个功能的信息。

Devtron

在 Devtron 中, 有一个新的辅助功能选项卡, 允许您对应用程序中的某一个页面进行审核, 并对审核结果进行排序和筛选。

devtron 截图

这两种工具都使用了Google 为 Chrome 所构建的 辅助功能开发工具 库。 您可以在该 repository's wiki 上更加详细的了解这个库使用了哪些辅助功能审核规则。

如果您知道其他很好的Electron辅助功能工具, 请创建一个pull request来将它们添加到 辅助功能文档 中。

npm install electron

· 阅读时间:约 4 分钟

Electron 从 1.3.1 版本开始,可以使用 npm install electron --save-dev 安装最新的预编译版本的 Electron 到你的应用程序中。


npm install electron

预编译的 Electron 二进制分发文件

如果你在此前使用过一些 Electron 构建的应用程序,你大概率会接触到 electron-prebuilt 这个 npm 软件包。 这个软件包对于每个 Electron 项目来说几乎都是不可或缺的。 当你安装这个软件包时,它会检测你的操作系统,然后下载适合于你的系统的二进制分发文件。

新的名字

Electron 的安装过程对于初学者来说几乎是一道高墙。 许多勇敢的人在初次使用 Electron 开发应用程序时往往会试图运行 npm install electron 安装 Electron,然而正确的做法却是反直觉的 npm install electron-prebuilt,他们往往在经历了许多混乱之后才会意识到这不是他们想要的 electron

这是因为在 npm 上已经有名为 electron 的项目,在 GitHub 的 Electron 项目创建之前就早已存在。 为了让 Electron 的开发更加容易入门且直观,我们联系了目前 electron 这个 npm 软件包的拥有者,询问他是否愿意将名字转让给我们。 幸运的是,他是我们项目的粉丝,并且同意了调整名字的提案。

prebuilt 依旧存在

从 1.3.1 版本开始,我们同时发布了 electronelectron-prebuilt 两个软件包到 npm 上。 这两个软件包是完全一致的。 由于现在有许多开发者还在他们的项目里使用 electron-prebuilt 这个名字,在未来的一段时间内,我们还会继续使用这两个名字发布软件包。 我们推荐更新你的package.json文件去使用新的electron依赖,但是我们会持续发布新的electron-prebuilt 直到2016年底。

electron-userland/electron-prebuilt 仓库将继续作为 electron 软件包存在。

致谢

我们特别感谢 @mafintosh@maxogden 和许多 贡献者,他们创建并维护了 electron-prebuilt 项目,并且不懈服务 JavaScript、Node.js 和 Electron 社区。

此外,我们也对转让了 electron 包名的 @logicalparadox 致以诚挚的谢意。

更新你的项目

我们与社区一起努力更新了受此次变更影响的一些热门软件包。 electron-packagerelectron-rebuildelectron-builder 等软件包已经支持了新名字,同时保留了对旧名字的支持。

如果你在使用这些新软件包的时候发现任何问题,请务必在 electron-userland/electron-prebuilt 开启一个 issue 告知我们。

对于 Electron 相关的其他问题,请在 electron/electron 仓库开启 issue。

Electron Internals: Using Node as a Library

· 阅读时间:约 5 分钟

This is the second post in an ongoing series explaining the internals of Electron. Check out the first post about event loop integration if you haven't already.

Most people use Node for server-side applications, but because of Node's rich API set and thriving community, it is also a great fit for an embedded library. This post explains how Node is used as a library in Electron.


构建系统

节点和 Electron 都使用 GYP 作为他们的构建系统。 If you want to embed Node inside your app, you have to use it as your build system too.

New to GYP? 新建 GYP? Read this guide before you continue further in this post.

Node's flags

个节点。 yp 节点源代码目录中的文件描述节点 是如何构建的, 加上许多 GYP 变量来控制 节点的哪些部分已启用以及是否打开某些配置。

To change the build flags, you need to set the variables in the .gypi file of your project. The configure script in Node can generate some common configurations for you, for example running ./configure --shared will generate a config.gypi with variables instructing Node to be built as a shared library.

Electron does not use the configure script since it has its own build scripts. 节点配置在 common.gypi 文件 中定义了 Electron的根源代码目录。

In Electron, Node is being linked as a shared library by setting the GYP variable node_shared to true, so Node's build type will be changed from executable to shared_library, and the source code containing the Node's main entry point will not be compiled.

Since Electron uses the V8 library shipped with Chromium, the V8 library included in Node's source code is not used. This is done by setting both node_use_v8_platform and node_use_bundled_v8 to false.

Shared library or static library

When linking with Node, there are two options: you can either build Node as a static library and include it in the final executable, or you can build it as a shared library and ship it alongside the final executable.

In Electron, Node was built as a static library for a long time. This made the build simple, enabled the best compiler optimizations, and allowed Electron to be distributed without an extra node.dll file.

然而,Chrome切换到使用 BoringSSL 后改变了这种情况。 BoringSSL is a fork of OpenSSL that removes several unused APIs and changes many existing interfaces. 因为节点仍在使用 OpenSSL,编译器会产生无数的 链接错误,如果它们是相互冲突的符号连接在一起的话。 Because Node still uses OpenSSL, the compiler would generate numerous linking errors due to conflicting symbols if they were linked together.

Electron 无法在节点中使用 BoringSSL 或在 Chromium 中使用 OpenSSL 所以唯一的 选项是切换到构建节点作为共享库, 和 隐藏每个组件中的 BoringSSL 和 OpenSSL 符号

This change brought Electron some positive side effects. Before this change, you could not rename the executable file of Electron on Windows if you used native modules because the name of the executable was hard coded in the import library. After Node was built as a shared library, this limitation was gone because all native modules were linked to node.dll, whose name didn't need to be changed.

Supporting native modules

节点工作中的原生模块 ,定义节点加载的条目函数。 然后从节点中搜索V8和libuv 的符号。 This is a bit troublesome for embedders because by default the symbols of V8 and libuv are hidden when building Node as a library and native modules will fail to load because they cannot find the symbols.

So in order to make native modules work, the V8 and libuv symbols were exposed in Electron. For V8 this is done by forcing all symbols in Chromium's configuration file to be exposed. For libuv, it is achieved by setting the BUILDING_UV_SHARED=1 definition.

Starting Node in your app

After all the work of building and linking with Node, the final step is to run Node in your app.

Node doesn't provide many public APIs for embedding itself into other apps. 通常您只能调用 节点:start节点::Init 开始 新的节点实例。 However, if you are building a complex app based on Node, you have to use APIs like node::CreateEnvironment to precisely control every step.

In Electron, Node is started in two modes: the standalone mode that runs in the main process, which is similar to official Node binaries, and the embedded mode which inserts Node APIs into web pages. The details of this will be explained in a future post.