无障碍
🌐 Accessibility
Electron 应用中的可访问性问题类似于网站的可访问性问题,因为它们最终都是 HTML。
🌐 Accessibility concerns in Electron applications are similar to those of websites because they're both ultimately HTML.
手动启用辅助功能
🌐 Manually enabling accessibility features
在检测到辅助技术存在时(例如 Windows 上的 JAWS 或 macOS 上的 VoiceOver),Electron 应用会自动启用辅助功能。更多详情请参阅 Chrome 的 无障碍文档。
🌐 Electron applications will automatically enable accessibility features in the presence of assistive technology (e.g. JAWS on Windows or VoiceOver on macOS). See Chrome's accessibility documentation for more details.
你也可以在 Electron 应用中手动切换这些功能,或者通过在第三方本地软件中设置标志来实现。
🌐 You can also manually toggle these features either within your Electron application or by setting flags in third-party native software.
使用 Electron 的 API
🌐 Using Electron's API
通过使用 app.setAccessibilitySupportEnabled(enabled) API,你可以在应用偏好设置中手动向用户公开 Chrome 的可访问性树。请注意,用户的系统辅助工具优先于此设置,并会覆盖它。
🌐 By using the app.setAccessibilitySupportEnabled(enabled)
API, you can manually expose Chrome's accessibility tree to users in the application preferences.
Note that the user's system assistive utilities have priority over this setting and
will override it.
在第三方软件内
🌐 Within third-party software
macOS
在 macOS 上,第三方辅助技术可以通过编程设置 AXManualAccessibility 属性来切换 Electron 应用内的辅助功能:
🌐 On macOS, third-party assistive technology can toggle accessibility features inside
Electron applications by setting the AXManualAccessibility attribute
programmatically:
使用 Objective-C:
🌐 Using Objective-C:
CFStringRef kAXManualAccessibility = CFSTR("AXManualAccessibility");
+ (void)enableAccessibility:(BOOL)enable inElectronApplication:(NSRunningApplication *)app
{
AXUIElementRef appRef = AXUIElementCreateApplication(app.processIdentifier);
if (appRef == nil)
return;
CFBooleanRef value = enable ? kCFBooleanTrue : kCFBooleanFalse;
AXUIElementSetAttributeValue(appRef, kAXManualAccessibility, value);
CFRelease(appRef);
}
使用 Swift:
🌐 Using Swift:
import Cocoa
let name = CommandLine.arguments.count >= 2 ? CommandLine.arguments[1] : "Electron"
let pid = NSWorkspace.shared.runningApplications.first(where: {$0.localizedName == name})!.processIdentifier
let axApp = AXUIElementCreateApplication(pid)
let result = AXUIElementSetAttributeValue(axApp, "AXManualAccessibility" as CFString, true as CFTypeRef)
print("Setting 'AXManualAccessibility' \(error.rawValue == 0 ? "succeeded" : "failed")")